Redis

时间:2023-11-29 本站 点击:1

背景

有些时候在一个项目里,由于业务问题,可能仅仅操作一个Redis数据源已经不能满足,比如某个运营系统,对接着多个不同的服务,处理数据时又不想通过远程调用,那只能增加一个数据源来解决问题,像MySQL的多数据源一样。

配置示例

这里连接池选用的是lettuce

redis:host:localhostport:6379password:123456timeout:60000database:10lettuce:pool:min-idle:0max-idle:10max-wait:-1max-active:200time-between-eviction-runs:-1redis-live:host:localhostport:6379password:123456database:0

实现代码

关于序列化使用的是jackson

以下提供了创建RedisTemplate以及StringRedisTemplate

关于二者的区别:

两者的关系是StringRedisTemplate继承RedisTemplate。

两者的数据是不共通的;也就是说StringRedisTemplate只能管理StringRedisTemplate里面的数据,RedisTemplate只能管理RedisTemplate中的数据。

SDR默认采用的序列化策略有两种,一种是String的序列化策略,一种是JDK的序列化策略。

StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。 RedisTemplate默认采用的是JDK的序列化策略,保存的key和value都是采用此策略序列化保存的。

/***<p>*RedisLettuce连接实例配置*</p>**@authorzhengshangjin*@version1.0.0*@since1.0.0*createdon2020-04-23*/@Configuration@EnableCachingpublicclassRedisLettuceConfig{/***live数据源*/@Value("${spring.redis-live.host}")privateStringredisLiveHost;@Value("${spring.redis-live.port}")privateintredisLivePort;@Value("${spring.redis-live.password}")privateStringredisLivePass;@Value("${spring.redis-live.database}")privateintredisLiveDb;/***公共配置*/@Value("${spring.redis.timeout}")privatelongtimeout;@Value("${spring.redis.lettuce.pool.min-idle}")privateintminIdle;@Value("${spring.redis.lettuce.pool.max-idle}")privateintmaxIdle;@Value("${spring.redis.lettuce.pool.max-active}")privateintmaxActive;@Value("${spring.redis.lettuce.pool.max-wait}")privateintmaxWait;/***装配RedisTemplate*<p>*这里根据默认连接配置装配实例*</>**@paramredisConnectionFactory默认*@returnredisTemplate*@authorzhengshangjin*createdon2020-04-23*/@Bean(name="redisTemplate")publicRedisTemplate<Object,Object>redisTemplate(RedisConnectionFactoryredisConnectionFactory){returncreateRedisTemplate(redisConnectionFactory);}/***装配StringRedisTemplate*<p>*这里根据默认连接配置装配实例*</>*@paramredisConnectionFactory默认*@returnStringRedisTemplate*@authorzhengshangjin*createdon2020-04-23*/@Bean(name="stringRedisTemplate")publicStringRedisTemplatestringRedisTemplate(RedisConnectionFactoryredisConnectionFactory){returncreateStringRedisTemplate(redisConnectionFactory);}/***装配Live数据源**@returnliveStringRedisTemplate*@authorzhengshangjin*createdon2020-04-23*/@Bean(name="liveStringRedisTemplate")publicStringRedisTemplateliveStringRedisTemplate(){returncreateStringRedisTemplate(redisLiveHost,redisLivePort,redisLivePass,redisLiveDb);}/***创建RedisTemplate**@paramredisConnectionFactoryredisConnectionFactory*@returnRedisTemplate*@authorzhengshangjin*createdon2020-04-23*/publicRedisTemplate<Object,Object>createRedisTemplate(RedisConnectionFactoryredisConnectionFactory){ObjectMapperobjectMapper=newObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL,JsonTypeInfo.As.PROPERTY);Jackson2JsonRedisSerializer<?>serializer=newJackson2JsonRedisSerializer<>(Object.class);serializer.setObjectMapper(objectMapper);RedisTemplate<Object,Object>redisTemplate=newRedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);redisTemplate.setKeySerializer(newStringRedisSerializer());redisTemplate.setValueSerializer(serializer);redisTemplate.setHashKeySerializer(newStringRedisSerializer());redisTemplate.setHashValueSerializer(serializer);redisTemplate.afterPropertiesSet();returnredisTemplate;}/***创建StringRedisTemplate**@paramredisConnectionFactoryredisConnectionFactory*@returnStringRedisTemplate*@authorzhengshangjin*createdon2020-04-23*/publicStringRedisTemplatecreateStringRedisTemplate(RedisConnectionFactoryredisConnectionFactory){StringRedisTemplatestringRedisTemplate=newStringRedisTemplate();stringRedisTemplate.setConnectionFactory(redisConnectionFactory);returnstringRedisTemplate;}/***创建StringRedisTemplate**@paramhost主机*@paramport端口*@parampassword密码*@paramdatabase库*@returnStringRedisTemplate*@authorzhengshangjin*createdon2020-04-23*/publicStringRedisTemplatecreateStringRedisTemplate(Stringhost,intport,Stringpassword,intdatabase){//基本配置RedisStandaloneConfigurationconfiguration=newRedisStandaloneConfiguration();configuration.setHostName(host);configuration.setPort(port);configuration.setDatabase(database);if(ObjectUtils.isNotEmpty(password)){RedisPasswordredisPassword=RedisPassword.of(password);configuration.setPassword(redisPassword);}//连接池通用配置GenericObjectPoolConfig<?>genericObjectPoolConfig=newGenericObjectPoolConfig<>();genericObjectPoolConfig.setMaxTotal(maxActive);genericObjectPoolConfig.setMinIdle(minIdle);genericObjectPoolConfig.setMaxIdle(maxIdle);genericObjectPoolConfig.setMaxWaitMillis(maxWait);//LettucePoolLettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilderbuilder=LettucePoolingClientConfiguration.builder();builder.poolConfig(genericObjectPoolConfig);builder.commandTimeout(Duration.ofSeconds(timeout));LettuceConnectionFactoryconnectionFactory=newLettuceConnectionFactory(configuration,builder.build());connectionFactory.afterPropertiesSet();StringRedisTemplatestringRedisTemplate=newStringRedisTemplate();stringRedisTemplate.setConnectionFactory(connectionFactory);returnstringRedisTemplate;}}

注入使用

根据@Qualifier指定beanname来获取

@Autowired@Qualifier("liveStringRedisTemplate")publicStringRedisTemplatestringRedisTemplate;


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:/SpringBoot/362.html