springboot+redis集群的三种方式

springboot2整合redis集群的三种方式
pom

		<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  1. 默认
#系统默认连接池,这种方式 redisTemplate 可直接使用默认,在使用的地方直接注入即可
spring.redis.cluster.nodes=192.168.100.1:6379,192.168.100.1:6380,192.168.100.2:6379,192.168.100.2:6380,192.168.100.3:6379,192.168.100.3:6380
spring.redis.cluster.max-redirects=3
spring.redis.pool.max-active=8
spring.redis.poll.max-idle=8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
spring.redis.timeout=6000
@Autowired
    private RedisTemplate<String, String> redisTemplate;

    @RequestMapping("/hello")
    public Object hello() {
        redisTemplate.opsForValue().set("1","111");
        Object result = redisTemplate.opsForValue().get("1");
        return result;
    }
  1. jedis
#使用jedis连接池,需要连接池注入配置信息,在使用的地方直接注入即可
spring.redis.cluster.nodes=192.168.100.1:6379,192.168.100.1:6380,192.168.100.2:6379,192.168.100.2:6380,192.168.100.3:6379,192.168.100.3:6380
spring.redis.cluster.max-redirects=3
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=6000
@Configuration
public class RedisJedisClusterConfig {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

}
@Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("/hello")
    public Object hello() {
        redisTemplate.opsForValue().set("1","111");
        Object result = redisTemplate.opsForValue().get("1");
        return result;
    }
  1. lettcue
#使用lettuce连接池,需要连接池注入配置信息,在使用的地方直接注入即可
spring.redis.cluster.nodes=192.168.100.1:6379,192.168.100.1:6380,192.168.100.2:6379,192.168.100.2:6380,192.168.100.3:6379,192.168.100.3:6380
spring.redis.cluster.max-redirects=3
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.min-idle=0
spring.redis.timeout=6000

@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisLettuceClusterConfig {

@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new StringRedisSerializer());
    redisTemplate.setConnectionFactory(lettuceConnectionFactory);
    return redisTemplate;
}

}

@Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("/hello")
    public Object hello() {
        redisTemplate.opsForValue().set("1","111");
        Object result = redisTemplate.opsForValue().get("1");
        return result;
    }

使用时可选择切换不同的方式,经测试ok。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值