本文分两部分:
(1)jedisCluster
(2)Spring-Data-Redis集群配置和RedisTemplate用法
我这里是在本机上搭建的3主3从6个redis实例。
一,jedisCluster
jar引入:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.4.2</version></dependency>12345
连接集群测试:
@Test public void testJedisCluster() throws Exception { // 第一步:使用JedisCluster对象。需要一个Set<HostAndPort>参数。Redis节点的列表。 Set<HostAndPort> nodes = new HashSet<>(); nodes.add(new HostAndPort("127.0.0.1", 7001)); nodes.add(new HostAndPort("127.0.0.1", 7002)); nodes.add(new HostAndPort("127.0.0.1", 7003)); nodes.add(new HostAndPort("127.0.0.1", 7004)); nodes.add(new HostAndPort("127.0.0.1", 7005)); nodes.add(new HostAndPort("127.0.0.1", 7006)); JedisCluster jedisCluster = new JedisCluster(nodes); // 第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。 jedisCluster.set("a", "1"); String result = jedisCluster.get("a"); // 第三步:打印结果 System.out.println(result); // 第四步:系统关闭前,关闭JedisCluster对象。 jedisCluster.close(); } 12345678910111213141516171819
基于Spring的配置:
<!-- 集群版的配置 --> <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7001"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7002"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7003"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7004"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7005"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7006"></constructor-arg> </bean> </set> </constructor-arg> </bean> <bean id="jedisClientCluster" class="com.demo.jedis.JedisClientCluster"/>1234567891011121314151617181920212223242526272829303132
接口定义:
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); } 123456789101112
实现类:
public class JedisClientCluster 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 Boolean exists(String key) { return jedisCluster.exists(key); } @Override public Long expire(String key, int seconds) { return jedisCluster.expire(key, seconds); } @Override public Long ttl(String key) { return jedisCluster.ttl(key); } @Override public Long incr(String key) { return jedisCluster.incr(key); } @Override public Long hset(String key, String field, String value) { return jedisCluster.hset(key, field, value); } @Override public String hget(String key, String field) { return jedisCluster.hget(key, field); } @Override public Long hdel(String key, String... field) { return jedisCluster.hdel(key, field); } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
最后可以在项目中直接使用接口类。
二,Spring-Data-Redis集群配置和RedisTemplate用法
注意版本与spring版本最好相差不多,不然会报错。
jar:
<!-- Redis --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.11.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.2</version> </dependency>1234567891011
spring-data-redis.xml配置:
<description>Jedis Cluster Configuration</description><!-- 加载配置属性文件 按需加载 --><context:property-placeholder ignore-unresolvable="true" location="classpath:redis.cluster.properties" /><!-- 配置Cluster --><bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <property name="maxRedirects" value="3"></property> <!-- 节点配置 --> <property name="clusterNodes"> <set> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7001"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7002"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7003"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7004"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7005"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7006"></constructor-arg> </bean> </set> </property></bean><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="100" /> <property name="maxTotal" value="600" /></bean><bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg ref="redisClusterConfiguration" /> <constructor-arg ref="jedisPoolConfig" /></bean><!-- redis 访问的模版 --><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jeidsConnectionFactory" /></bean>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
以上就是全部配置,可以使用模板操作:
@Autowired private RedisTemplate redisTemplate;
转自:https://blog.csdn.net/qq_22860341/article/details/80706325
2
0条评论
点击登录参与评论