一、背景
上篇文章我們介紹瞭如何在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做了單機以及叢集的實現。很有成就感,未來走向架構師的路還有很遠,繼續努力吧!