本文主要是介绍spring + jedis 连接 redis集群,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先引入jedis的pom依赖:
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.7.2</version>
</dependency>
下面是项目结构图:
redis.properties:
redis.host1=192.168.1.118
redis.host2=192.168.1.118
redis.host3=192.168.1.118
redis.host4=192.168.1.118
redis.host5=192.168.1.118
redis.host6=192.168.1.118
redis.port1=7001
redis.port2=7002
redis.port3=7003
redis.port4=7004
redis.port5=7005
redis.port6=7006
连接单机版redis:
applicationContext-redis.xml 文件内容:
<?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.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!-- 加载配置文件 --><context:property-placeholder location="classpath:properties/redis.properties" /><context:annotation-config/><!-- redis 单机版 --><bean id="jedisPool" class="redis.clients.jedis.JedisPool"><constructor-arg name="host" value="${redis.host1}"/><constructor-arg name="port" value="${redis.port1}"/></bean><bean id="jedisClientPool" class="com.XXX.jedis.JedisClientPool"></bean>
</beans>
JedisClient 接口:
public interface JedisClient {String set(String key, String value);String get(String key);Boolean exists(String key);Long expire(String key, int seconds);Long ttl(String key);Long incr(String key);Long hset(String key, String field, String value);String hget(String key, String field);Long hdel(String key, String... field);
}
JedisClientPool 单机版实现类:
public class JedisClientPool implements JedisClient {@Autowiredprivate JedisPool jedisPool;@Overridepublic String set(String key, String value) {Jedis jedis = jedisPool.getResource();String result = jedis.set(key, value);jedis.close();return result;}@Overridepublic String get(String key) {Jedis jedis = jedisPool.getResource();String result = jedis.get(key);jedis.close();return result;}@Overridepublic Boolean exists(String key) {Jedis jedis = jedisPool.getResource();Boolean result = jedis.exists(key);jedis.close();return result;}@Overridepublic Long expire(String key, int seconds) {Jedis jedis = jedisPool.getResource();Long result = jedis.expire(key, seconds);jedis.close();return result;}@Overridepublic Long ttl(String key) {Jedis jedis = jedisPool.getResource();Long result = jedis.ttl(key);jedis.close();return result;}@Overridepublic Long incr(String key) {Jedis jedis = jedisPool.getResource();Long result = jedis.incr(key);jedis.close();return result;}@Overridepublic Long hset(String key, String field, String value) {Jedis jedis = jedisPool.getResource();Long result = jedis.hset(key, field, value);jedis.close();return result;}@Overridepublic String hget(String key, String field) {Jedis jedis = jedisPool.getResource();String result = jedis.hget(key, field);jedis.close();return result;}@Overridepublic Long hdel(String key, String... field) {Jedis jedis = jedisPool.getResource();Long result = jedis.hdel(key, field);jedis.close();return result;}}
JedisClientCluster 集群版实现类:
public class JedisClientCluster implements JedisClient {@Autowiredprivate JedisCluster jedisCluster;@Overridepublic String set(String key, String value) {return jedisCluster.set(key, value);}@Overridepublic String get(String key) {return jedisCluster.get(key);}@Overridepublic Boolean exists(String key) {return jedisCluster.exists(key);}@Overridepublic Long expire(String key, int seconds) {return jedisCluster.expire(key, seconds);}@Overridepublic Long ttl(String key) {return jedisCluster.ttl(key);}@Overridepublic Long incr(String key) {return jedisCluster.incr(key);}@Overridepublic Long hset(String key, String field, String value) {return jedisCluster.hset(key, field, value);}@Overridepublic String hget(String key, String field) {return jedisCluster.hget(key, field);}@Overridepublic Long hdel(String key, String... field) {return jedisCluster.hdel(key, field);}}
TestJredisClient 测试方法:
@Testpublic void TestJredisClient() {// 初始化spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");// 从容器中获得JedisClient对象JedisClient jedisClient = applicationContext.getBean(JedisClient.class);// 使用JedisClient对象操作redisjedisClient.set("jedisclient", "mytest");String result = jedisClient.get("jedisclient");System.out.println(result);}
测试结果:
集群版applicationContext.redis.xml 配置:
<?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.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!-- 加载配置文件 --><context:property-placeholder location="classpath:properties/redis.properties" /><context:annotation-config/><!-- redis 单机版 --><!-- <bean id="jedisPool" class="redis.clients.jedis.JedisPool"><constructor-arg name="host" value="${redis.host1}"/><constructor-arg name="port" value="${redis.port1}"/></bean><bean id="jedisClientPool" class="com.XXX.jedis.JedisClientPool"></bean> --><!-- redis 集群版 --><bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"><constructor-arg><set><bean class="redis.clients.jedis.HostAndPort"><constructor-arg name="host" value="${redis.host1}"/><constructor-arg name="port" value="${redis.port1}"/></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg name="host" value="${redis.host2}"/><constructor-arg name="port" value="${redis.port2}"/></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg name="host" value="${redis.host3}"/><constructor-arg name="port" value="${redis.port3}"/></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg name="host" value="${redis.host4}"/><constructor-arg name="port" value="${redis.port4}"/></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg name="host" value="${redis.host5}"/><constructor-arg name="port" value="${redis.port5}"/></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg name="host" value="${redis.host6}"/><constructor-arg name="port" value="${redis.port6}"/></bean></set></constructor-arg></bean> <bean id="jedisClientCluster" class="com.XXX.jedis.JedisClientCluster"></bean></beans>
测试结果和上面单机版一致
这篇关于spring + jedis 连接 redis集群的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!