springboot2 连接redis5集群,连接redis,连接redis哨兵模式,Jedis,Lettuce
一、引入依赖
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 要用redis连接池 必须有pool依赖-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!-- jackson -->
<!-- java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
二、使用Lettuce连接redis集群
springboot模式使用lettuce
application.yml
spring:
redis:
database: 0 # Redis数据库索引(默认为0)
#host: 192.168.0.201 # Redis服务器地址
#port: 7001 # Redis服务器连接端口
password: redis508 # Redis服务器连接密码(默认为空)
timeout: 5000ms # 连接超时时间(毫秒)默认是2000ms
lettuce:
pool:
max-active: 200 # 连接池最大连接数(使用负值表示没有限制)
max-idle: 20 # 连接池中的最大空闲连接
min-idle: 0 # 连接池中的最小空闲连接
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
#sentinel:
#master: mymaster #哨兵监听的master名称
#nodes: 192.168.203.205:26479, 192.168.203.205:26480 #哨兵地址列表,多个以,分割
cluster:
max-redirects: 3
nodes: 192.168.0.201:7001,192.168.0.201:7002,192.168.0.201:7003,192.168.0.201:7004,192.168.0.201:7005,192.168.0.201:7006
LettuceConfig.java
package com.xin.redis.rediscluster.config;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableCaching //支持缓存注解
public class LettuceConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer()); // key序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // value序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // Hash key序列化
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // Hash value序列化
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
RedisClusterTest.java
package com.xin.redis.rediscluster;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
//@Transactional //支持事物,@SpringBootTest 事物默认自动回滚
//@Rollback // 事务自动回滚,不自动回滚@Rollback(false)
class RedisClusterTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
void test() {
redisTemplate.opsForValue().set("spring","redis");
System.out.println(redisTemplate.opsForValue().get("spring"));
redisTemplate.opsForValue().set("foo","bar"); //这个键在集群中可能有节点跳转
System.out.println(redisTemplate.opsForValue().get("foo"));
}
}
三、使用Jedis连接redis集群
application.yml
就是把lettuce改成redis
spring:
redis:
database: 0 # Redis数据库索引(默认为0)
#host: 192.168.0.201 # Redis服务器地址
#port: 7001 # Redis服务器连接端口
password: redis508 # Redis服务器连接密码(默认为空)
timeout: 5000ms # 连接超时时间(毫秒)默认是2000ms
jedis:
pool:
max-active: 200 # 连接池最大连接数(使用负值表示没有限制)
max-idle: 20 # 连接池中的最大空闲连接
min-idle: 0 # 连接池中的最小空闲连接
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
#sentinel:
#master: mymaster #哨兵监听的master名称
#nodes: 192.168.203.205:26479, 192.168.203.205:26480 #哨兵地址列表,多个以,分割
cluster:
max-redirects: 3
nodes: 192.168.0.201:7001,192.168.0.201:7002,192.168.0.201:7003,192.168.0.201:7004,192.168.0.201:7005,192.168.0.201:7006
JedisConfig.java
就是把LettuceConnectionFactory改成RedisConnectionFactory
package com.xin.redis.rediscluster.config;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableCaching //支持缓存注解
public class JedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer()); // key序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // value序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // Hash key序列化
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // Hash value序列化
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
RedisClusterTest.java
package com.xin.redis.rediscluster;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
//@Transactional //支持事物,@SpringBootTest 事物默认自动回滚
//@Rollback // 事务自动回滚,不自动回滚@Rollback(false)
class RedisClusterTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
void test() {
redisTemplate.opsForValue().set("spring","redis");
System.out.println(redisTemplate.opsForValue().get("spring"));
redisTemplate.opsForValue().set("foo","bar"); //这个键在集群中可能有节点跳转
System.out.println(redisTemplate.opsForValue().get("foo"));
}
}
四、默认连接池连接单台
这种基本不用了
application.yml
spring:
redis:
database: 0 # Redis数据库索引(默认为0)
host: 192.168.0.201 # Redis服务器地址
port: 7001 # Redis服务器连接端口
password: redis508 # Redis服务器连接密码(默认为空)
timeout: 2000ms # 连接超时时间(毫秒)默认是2000ms
pool:
#Deprecated
max-active: 200 # 连接池最大连接数(使用负值表示没有限制)
max-idle: 20 # 连接池中的最大空闲连接
min-idle: 0 # 连接池中的最小空闲连接
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
RedisConfig.java
package com.xin.redis.rediscluster.config;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer()); // key序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // value序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // Hash key序列化
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // Hash value序列化
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
RedisTest.java
package com.xin.redis.rediscluster;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
//@Transactional //支持事物,@SpringBootTest 事物默认自动回滚
//@Rollback // 事务自动回滚,不自动回滚@Rollback(false)
class RedisTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
void test() {
redisTemplate.opsForValue().set("spring","redis");
System.out.println(redisTemplate.opsForValue().get("spring"));
}
}
还没有评论,来说两句吧...