Java使用Redis
Redis 的安装略过
首先导入jedis.jar包
1. 连接 redis 服务
写个简单的程序:
package com.fpc.Test;import redis.clients.jedis.Jedis;
public class ManipulateRedis {
public static void main(String[] args ) {
//连接 10.0.20.251 的 Redis 服务
Jedis jedis = new Jedis("10.0.20.251");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查看服务是否运行</span> System.out.println("服务正在运行 : " +<span style="color: rgba(0, 0, 0, 1)"> jedis.ping()); }
}
注意:说下 2 个主要的坑点:
1.Redis 要配置下 redis.conf 文件
要配置什么呢?配置其监听的 ip 地址
2. 第二个要注意的就是 redis-server 的启动要加参数
如果不加 --protected-mode no,Java 运行报错误:
Exception in thread "main" 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) at redis.clients.jedis.Protocol.process(Protocol.java:161) at redis.clients.jedis.Protocol.read(Protocol.java:215) at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340) at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239) at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:196) at com.fpc.Test.ManipulateRedis.main(ManipulateRedis.java:11)
正常运行的结果:
Redis&Java 操作元素
package com.fpc.Test;import redis.clients.jedis.Jedis;
public class ManipulateRedis {
public static void main(String[] args ) {
//连接 10.0.20.251 的 Redis 服务
Jedis jedis = new Jedis("10.0.20.251");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查看服务是否运行</span> System.out.println("服务正在运行 : " +<span style="color: rgba(0, 0, 0, 1)"> jedis.ping()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置redis字符串数据</span> jedis.set("fpc", "fangpengcheng"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取存储的数据并输出</span> String value = jedis.get("fpc"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"Redis 存储的字符串为 :" +<span style="color: rgba(0, 0, 0, 1)"> value); }
}
运行结果:
Redis&Java 操作列表
package com.fpc.Test;import java.util.List;
import redis.clients.jedis.Jedis;
public class ManipulateRedis {
public static void main(String[] args ) {
//连接 10.0.20.251 的 Redis 服务
Jedis jedis = new Jedis("10.0.20.251");</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查看服务是否运行</span> System.out.println("服务正在运行 : " +<span style="color: rgba(0, 0, 0, 1)"> jedis.ping()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将数据存储到列表中,每次都是从左边push进入list</span> jedis.lpush("list", "fangpengcheng1"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"list", "fangpengcheng2"<span style="color: rgba(0, 0, 0, 1)">); jedis.lpush(</span>"list", "fangpengcheng3"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取存储的数据并输出</span> List<String> list = jedis.lrange("list", 0, 2<span style="color: rgba(0, 0, 0, 1)">); System.out.println(list.toString()); list </span>= jedis.lrange("list", 0, 3<span style="color: rgba(0, 0, 0, 1)">); System.out.println(list.toString()); list </span>= jedis.lrange("list", 0, 4<span style="color: rgba(0, 0, 0, 1)">); System.out.println(list.toString()); list </span>= jedis.lrange("list", 0, 5<span style="color: rgba(0, 0, 0, 1)">); System.out.println(list.toString()); list </span>= jedis.lrange("list", 0, 6<span style="color: rgba(0, 0, 0, 1)">); System.out.println(list.toString()); list </span>= jedis.lrange("list", 0, 7<span style="color: rgba(0, 0, 0, 1)">); System.out.println(list.toString()); list </span>= jedis.lrange("list", 0, 8<span style="color: rgba(0, 0, 0, 1)">); System.out.println(list.toString()); }
}
运行的结果是:
可见如果 jedis.lrange 中给出的范围是超过 Redis 中实际 list 的长度的话是会循环继续取元素的。
Redis&Java 也可以操作 Redis 中的 Set,Map 等,再这里就不赘述了,查下文档就可以。下面打算用 Redis&Java 实现一个消息队列。