用网站名查询网址/seo基本步骤
如果是配置多个集群或者只有一个单机的Redis数据源可以参考:
https://blog.csdn.net/lonely_bin/article/details/100137626
如果配置单机+哨兵两种数据源可以用下方代码:
1、springboot关于redis的启动pom要引入,这个都是一样的
2、yml配置文件:
spring:redis:database: 2password: xxsentinel:master: mymaster# 逗号分隔, 逗号前后不要有空格nodes: 172.16.116.***:26381,172.16.116.###:26382,172.16.116.XXX:26383lettuce:pool:# 最大活跃链接数 默认8max-active: 5# 最大空闲连接数 默认8max-idle: 10# 最小空闲连接数 默认0min-idle: 0secondaryRedis:host: 172.16.100.xxport: 6379password: xxx
3、Redis配置类
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;import java.util.HashMap;
import java.util.Map;@Configuration
public class RedisConfig {@Autowiredprivate Environment environment;@Bean@Primary@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")public GenericObjectPoolConfig redisPool() {return new GenericObjectPoolConfig();}/*** 配置第一个数据源的** @return*/@Bean("redisSentinelConfig")@Primarypublic RedisSentinelConfiguration redisSentinelConfig() {Map<String, Object> source = new HashMap<>(8);source.put("spring.redis.sentinel.nodes", environment.getProperty("spring.redis.sentinel.nodes"));RedisSentinelConfiguration redisSentinelConfiguration;redisSentinelConfiguration = new RedisSentinelConfiguration(new MapPropertySource("RedisSentinelConfiguration", source));redisSentinelConfiguration.setMaster(environment.getProperty("spring.redis.sentinel.master"));redisSentinelConfiguration.setPassword(environment.getProperty("spring.redis.password"));return redisSentinelConfiguration;}/*** 配置第一个数据源的连接工厂* 这里注意:需要添加@Primary 指定bean的名称,目的是为了创建两个不同名称的LettuceConnectionFactory** @param redisPool* @param redisSentinelConfig* @return*/@Bean("lettuceConnectionFactory")@Primarypublic LettuceConnectionFactory lettuceConnectionFactory(GenericObjectPoolConfig redisPool, @Qualifier("redisSentinelConfig") RedisSentinelConfiguration redisSentinelConfig) {LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(redisPool).build();return new LettuceConnectionFactory(redisSentinelConfig, clientConfiguration);}/*** 配置第一个数据源的RedisTemplate* 注意:这里指定使用名称=factory 的 RedisConnectionFactory* 并且标识第一个数据源是默认数据源 @Primary** @param redisConnectionFactory* @return*/@Bean("redisTemplate")@Primarypublic RedisTemplate redisTemplate(@Qualifier("lettuceConnectionFactory") RedisConnectionFactory redisConnectionFactory) {return getRedisTemplate(redisConnectionFactory);}/*** 配置第二个数据源** @return*/@Bean("secondaryRedisStandaloneConfig")public RedisStandaloneConfiguration secondaryRedisConfig() {RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();redisStandaloneConfiguration.setHostName(environment.getProperty("spring.secondaryRedis.host"));redisStandaloneConfiguration.setPort(Integer.valueOf(environment.getProperty("spring.secondaryRedis.port")));redisStandaloneConfiguration.setPassword(environment.getProperty("spring.secondaryRedis.password"));return redisStandaloneConfiguration;}@Bean("secondaryLettuceConnectionFactory")public LettuceConnectionFactory secondaryLettuceConnectionFactory(GenericObjectPoolConfig redisPool, @Qualifier("secondaryRedisStandaloneConfig")RedisStandaloneConfiguration secondaryRedisStandaloneConfig) {LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(redisPool).build();return new LettuceConnectionFactory(secondaryRedisStandaloneConfig, clientConfiguration);}/*** 配置第一个数据源的RedisTemplate* 注意:这里指定使用名称=factory2 的 RedisConnectionFactory** @param redisConnectionFactory* @return*/@Bean("secondaryRedisTemplate")public RedisTemplate secondaryRedisTemplate(@Qualifier("secondaryLettuceConnectionFactory") RedisConnectionFactory redisConnectionFactory) {return getRedisTemplate(redisConnectionFactory);}private RedisTemplate getRedisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);// key采用String的序列化方式template.setKeySerializer(stringRedisSerializer);// hash的key也采用String的序列化方式template.setHashKeySerializer(stringRedisSerializer);// value序列化方式采用FastJSON的序列化方式template.setValueSerializer(fastJsonRedisSerializer);// hash的value序列化方式采用FastJSON的序列化方式template.setHashValueSerializer(fastJsonRedisSerializer);template.afterPropertiesSet();return template;}}
4、使用:
@Autowired
@Qualifier("redisTemplate")
private RedisTemplate redisTemplate;@Autowired
@Qualifier("secondaryRedisTemplate")
private RedisTemplate secondaryRedisTemplate;
5、使用的时候出现了bug:
ClassCastException:Long cannot be cast to String
明明存的时候是String,接的时候也是String,但是debug发现序列化的时候序列化成了Long类型,这个set是long类型的了,后边按照String类型便利则报类型转换异常了
解决:
由于redisTemplate配置的序列化为json,所以序列化的问题,修改: