Redis学习02——Jedis使用

Redis学习02Jedis使用
一准备jar包
二创建第一实例程序
三配置服务器
1 connect timed out
11 异常
12 解决方式
2 javanetConnectException Connection refused connect
21 异常
22 解决方式
四打印结果
五使用连接池
1 代码
2 结果
六使用配置的使用连接池
1 redisproperties
2 JedisPoolUtils 连接池工具类
3 测试代码
4 结果
Redis学习02——Jedis使用
一、准备jar包
我们需要使用到的jar包如下

jedis.jar

common-pool

下载地址如下

http://central.maven.org/maven2/redis/clients/jedis/2.9.0/

http://commons.apache.org/proper/commons-pool/download_pool.cgi

二、创建第一实例程序
private String ipAddress = "172.16.50.145";
private int port = 6379;

@Test
public void test1(){
//1.获取链接对象
Jedis jedis = new Jedis(ipAddress,port);

//2.获得数据
String username = jedis.get("username");
System.out.println(username);

//3.存储
jedis.set("addr","上海");
System.out.println(jedis.get("addr"));
jedis.close();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
但是如果你运行你会发现是报超时错,这是因为linux默认没有打开我们的6379端口。具体配置请看下面。

三、配置服务器
3.1 connect timed out
3.1.1 异常
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out

at redis.clients.jedis.Connection.connect(Connection.java:207)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
at redis.clients.jedis.Connection.sendCommand(Connection.java:117)
at redis.clients.jedis.Jedis.get(Jedis.java:152)
at com.qwm.redis.Demo1.test1(Demo1.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at redis.clients.jedis.Connection.connect(Connection.java:184)
... 27 more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
3.1.2 解决方式
这个是由于linux默认是没有允许访问 6379 端口的,我们需要配置可以访问这个端口。

使用 vim 修改 /etc/sysconfig/iptables

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
加上一行

-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
1
例如


# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
保存重启 iptables

[root@wiming ~]# service iptables restart
Redirecting to /bin/systemctl restart iptables.service
1
2
3.2 java.net.ConnectException: Connection refused: connect
3.2.1 异常
redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect

at redis.clients.jedis.Connection.connect(Connection.java:207)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
at redis.clients.jedis.Connection.sendCommand(Connection.java:117)
at redis.clients.jedis.Jedis.get(Jedis.java:152)
at com.qwm.redis.Demo1.test1(Demo1.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at redis.clients.jedis.Connection.connect(Connection.java:184)
... 27 more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
3.2.2 解决方式
第一步:

注释 redis.conf 中的 bind 127.0.0.1,还是一样使用vim 打开配置文件。找到 69行,修改如下

67 # JUST COMMENT THE FOLLOWING LINE.
68 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69 # bind 127.0.0.1
1
2
3
保存,重启redis,在看可一看是时候已经ok了,如果不行,再看第二步

第二步:

修改 redis.conf 中的 88 行**protected-mode no 
**,yes 改为no

86 # even if no authentication is configured, nor a specific set of interfaces
87 # are explicitly listed using the "bind" directive.
88 protected-mode no
1
2
3
4
保存重启redis,现在就ok了。

四、打印结果
wiming
上海
1
2
五、使用连接池
5.1 代码
@Test
public void test2(){
//1.创建连接池的配置对象
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(30);//最大闲置个数
config.setMinIdle(10);//最小闲置个数
config.setMaxTotal(50);//最大连接数

//2.创建连接池
JedisPool pool = new JedisPool(config,ipAddress,port);

//3.从连接池子中获取redis连接
Jedis jedis = pool.getResource();

//4.操作数据库
System.out.println(jedis.get("username"));

//5.关闭资源
jedis.close();
pool.close();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
5.2 结果
wiming
1
六、使用配置的使用连接池
我们可以使配置的方式来设置连接池的配置。选用 properties 相对简单好多,所以我们使用这个。

6.1 redis.properties
在src下创建 redis.properties

//最大闲置个数
redis.maxIdle=30
//最小闲置个数
redis.minIdle=10
//最大连接数
redis.maxTotal=50
//主机
redis.host=172.16.50.145
//端口
redis.port=6379
1
2
3
4
5
6
7
8
9
10
6.2 JedisPoolUtils 连接池工具类
package com.qwm.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
* @author:qiwenming
* @date:2017/9/24 0024 19:43
* @description:
* redis连接池工具类
*/
public class JedisPoolUtils {
private static JedisPool pool = null;
static {
//加载配置文件
InputStream in = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties");
Properties pro = new Properties();
try {
pro.load(in);
//最大闲置个数
int maxIdle = Integer.parseInt(pro.getProperty("redis.maxIdle"));
//最小闲置个数
int minIdle = Integer.parseInt(pro.getProperty("redis.minIdle"));
//最大连接数
int maxTotal = Integer.parseInt(pro.getProperty("redis.maxTotal"));
//主机
String host = pro.getProperty("redis.host");
//端口
int port = Integer.parseInt(pro.getProperty("redis.port"));

//配置参数
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMinIdle(minIdle);
poolConfig.setMaxTotal(maxTotal);

pool = new JedisPool(poolConfig,host,port);

} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 获得jedis资源的方法
* @return
*/
public static Jedis getJedis(){
return pool.getResource();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
6.3 测试代码
package com.qwm.redis;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
* @author:qiwenming
* @date:2017/9/24 0024 20:00
* @description:
*/
public class Demo2 {
@Test
public void test1(){
Jedis jedis = JedisPoolUtils.getJedis();
System.out.println(jedis.get("username"));

jedis.hset("myhash","s","kkkkkkk");
jedis.hset("myhash","k","fdsafdsafds");
jedis.hset("myhash","v","fdsafdsafds");

//获取myhash中的全部数据
Map<String,String> map = jedis.hgetAll("myhash");
Set<Map.Entry<String,String>> kv = map.entrySet();
Iterator<Map.Entry<String,String>> iterator = kv.iterator();
//迭代
while(iterator.hasNext()){
Map.Entry<String,String> entry = iterator.next();
System.out.println(entry.getKey()+"---"+entry.getValue());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
6.4 结果
wiming
v---fdsafdsafds
s---kkkkkkk
k---fdsafdsafds
--------------------- 
作者:愤怒的小明 
来源:CSDN 
原文:https://blog.csdn.net/qiwenmingshiwo/article/details/78081325 
版权声明:本文为博主原创文章,转载请附上博文链接!

posted @ 2019-04-18 20:38  一心二念  阅读(96)  评论(0编辑  收藏  举报