Jedis介绍和入门案例

来源:互联网 发布:java架构师视频百度云 编辑:程序博客网 时间:2024/06/04 19:04

一.Jedis介绍

Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如Java,C,C#,c++,php,Node.js,Go等,在官方网站里列一些Java的客户端,有Jedis,Redisson,Jredis,JDBC-Redis,等其中官方推荐使用Jedis和Redisson,在企业中用的最多的就是Jedis,下面我们就重点学习下Jedis

Jedis同样也是托管在github上,地址:https://github.com/xetorthio/jedis

二.入门案例

1.导包用eclipse连接redis

这里写图片描述

Jedis入门包下载

2.进行入门案例

这里写图片描述

public class Demo {             @Test             public void fun1(){                 Jedis jedis=new Jedis("115.159.99.209",6379);//设置连接的ip地址和端口号                 jedis.set("name","张三");//直接拿来用             }}

但是此时我们运行程序会报错
这里写图片描述

明显是连接的错误,这个就是我们并没有让防火墙开放6379端口的锅

像这种情况,有很多种解决方法,直接关闭防火墙,或者开放6379端口,这里我们就选择后者

这里要注意一下对于Linux7之前和之后,设置方法不同,

大家可以看看这两篇文章

http://www.cnblogs.com/shiguotao-com/p/4562137.html

http://www.linuxidc.com/Linux/2015-03/114749.htm

firewall-cmd --zone=public --add-port=端口号/tcp --permanent //开放端口号firewall-cmd --reload  //重启防火墙

这里写图片描述

此刻我们或许会认为连接没问题了,但是接下来就有问题

Caused by: redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.    at redis.clients.jedis.Protocol.processError(Protocol.java:127) ~[jedis-2.9.0.jar:na]    at redis.clients.jedis.Protocol.process(Protocol.java:161) ~[jedis-2.9.0.jar:na]    at redis.clients.jedis.Protocol.read(Protocol.java:215) ~[jedis-2.9.0.jar:na]    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340) ~[jedis-2.9.0.jar:na]    at redis.clients.jedis.Connection.getIntegerReply(Connection.java:265) ~[jedis-2.9.0.jar:na]    at redis.clients.jedis.BinaryJedis.exists(BinaryJedis.java:279) ~[jedis-2.9.0.jar:na]    at org.springframework.data.redis.connection.jedis.JedisConnection.exists(JedisConnection.java:813) ~[spring-data-redis-1.8.3.RELEASE.jar:na]    ... 14 common frames omitted

这个问题是由于redis没有配置密码的原因导致的,只需要为redis设置密码即可

在客户端中执行如下两个命令

config get requirepass     //这是查询redis是否配置密码,如果返回为空,则表明未配置密码。 config set requirepass "admin"   //“admin”这是将redis的密码设置为“admin”

这里写图片描述

此时,由于你设置了密码,所以我们代码也要添上一行,为redis连接设置密码,如果你不加,执行运行又会报如下错误
这里写图片描述

public class Demo {             @Test             public void fun1(){                 Jedis jedis=new Jedis("115.159.99.209",6379);                 jedis.auth("admin");                 jedis.set("name","张三");             }}

此时,运行代码成功,我们可以去客户端查看

这里写图片描述

由于你设置了密码,所以我们要输入密码才能进行其他操作

这里写图片描述

充分说明redis存储的二进制数据,它与mysql等数据库不同,中间不会进行多次解码编码

当你在eclipse中打印时,不会出现乱码的,它打印前进行了解码的

这里写图片描述

充分说明二进制存储数据的安全性!!!!!!!!!
end!!!!!!!!!