使用Java客户端对Redis进行操作
Posted tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用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 public void testJedisSingle() { // 创建一个jedis连接 Jedis client = new Jedis("192.168.25.153", 6379); client.set("name", "zhangsan"); String name = client.get("name"); System.out.println(name); // 关闭连接 client.close(); } @Test public void testJedisPoolSingle() { // 创建一个连接池对象,系统中应该是单例的 JedisPool pool = new JedisPool("192.168.25.153", 6379); // 从连接池中获取一个连接 Jedis client = pool.getResource(); client.set("age", "100"); String name = client.get("name"); String age = client.get("age"); System.out.println(name); System.out.println(age); // jedis必须关闭连接 client.close(); // 关闭连接池 pool.close(); } @Test public void testJedisCluster() { // 创建一个jedisCluster对象的节点集合 Set<HostAndPort> nodes = new HashSet<>(); // 在nodes中指定每个节点的地址 nodes.add(new HostAndPort("192.168.25.153", 7001)); nodes.add(new HostAndPort("192.168.25.153", 7002)); nodes.add(new HostAndPort("192.168.25.153", 7003)); nodes.add(new HostAndPort("192.168.25.153", 7004)); nodes.add(new HostAndPort("192.168.25.153", 7005)); nodes.add(new HostAndPort("192.168.25.153", 7006)); // 创建一个jedisCluster对象,该对象在系统中应该是单例的 JedisCluster cluster = new JedisCluster(nodes); cluster.set("id", "100"); cluster.set("value", "Hello Jedis"); System.out.println(cluster.get("id")); System.out.println(cluster.get("value")); // 系统关闭时,关闭集群 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"> <!-- Redis stand-alone config --> <bean id = "jedisPool" class = "redis.clients.jedis.JedisPool"> <constructor-arg name="host" value="${redis.single.host}"/> <constructor-arg name="port" value="${redis.single.port}"/> </bean> <bean id = "singleJedisClient" class = "com.taotao.rest.component.impl.SingleJedisClient"/> <!-- Redis cluster config --> <!-- <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"/> --> </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, int 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 public void testJedisClientSpring() throws Exception { //创建一个spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml"); //从容器中获得JedisClient对象 JedisClient jedisClient = applicationContext.getBean(JedisClient.class); //jedisClient操作redis jedisClient.set("cliet1", "1000"); String string = jedisClient.get("cliet1"); System.out.println(string); } }
四、总结
通过本文我们对jedis的使用有了进一步的了解,知道了如何使用spring对redis进行集成,也对jedisClient做了单机以及集群的实现。很有成就感,未来走向架构师的路还有很远,继续努力吧!
以上是关于使用Java客户端对Redis进行操作的主要内容,如果未能解决你的问题,请参考以下文章