使用Java客户端对Redis进行操作
一、背景
上篇文章我们介绍了如何在 centos7 下面进行安装单机版 redis 以及 redis 集群。这篇文章,我们来聊一聊如何使用 java 客户端来进行操作 redis。我们知道 redis 的 java 客户端有很多, 如:jedis、redission 等。这篇文章着重介绍我们平常使用最多的 redis 的 java 客户端 jedis。
二、通过单元测试来小试牛刀
1. 首先在 maven 的 pom.xml 中引入 jedis-client 的依赖
复制<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> </dependency>
2. 使用 junit 进行单元测试
复制package com.hafiz.redis.test;import java.util.HashSet;
import java.util.Set;import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;public class JedisTest {
@Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testJedisSingle() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个jedis连接</span> Jedis client = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis("192.168.25.153", 6379<span style="color: rgba(0, 0, 0, 1)">); client.set(</span>"name", "zhangsan"<span style="color: rgba(0, 0, 0, 1)">); String name </span>= client.get("name"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(name); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 关闭连接</span>
client.close();
}@Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testJedisPoolSingle() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个连接池对象,系统中应该是单例的</span> JedisPool pool = <span style="color: rgba(0, 0, 255, 1)">new</span> JedisPool("192.168.25.153", 6379<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> Jedis client =<span style="color: rgba(0, 0, 0, 1)"> pool.getResource(); client.set(</span>"age", "100"<span style="color: rgba(0, 0, 0, 1)">); String name </span>= client.get("name"<span style="color: rgba(0, 0, 0, 1)">); String age </span>= client.get("age"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(name); System.out.println(age); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> jedis必须关闭连接</span>
client.close();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 关闭连接池</span>
pool.close();
}@Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testJedisCluster() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个jedisCluster对象的节点集合</span> Set<HostAndPort> nodes = <span style="color: rgba(0, 0, 255, 1)">new</span> HashSet<><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)"> 在nodes中指定每个节点的地址</span> nodes.add(<span style="color: rgba(0, 0, 255, 1)">new</span> HostAndPort("192.168.25.153", 7001<span style="color: rgba(0, 0, 0, 1)">)); nodes.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> HostAndPort("192.168.25.153", 7002<span style="color: rgba(0, 0, 0, 1)">)); nodes.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> HostAndPort("192.168.25.153", 7003<span style="color: rgba(0, 0, 0, 1)">)); nodes.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> HostAndPort("192.168.25.153", 7004<span style="color: rgba(0, 0, 0, 1)">)); nodes.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> HostAndPort("192.168.25.153", 7005<span style="color: rgba(0, 0, 0, 1)">)); nodes.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span> HostAndPort("192.168.25.153", 7006<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)"> 创建一个jedisCluster对象,该对象在系统中应该是单例的</span> JedisCluster cluster = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisCluster(nodes); cluster.set(</span>"id", "100"<span style="color: rgba(0, 0, 0, 1)">); cluster.set(</span>"value", "Hello Jedis"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(cluster.get(</span>"id"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(cluster.get(</span>"value"<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>
cluster.close();
}
}
三、在项目中,使用 Spring 集成 Redis
1.redis.properties 文件如下:
复制#Redis stand-alone config redis.single.host=192.168.25.153 redis.single.port=6379#Redis cluster config
redis.cluster.node1.host=192.168.25.153
redis.cluster.node1.port=7001
redis.cluster.node2.host=192.168.25.153
redis.cluster.node2.port=7002
redis.cluster.node3.host=192.168.25.153
redis.cluster.node3.port=7003
redis.cluster.node4.host=192.168.25.153
redis.cluster.node4.port=7004
redis.cluster.node5.host=192.168.25.153
redis.cluster.node5.port=7005
redis.cluster.node6.host=192.168.25.153
redis.cluster.node6.port=7006
2.spring-redis 配置文件如下:
复制<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><span style="color: rgba(0, 128, 0, 1)"><!--</span><span style="color: rgba(0, 128, 0, 1)"> Redis stand-alone config </span><span style="color: rgba(0, 128, 0, 1)">--></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">bean </span><span style="color: rgba(255, 0, 0, 1)">id </span><span style="color: rgba(0, 0, 255, 1)">= "jedisPool"</span><span style="color: rgba(255, 0, 0, 1)"> class </span><span style="color: rgba(0, 0, 255, 1)">= "redis.clients.jedis.JedisPool"</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">constructor-arg </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="host"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.single.host}"</span><span style="color: rgba(0, 0, 255, 1)">/></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">constructor-arg </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="port"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="${redis.single.port}"</span><span style="color: rgba(0, 0, 255, 1)">/></span> <span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">bean</span><span style="color: rgba(0, 0, 255, 1)">></span> <span style="color: rgba(0, 0, 255, 1)"><</span><span style="color: rgba(128, 0, 0, 1)">bean </span><span style="color: rgba(255, 0, 0, 1)">id </span><span style="color: rgba(0, 0, 255, 1)">= "singleJedisClient"</span><span style="color: rgba(255, 0, 0, 1)"> class </span><span style="color: rgba(0, 0, 255, 1)">= "com.taotao.rest.component.impl.SingleJedisClient"</span><span style="color: rgba(0, 0, 255, 1)">/></span><br><br><span style="color: rgba(0, 128, 0, 1)"> <!--</span><span style="color: rgba(0, 128, 0, 1)"> Redis cluster config </span><span style="color: rgba(0, 128, 0, 1)">--><br></span><span style="color: rgba(0, 128, 0, 1)"> <!--</span><span style="color: rgba(0, 128, 0, 1)"> <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg name="nodes"> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${redis.cluster.node1.host}"/> <constructor-arg name="port" value="${redis.cluster.node1.port}"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${redis.cluster.node2.host}"/> <constructor-arg name="port" value="${redis.cluster.node2.port}"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${redis.cluster.node3.host}"/> <constructor-arg name="port" value="${redis.cluster.node3.port}"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${redis.cluster.node4.host}"/> <constructor-arg name="port" value="${redis.cluster.node4.port}"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${redis.cluster.node5.host}"/> <constructor-arg name="port" value="${redis.cluster.node5.port}"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="${redis.cluster.node6.host}"/> <constructor-arg name="port" value="${redis.cluster.node6.port}"/> </bean> </set> </constructor-arg> </bean> <bean id="clusterJedisClient" class="com.hafiz.rest.component.impl.ClusterJedisClient"/> </span><span style="color: rgba(0, 128, 0, 1)">--></span>
</beans>
默认放开的是单机版配置,如需使用集群版请注释上面单机版配置,并打开下面集群版配置。
3. 接着我们编写一个 JedisClient 接口类
复制package com.hafiz.component;public interface JedisClient {
String set(String key, String value); String get(String key); Long del(String key); Long hset(String key, String item, String value); String hget(String key, String item); Long hdel(String key, String... item); Long incr(String key); Long decr(String key); Long expire(String key, </span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> seconds); Long ttl(String key);
}
4. 然后我们给出单机版的实现类:SingleJedisClient.java
复制package com.hafiz.component.impl;import org.springframework.beans.factory.annotation.Autowired;
import com.taotao.rest.component.JedisClient;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;/**
Redis 单机客户端工具类
@author Administrator
*/
public class SingleJedisClient implements JedisClient {@Autowired
private JedisPool jedisPool;@Override
public String set(String key, String value) {
Jedis client = jedisPool.getResource();
String result = client.set(key, value);
client.close();
return result;
}@Override
public String get(String key) {
Jedis client = jedisPool.getResource();
String result = client.get(key);
client.close();
return result;
}@Override
public Long del(String key) {
Jedis client = jedisPool.getResource();
Long result = client.del(key);
client.close();
return result;
}@Override
public Long hset(String key, String item, String value) {
Jedis client = jedisPool.getResource();
Long result = client.hset(key, item, value);
client.close();
return result;
}@Override
public String hget(String key, String item) {
Jedis client = jedisPool.getResource();
String result = client.hget(key, item);
client.close();
return result;
}@Override
public Long hdel(String key, String... item) {
Jedis client = jedisPool.getResource();
Long result = client.hdel(key, item);
client.close();
return result;
}@Override
public Long incr(String key) {
Jedis client = jedisPool.getResource();
Long result = client.incr(key);
client.close();
return result;
}@Override
public Long decr(String key) {
Jedis client = jedisPool.getResource();
Long result = client.decr(key);
client.close();
return result;
}@Override
public Long expire(String key, int seconds) {
Jedis client = jedisPool.getResource();
Long result = client.expire(key, seconds);
client.close();
return result;
}@Override
public Long ttl(String key) {
Jedis client = jedisPool.getResource();
Long result = client.ttl(key);
client.close();
return result;
}}
5. 我们再提供集群版的实现类:ClusterJedisClient.java
复制package com.hafiz.component.impl;import org.springframework.beans.factory.annotation.Autowired;
import com.taotao.rest.component.JedisClient;
import redis.clients.jedis.JedisCluster;
/**
Redis 集群客户端工具类
@author Administrator
*/
public class ClusterJedisClient implements JedisClient {@Autowired
private JedisCluster jedisCluster;@Override
public String set(String key, String value) {
return jedisCluster.set(key, value);
}@Override
public String get(String key) {
return jedisCluster.get(key);
}@Override
public Long del(String key) {
return jedisCluster.del(key);
}@Override
public Long hset(String key, String item, String value) {
return jedisCluster.hset(key, item, value);
}@Override
public String hget(String key, String item) {
return jedisCluster.hget(key, item);
}@Override
public Long hdel(String key, String... item) {
return jedisCluster.hdel(key, item);
}@Override
public Long incr(String key) {
return jedisCluster.incr(key);
}@Override
public Long decr(String key) {
return jedisCluster.decr(key);
}@Override
public Long expire(String key, int seconds) {
return jedisCluster.expire(key, seconds);
}@Override
public Long ttl(String key) {
return jedisCluster.ttl(key);
}}
6.spring 集成 redis 单元测试
复制package com.taotao.rest.test;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.taotao.rest.component.JedisClient;
public class SpringJedisTest {
@Test </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> testJedisClientSpring() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建一个spring容器</span> ApplicationContext applicationContext = <span style="color: rgba(0, 0, 255, 1)">new</span> ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml"<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)">从容器中获得JedisClient对象</span> JedisClient jedisClient = applicationContext.getBean(JedisClient.<span style="color: rgba(0, 0, 255, 1)">class</span><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)">jedisClient操作redis</span> jedisClient.set("cliet1", "1000"<span style="color: rgba(0, 0, 0, 1)">); String string </span>= jedisClient.get("cliet1"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(string); }
}
四、总结
通过本文我们对 jedis 的使用有了进一步的了解,知道了如何使用 spring 对 redis 进行集成,也对 jedisClient 做了单机以及集群的实现。很有成就感,未来走向架构师的路还有很远,继续努力吧!