亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

springboot redis使用lettuce配置多數(shù)據(jù)源的實(shí)現(xiàn)

瀏覽:71日期:2023-03-14 10:22:46

目前項(xiàng)目上需要連接兩個(gè)redis數(shù)據(jù)源,一個(gè)redis數(shù)據(jù)源是單機(jī)模式,一個(gè)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>一、在yml中配置redis數(shù)據(jù)源信息

redis: cluster: nodes: 127.0.0.1:9001 lettuce: #連接池配置 pool:#連接池最大連接數(shù)max-active: 20#連接池最大等待時(shí)間,負(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: 20max-idle: 8max-wait: -1min-idle: 0 timeout: 500000

(這里的redis都沒有配置密碼)

二、添加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 */@Configurationpublic 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è)數(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; } /** * 配置第一個(gè)數(shù)據(jù)源的連接工廠 * 這里注意:需要添加@Primary 指定bean的名稱,目的是為了創(chuàng)建兩個(gè)不同名稱的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è)數(shù)據(jù)源的RedisTemplate * 注意:這里指定使用名稱=factory 的 RedisConnectionFactory * 并且標(biāo)識(shí)第一個(gè)數(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序列化方式采用jacksontemplate.setValueSerializer(stringRedisSerializer);// hash的value序列化方式采用jacksontemplate.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) {//注意傳入的對(duì)象名和類型RedisStandaloneConfigurationLettuceClientConfiguration 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) {//注意傳入的對(duì)象名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; }}三、使用redis

使用單實(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ù)源的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot lettuce多數(shù)據(jù)源內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 黄网站在线观看永久免费 | 中文三 级 黄 色 片 | 中国一级特黄aa毛片大片 | 曰本一级毛片免费播放 | 免费看的毛片 | 五月婷婷六月丁香综合 | 欧美精品亚洲精品日韩专区 | 成人国产永久福利看片 | 久久久久久国产精品免费 | 一级特黄aaa大片在线观看视频 | 奇米狠狠 | 国产综合欧美 | 国产日产高清欧美一区二区三区 | 黑人巨大vs北条麻妃在线 | 久久最新免费视频 | 久久99国产亚洲精品观看 | 亚洲乱码国产乱码精品精98 | 中文字幕2022永久在线 | 狠狠色丁香婷婷久久综合2021 | 久久精品成人免费网站 | 免费国产人做人视频在线观看 | 日韩一区国产二区欧美三区 | 亚洲欧美黄 | 欧美成人xx禁片在线观看 | 在线观看一区二区三区四区 | 国产精品 主播精选 网红 | 三级福利片 | 欧美日韩中文字幕在线视频 | 一级免费黄色录像 | 精品国产成人综合久久小说 | 国产欧美一区二区三区视频在线观看 | 日韩一区二区视频在线观看 | 亚洲国产精品a一区二区三区 | 国产成人综合亚洲欧美在线n | 欧美在线免费观看视频 | 亚洲一页 | 成人国产在线视频 | 免费鲁丝片一级观看 | 9191国语精品高清在线最新 | 色综合色综合色综合 | 国产黄色二级片 |