spring-boot - 整合Reids集群

【spring-boot - 整合Reids集群】

1.引入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--SpringBoot与Redis整合依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>


2.配置application.properties

server.port=8080
server.servlet.encoding.charset=UTF-8
# 应用名称
spring.application.name=redis-sample
spring.jackson.date-format='yyyy-MM-dd HH:mm:ss'
spring.jackson.time-zone=GMT+8
# non_null - 字段值非空才返回该字段,否则返回体中不包含该字段
# always - 始终包含该属性,与该属性的值无关
spring.jackson.default-property-inclusion=always

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

# ===============================redis cluster nodes config=================================

# 最大的要重定向的次数
spring.redis.cluster.max-redirects=3
spring.redis.cluster.nodes=192.168.0.111:6379,192.168.0.112:6379,192.168.0.113:6379,192.168.0.114:6379,192.168.0.115:6379,192.168.0.116:6379
# 连接密码
spring.redis.password=123456
# 连接池配置,这里使用的lettuce
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0

# lettuce 支持集群拓扑动态感知刷新,自适应拓扑刷新是否使用所有可用的更新,默认关闭:false,改为:true
spring.redis.lettuce.cluster.refresh.adaptive=true
# 定时刷新,单位: 毫秒ms
spring.redis.lettuce.cluster.refresh.period=2000


3.出于序列化考虑,编写配置类:RedisConfig,指定RedisTemplate对应的key-value序列化方式

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
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {

        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        //设置key序列化方式: string
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //设置value的序列化方式json,使用 GenericJackson2JsonRedisSerializer 替换默认序列化
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());


        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        redisTemplate.afterPropertiesSet();
        return redisTemplate;

    }


}


4.使用

    @Autowired
    private RedisTemplate redisTemplate;

    // 写入
    redisTemplate.opsForValue().set(key, value);
    // 获取
    Object obj = redisTemplate.opsForValue().get(key);









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值