- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- springboot redis使用lettuce配置多數據源的實(shí)現
目前項目上需要連接兩個(gè)redis數據源,一個(gè)redis數據源是單機模式,一個(gè)redis數據源是分片集群模式,這里將具體配置列一下。
項目用的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: #連接池最大連接數 max-active: 20 #連接池最大等待時(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都沒(méi)有配置密碼)
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(); } /** * 配置第一個(gè)數據源的 * * @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; } /** * 配置第一個(gè)數據源的連接工廠(chǎng) * 這里注意:需要添加@Primary 指定bean的名稱(chēng),目的是為了創(chuàng )建兩個(gè)不同名稱(chē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); } /** * 配置第一個(gè)數據源的RedisTemplate * 注意:這里指定使用名稱(chēng)=factory 的 RedisConnectionFactory * 并且標識第一個(gè)數據源是默認數據源 @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) {//注意傳入的對象名和類(lèi)型RedisStandaloneConfiguration LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build(); return new LettuceConnectionFactory(redisConfig, clientConfiguration); } /** * 單實(shí)例redis數據源 * * @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 單節點(diǎn) */ @Resource(name = "redisTemplateSingle") private RedisTemplate redisTemplateSingle;
使用redis集群
/** * redis 集群 */ @Resource(name = "redisTemplate") private RedisTemplate redisTemplate;
到此這篇關(guān)于springboot redis使用lettuce配置多數據源的實(shí)現的文章就介紹到這了,更多相關(guān)springboot lettuce多數據源內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站