电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

springboot redis使用lettuce配置多數據源的實現

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

目前項目上需要連接兩個redis數據源,一個redis數據源是單機模式,一個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>一、在yml中配置redis數據源信息

redis: cluster: nodes: 127.0.0.1:9001 lettuce: #連接池配置 pool:#連接池最大連接數max-active: 20#連接池最大等待時間,負數表示不做限制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(); } /** * 配置第一個數據源的 * * @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; } /** * 配置第一個數據源的連接工廠 * 這里注意:需要添加@Primary 指定bean的名稱,目的是為了創建兩個不同名稱的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); } /** * 配置第一個數據源的RedisTemplate * 注意:這里指定使用名稱=factory 的 RedisConnectionFactory * 并且標識第一個數據源是默認數據源 @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) {//注意傳入的對象名和類型RedisStandaloneConfigurationLettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();return new LettuceConnectionFactory(redisConfig, clientConfiguration); } /** * 單實例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; }}三、使用redis

使用單實例redis

/** * redis 單節點 */ @Resource(name = 'redisTemplateSingle') private RedisTemplate redisTemplateSingle;

使用redis集群

/** * redis 集群 */ @Resource(name = 'redisTemplate') private RedisTemplate redisTemplate;

到此這篇關于springboot redis使用lettuce配置多數據源的實現的文章就介紹到這了,更多相關springboot lettuce多數據源內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 工业设计,人工智能,体验式3D展示的智能技术交流服务平台-纳金网 J.S.Bach 圣巴赫_高端背景音乐系统_官网 | 【德信自动化】点胶机_全自动点胶机_自动点胶机厂家_塑料热压机_自动螺丝机-深圳市德信自动化设备有限公司 | 柔性测斜仪_滑动测斜仪-广州杰芯科技有限公司 | 气动机械手-搬运机械手-气动助力机械手-山东精瑞自动化设备有限公司 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 茅茅虫AI论文写作助手-免费AIGC论文查重_写毕业论文降重 | 有声小说,听书,听小说资源库-听世界网 | 运动木地板价格,篮球馆体育运动木地板生产厂家_欧氏地板 | 广州展览设计公司_展台设计搭建_展位设计装修公司-众派展览装饰 广州展览制作工厂—[优简]直营展台制作工厂_展会搭建资质齐全 | 整车VOC采样环境舱-甲醛VOC预处理舱-多舱法VOC检测环境仓-上海科绿特科技仪器有限公司 | 防爆电机-高压防爆电机-ybx4电动机厂家-河南省南洋防爆电机有限公司 | 针焰试验仪,灼热丝试验仪,漏电起痕试验仪,水平垂直燃烧试验仪 - 苏州亚诺天下仪器有限公司 | 广西绿桂涂料--承接隔热涂料、隔音涂料、真石漆、多彩仿石漆等涂料工程双包施工 | 棉服定制/厂家/公司_棉袄订做/价格/费用-北京圣达信棉服 | 福州甲醛检测-福建室内空气检测_环境检测_水质检测-福建中凯检测技术有限公司 | 讲师宝经纪-专业培训机构师资供应商_培训机构找讲师、培训师、讲师经纪就上讲师宝经纪 | 烟雾净化器-滤筒除尘器-防爆除尘器-除尘器厂家-东莞执信环保科技有限公司 | 珠海白蚁防治_珠海灭鼠_珠海杀虫灭鼠_珠海灭蟑螂_珠海酒店消杀_珠海工厂杀虫灭鼠_立净虫控防治服务有限公司 | 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 物联网卡_物联网卡购买平台_移动物联网卡办理_移动联通电信流量卡通信模组采购平台? | 多米诺-多米诺世界纪录团队-多米诺世界-多米诺团队培训-多米诺公关活动-多米诺创意广告-多米诺大型表演-多米诺专业赛事 | 光谱仪_积分球_分布光度计_灯具检测生产厂家_杭州松朗光电【官网】 | 锌合金压铸-铝合金压铸厂-压铸模具-冷挤压-誉格精密压铸 | 智能化的检漏仪_气密性测试仪_流量测试仪_流阻阻力测试仪_呼吸管快速检漏仪_连接器防水测试仪_车载镜头测试仪_奥图自动化科技 | 保定市泰宏机械制造厂-河北铸件厂-铸造厂-铸件加工-河北大件加工 | 海德莱电力(HYDELEY)-无功补偿元器件生产厂家-二十年专业从事电力电容器 | 贝朗斯动力商城(BRCPOWER.COM) - 买叉车蓄电池上贝朗斯商城,价格更超值,品质有保障! | 长春网站建设,五合一网站设计制作,免费优化推广-长春网站建设 | 国产离子色谱仪,红外分光测油仪,自动烟尘烟气测试仪-青岛埃仑通用科技有限公司 | 合肥风管加工厂-安徽螺旋/不锈钢风管-通风管道加工厂家-安徽风之范 | 长沙广告公司_制作,长沙喷绘_发光字_招牌制作_长沙泓润广告官网 长城人品牌官网 | 炭黑吸油计_测试仪,单颗粒子硬度仪_ASTM标准炭黑自销-上海贺纳斯仪器仪表有限公司(HITEC中国办事处) | 电动车头盔厂家_赠品头盔_安全帽批发_山东摩托车头盔—临沂承福头盔 | 东亚液氮罐-液氮生物容器-乐山市东亚机电工贸有限公司 | 山东活动策划|济南活动公司|济南公关活动策划-济南锐嘉广告有限公司 | 交流伺服电机|直流伺服|伺服驱动器|伺服电机-深圳市华科星电气有限公司 | 耐酸碱胶管_耐腐蚀软管总成_化学品输送软管_漯河利通液压科技耐油耐磨喷砂软管|耐腐蚀化学软管 | 固诺家居-全屋定制十大品牌_整体衣柜木门橱柜招商加盟 | 空心明胶胶囊|植物胶囊|清真胶囊|浙江绿键胶囊有限公司欢迎您! | 美的商用净水器_美的直饮机_一级代理经销商_Midea租赁价格-厂家反渗透滤芯-直饮水批发品牌售后 | 模切之家-专注服务模切行业的B2B平台! |