您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)springboot redis如何使用lettuce配置多數(shù)據(jù)源,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
springboot一種全新的編程規(guī)范,其設(shè)計(jì)目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程,SpringBoot也是一個服務(wù)于框架的框架,服務(wù)范圍是簡化配置文件。
目前項(xiàng)目上需要連接兩個redis數(shù)據(jù)源,一個redis數(shù)據(jù)源是單機(jī)模式,一個redis數(shù)據(jù)源是分片集群模式,這里將具體配置列一下。
項(xiàng)目用的springboot版本為
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
redis: cluster: nodes: 127.0.0.1:9001 lettuce: #連接池配置 pool: #連接池最大連接數(shù) max-active: 20 #連接池最大等待時間,負(fù)數(shù)表示不做限制 max-wait: -1 #最大空閑連接 max-idle: 9 #最小空閑連接 min-idle: 0 timeout: 500000 redis2: host: 127.0.0.1 port: 6385 lettuce: pool: max-active: 20 max-idle: 8 max-wait: -1 min-idle: 0 timeout: 500000
(這里的redis都沒有配置密碼)
package com.cq.config; import cn.hutool.core.convert.Convert; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; 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.RedisClusterConfiguration; import org.springframework.data.redis.connection.RedisConnectionFactory; 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.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.io.Serializable; import java.util.HashMap; import java.util.Map; /** * @author cccccloud on 2020/11/16 17:16 */ @Configuration public class RedisConfig { @Autowired private Environment environment; @Value("${spring.redis2.host}") private String host; @Value("${spring.redis2.port}") private String port; @Value("${spring.redis2.lettuce.pool.max-active}") private String max_active; @Value("${spring.redis2.lettuce.pool.max-idle}") private String max_idle; @Value("${spring.redis2.lettuce.pool.max-wait}") private String max_wait; @Value("${spring.redis2.lettuce.pool.min-idle}") private String min_idle; /** * 配置lettuce連接池 * * @return */ @Bean @Primary @ConfigurationProperties(prefix = "spring.redis.cluster.lettuce.pool") public GenericObjectPoolConfig redisPool() { return new GenericObjectPoolConfig(); } /** * 配置第一個數(shù)據(jù)源的 * * @return */ @Bean("redisClusterConfig") @Primary public RedisClusterConfiguration redisClusterConfig() { Map<String, Object> source = new HashMap<>(8); source.put("spring.redis.cluster.nodes", environment.getProperty("spring.redis.cluster.nodes")); RedisClusterConfiguration redisClusterConfiguration; redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source)); redisClusterConfiguration.setPassword(environment.getProperty("spring.redis.password")); return redisClusterConfiguration; } /** * 配置第一個數(shù)據(jù)源的連接工廠 * 這里注意:需要添加@Primary 指定bean的名稱,目的是為了創(chuàng)建兩個不同名稱的LettuceConnectionFactory * * @param redisPool * @param redisClusterConfig * @return */ @Bean("lettuceConnectionFactory") @Primary public LettuceConnectionFactory lettuceConnectionFactory(GenericObjectPoolConfig redisPool, @Qualifier("redisClusterConfig") RedisClusterConfiguration redisClusterConfig) { LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(redisPool).build(); return new LettuceConnectionFactory(redisClusterConfig, clientConfiguration); } /** * 配置第一個數(shù)據(jù)源的RedisTemplate * 注意:這里指定使用名稱=factory 的 RedisConnectionFactory * 并且標(biāo)識第一個數(shù)據(jù)源是默認(rèn)數(shù)據(jù)源 @Primary * * @param redisConnectionFactory * @return */ @Bean("redisTemplate") @Primary public RedisTemplate redisTemplate(@Qualifier("lettuceConnectionFactory") RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key采用String的序列化方式 template.setKeySerializer(stringRedisSerializer); // hash的key也采用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); // value序列化方式采用jackson template.setValueSerializer(stringRedisSerializer); // hash的value序列化方式采用jackson template.setHashValueSerializer(stringRedisSerializer); template.afterPropertiesSet(); return template; } @Bean public GenericObjectPoolConfig redisPool2() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMinIdle(Convert.toInt(min_idle)); config.setMaxIdle(Convert.toInt(max_idle)); config.setMaxTotal(Convert.toInt(max_active)); config.setMaxWaitMillis(Convert.toInt(max_wait)); return config; } @Bean public RedisStandaloneConfiguration redisConfig2() { RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration(host,Convert.toInt(port)); return redisConfig; } @Bean("factory2") public LettuceConnectionFactory factory2(@Qualifier("redisPool2") GenericObjectPoolConfig config, @Qualifier("redisConfig2") RedisStandaloneConfiguration redisConfig) {//注意傳入的對象名和類型RedisStandaloneConfiguration LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build(); return new LettuceConnectionFactory(redisConfig, clientConfiguration); } /** * 單實(shí)例redis數(shù)據(jù)源 * * @param connectionFactory * @return */ @Bean("redisTemplateSingle") public RedisTemplate<String, Object> redisTemplateSingle(@Qualifier("factory2")LettuceConnectionFactory connectionFactory) {//注意傳入的對象名 RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(redisSerializer); redisTemplate.setValueSerializer(redisSerializer); redisTemplate.setHashKeySerializer(redisSerializer); redisTemplate.setHashValueSerializer(redisSerializer); return redisTemplate; } }
使用單實(shí)例redis
/** * redis 單節(jié)點(diǎn) */ @Resource(name = "redisTemplateSingle") private RedisTemplate redisTemplateSingle;
使用redis集群
/** * redis 集群 */ @Resource(name = "redisTemplate") private RedisTemplate redisTemplate;
關(guān)于“springboot redis如何使用lettuce配置多數(shù)據(jù)源”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。