SpringBoot2.1.9 多Redis Lettuce配置

一、配置文件

pom.xml

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

application.yml

spring:application:name: double-redis-lettuceprofiles:active: devjackson:date-format: yyyy-MM-dd HH:mm:sstime-zone: GMT+8server:port: 8008sys:redis:one:    host: 192.168.1.2port: 5009timeout: 6000lettuce:pool:max-active: 30max-idle: 10max-wait: 6000slave:host: 192.168.1.3port: 5010database: 1two:timeout: 12000lettuce:pool:max-active: 30max-idle: 10max-wait: 6000sentinel:master: redis_sentinel_masternodes:- sentinel-group001.mk.com:10003- sentinel-group002.mk.com:10003- sentinel-group003.mk.com:10003

二、配置Configuration Bean

Lettuce配置基类

public class AbstractLettuceRedisConfig {public LettuceConnectionFactory createLettuceConnectionFactory(ClientResources clientResources, RedisProperties properties) {LettuceClientConfiguration clientConfiguration = getLettuceClientConfiguration(clientResources, properties);RedisSentinelConfiguration sentinelConf = getSentinelConfig(properties);if (sentinelConf != null) {return new LettuceConnectionFactory(sentinelConf, clientConfiguration);}RedisClusterConfiguration clusterConf = getClusterConfiguration(properties);if (clusterConf != null) {return new LettuceConnectionFactory(clusterConf, clientConfiguration);}return new LettuceConnectionFactory(getStandaloneConfig(properties), clientConfiguration);}private RedisStandaloneConfiguration getStandaloneConfig(RedisProperties properties) {RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();if (StringUtils.hasText(properties.getUrl())) {ConnectionInfo connectionInfo = parseUrl(properties.getUrl());config.setHostName(connectionInfo.getHostName());config.setPort(connectionInfo.getPort());config.setPassword(RedisPassword.of(connectionInfo.getPassword()));} else {config.setHostName(properties.getHost());config.setPort(properties.getPort());config.setPassword(RedisPassword.of(properties.getPassword()));}config.setDatabase(properties.getDatabase());return config;}private RedisClusterConfiguration getClusterConfiguration(RedisProperties properties) {if (properties.getCluster() == null) {return null;}RedisProperties.Cluster clusterProperties = properties.getCluster();RedisClusterConfiguration config = new RedisClusterConfiguration(clusterProperties.getNodes());if (clusterProperties.getMaxRedirects() != null) {config.setMaxRedirects(clusterProperties.getMaxRedirects());}if (properties.getPassword() != null) {config.setPassword(RedisPassword.of(properties.getPassword()));}return config;}private RedisSentinelConfiguration getSentinelConfig(RedisProperties properties) {RedisProperties.Sentinel sentinelProperties = properties.getSentinel();if (sentinelProperties != null) {RedisSentinelConfiguration config = new RedisSentinelConfiguration();config.master(sentinelProperties.getMaster());config.setSentinels(createSentinels(sentinelProperties));if (properties.getPassword() != null) {config.setPassword(RedisPassword.of(properties.getPassword()));}config.setDatabase(properties.getDatabase());return config;}return null;}private List<RedisNode> createSentinels(RedisProperties.Sentinel sentinel) {List<RedisNode> nodes = new ArrayList<>();for (String node : sentinel.getNodes()) {try {String[] parts = StringUtils.split(node, ":");Assert.state(parts.length == 2, "Must be defined as 'host:port'");nodes.add(new RedisNode(parts[0], Integer.valueOf(parts[1])));} catch (RuntimeException ex) {throw new IllegalStateException("Invalid redis sentinel " + "property '" + node + "'", ex);}}return nodes;}private LettuceClientConfiguration getLettuceClientConfiguration(ClientResources clientResources, RedisProperties properties) {LettuceClientConfiguration.LettuceClientConfigurationBuilder builder = createBuilder(Optional.ofNullable(properties.getLettuce()).map(v -> v.getPool()).orElse(null));applyProperties(builder, properties);if (StringUtils.hasText(properties.getUrl())) {customizeConfigurationFromUrl(builder, properties);}builder.clientResources(clientResources);return builder.build();}private LettuceClientConfiguration.LettuceClientConfigurationBuilder createBuilder(RedisProperties.Pool pool) {if (pool == null) {return LettuceClientConfiguration.builder();}return new PoolBuilderFactory().createBuilder(pool);}private LettuceClientConfiguration.LettuceClientConfigurationBuilder applyProperties(LettuceClientConfiguration.LettuceClientConfigurationBuilder builder, RedisProperties properties) {if (properties.isSsl()) {builder.useSsl();}if (properties.getTimeout() != null) {builder.commandTimeout(properties.getTimeout());}if (properties.getLettuce() != null) {RedisProperties.Lettuce lettuce = properties.getLettuce();if (lettuce.getShutdownTimeout() != null && !lettuce.getShutdownTimeout().isZero()) {builder.shutdownTimeout(properties.getLettuce().getShutdownTimeout());}}return builder;}private void customizeConfigurationFromUrl(LettuceClientConfiguration.LettuceClientConfigurationBuilder builder, RedisProperties properties) {ConnectionInfo connectionInfo = parseUrl(properties.getUrl());if (connectionInfo.isUseSsl()) {builder.useSsl();}}private ConnectionInfo parseUrl(String url) {try {URI uri = new URI(url);boolean useSsl = (url.startsWith("rediss://"));String password = null;if (uri.getUserInfo() != null) {password = uri.getUserInfo();int index = password.indexOf(':');if (index >= 0) {password = password.substring(index + 1);}}return new ConnectionInfo(uri, useSsl, password);} catch (URISyntaxException ex) {throw new IllegalArgumentException("Malformed url '" + url + "'", ex);}}private static class ConnectionInfo {private final URI uri;private final boolean useSsl;private final String password;public ConnectionInfo(URI uri, boolean useSsl, String password) {this.uri = uri;this.useSsl = useSsl;this.password = password;}public boolean isUseSsl() {return this.useSsl;}public String getHostName() {return this.uri.getHost();}public int getPort() {return this.uri.getPort();}public String getPassword() {return this.password;}}private static class PoolBuilderFactory {public LettuceClientConfiguration.LettuceClientConfigurationBuilder createBuilder(RedisProperties.Pool properties) {return LettucePoolingClientConfiguration.builder().poolConfig(getPoolConfig(properties));}private GenericObjectPoolConfig<?> getPoolConfig(RedisProperties.Pool properties) {GenericObjectPoolConfig<?> config = new GenericObjectPoolConfig<>();config.setMaxTotal(properties.getMaxActive());config.setMaxIdle(properties.getMaxIdle());config.setMinIdle(properties.getMinIdle());if (properties.getTimeBetweenEvictionRuns() != null) {config.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRuns().toMillis());}if (properties.getMaxWait() != null) {config.setMaxWaitMillis(properties.getMaxWait().toMillis());}return config;}}}

(1)第一个Redis配置


public class OneLettuceRedisConfig  extends AbstractLettuceRedisConfig{@Beanpublic StringRedisTemplate oneStringRedisTemplate(@Autowired @Qualifier("oneRedisConnectionFactory")
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {StringRedisTemplate template = new StringRedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template;}@Beanpublic LettuceConnectionFactory oneRedisConnectionFactory(@Autowired @Qualifier("oneRedisProperties") RedisProperties properties,ClientResources clientResources) throws UnknownHostException {return createLettuceConnectionFactory(clientResources, properties);}@Bean@ConfigurationProperties("sys.redis.one")public RedisProperties oneRedisProperties() {return new RedisProperties();}
}

(2)第二个Redis配置(主)

public class TwoLettuceRedisConfig extends AbstractLettuceRedisConfig{@Primary@Beanpublic StringRedisTemplate twoStringRedisTemplate(@Autowired @Qualifier("twoRedisConnectionFactory")
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {StringRedisTemplate template = new StringRedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template;}@Primary@Beanpublic LettuceConnectionFactory twoRedisConnectionFactory(@Autowired @Qualifier("twoRedisProperties") RedisProperties properties,ClientResources clientResources) throws UnknownHostException {return createLettuceConnectionFactory(clientResources, properties);}@Primary@Bean@ConfigurationProperties("sys.redis.two")public RedisProperties twoRedisProperties() {return new RedisProperties();}
}

(3)redis导入配置

@Configuration
@Import({OneLettuceRedisConfig.class, TwoLettuceRedisConfig.class})
public class RedisConfig {}

注意:去掉默认自动配置

@SpringBootApplication(exclude = {RedisAutoConfiguration.class})
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ASP.NET Core缓存静态资源

背景 缓存样式表&#xff0c;JavaScript或图像文件等静态资源可以提高您网站的性能。在客户端&#xff0c;总是从缓存中加载一个静态文件&#xff0c;这样可以减少对服务器的请求数量&#xff0c;从而减少获取页面及其资源的时间。在服务器端&#xff0c;由于它们的请求较少&am…

P3830-[SHOI2012]随机树【数学期望,dp】

正题 题目大意 开始的时候一个点&#xff0c;然后等概率随机选一个叶子节点展开成两个&#xff0c;求nnn个叶子节点的树的叶子节点平均深度和最大深度期望。 解题思路 平均深度很好求&#xff0c;因为每展开一个&#xff0c;叶子节点总深度就加上个2。 所以gigi−12ig_{i}g_…

【程序员】保持一颗虚心好学的心态去敲代码

最近&#xff0c;我感觉是自己突破最大的一段时间&#xff0c;为什么呢&#xff1f;主要是打通了接口这一块&#xff0c;就是用postman发送各种数据&#xff0c;我都能用后端接受到相关数据&#xff0c;并且解析出来。 在这之前我尝试过spring boot 和node&#xff0c;前者是太…

SpringBoot @Resource注入的坑

一、场景复现 双kafka消费者配置 &#xff08;1&#xff09;第一个kafka public class OneKafkaConfig {Beanpublic KafkaListenerContainerFactory oneKafkaFactory(Resource ConsumerFactory oneConsumerFactory) {ConcurrentKafkaListenerContainerFactory factory new …

跟着老桂学ASP.NET Core 2.0

.net core作为微软开发技术中跨平台的利器&#xff0c;2.0的发布已经有一段时间了&#xff0c;asp.net core是新一代微软的BS开发框架&#xff0c;同时兼容.net core和.net framework&#xff0c;它的出现&#xff0c;使基于微软体系的BS开发迎来新的契机&#xff0c;开源&…

jzoj4252-QYQ的图【dfs】

正题 题目大意 有nnn个点mmm条边&#xff0c;若一个点没选&#xff0c;那么连接到的点都得选择。求最小选择权值。 解题思路 对于每个联通块暴力dfsdfsdfs&#xff0c;若一个点不选那么就把别的点都选上。 因为每个点至少限制一个点&#xff0c;那么时间复杂度最坏为O(2n2)O…

如何安装并启动django

这里我用的是pip3&#xff0c;一般没装两个版本的用pip就行了 安装 pip3 install django如何检测 python3 -m django --version显示版本号即可 如何创建并启动项目 创建 django-admin startproject HelloWorld启动 然后cd到HelloWorld目录里 python manage.py runserver…

Hadoop生态hive(四)数据类型

转载自 Hive数据类型 一、列类型 列类型被用作Hive的列数据类型。它们如下&#xff1a; &#xff08;1&#xff09;整型 整型数据可以指定使用整型数据类型&#xff0c;INT。当数据范围超过INT的范围&#xff0c;需要使用BIGINT&#xff0c;如果数据范围比INT小&#xff0…

ASP.NET Core 认证与授权[6]:授权策略是怎么执行的?

在上一章中ASP.NET Core 认证与授权[5]:初识授权&#xff0c;详细介绍了 ASP.NET Core 中的授权策略&#xff0c;在需要授权时&#xff0c;只需要在对应的Controler或者Action上面打上[Authorize]特性&#xff0c;并指定要执行的策略名称即可&#xff0c;但是&#xff0c;授权策…

jzoj4248-n染色【数学,快速幂】

正题 题目大意 一个环&#xff0c;nnn个部分mmm个颜色求每段连续的都不同颜色的方案数。 解题思路 打表找一下规律发现 fifi−1∗(m−1)fi−2∗(m−1)f_if_{i-1}*(m-1)f_{i-2}*(m-1)fi​fi−1​∗(m−1)fi−2​∗(m−1) 然后特征根:fi(m−1)n(m−1)∗(−1)nf_i(m-1)^n(m-1)*…

SpringBoot2.1.9 多Redis Jedis配置

一、配置文件 pom.xml <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency><groupId>redis.clients</groupId><artifa…

.net core2.0下使用Identity改用dapper存储数据

前言、 已经好多天没写博客了&#xff0c;鉴于空闲无聊之时又兴起想写写博客&#xff0c;也当是给自己做个笔记。过了这么些天&#xff0c;我的文笔还是依然那么烂就请多多谅解了。今天主要是分享一下在使用.net core2.0下的实际遇到的情况。在使用webapi时用了identity做用户验…

如何解决vuepress部署出现样式问题

以前在安装hexo的时候出了样式问题&#xff0c;现在用vuepress也出现了相同的问题。 本地测试完全可以 然而打包之后就彻底乱了 即使是自己本地打包成dist之后也会出现相同的问题 有点困扰&#xff0c;应该是打包配置的问题 通过修改index.html里的内容。将/改为./ 发现部…

jzoj4249-游戏【贪心】

正题 题目大意 0∼n0\sim n0∼n的点&#xff0c;从iii移动到jjj获得aj∗(j−i)a_j*(j-i)aj​∗(j−i)的价值。求最大价值。 解题思路 考虑贪心&#xff0c;每次移动到往后aia_iai​最大的点。 证明: 反证明:我们假设有一种情况i<ji<ji<j且aj<aia_j<a_iaj​<…

想使用Docker容器?先看看这些注意事项

Docker容器无疑是最近十年来最引人注目的技术之一&#xff0c;因为有了它&#xff0c;对我们思考设计、开发和运维软件的方式产生了非常有益的影响。 但是就像每一个开发工具一样&#xff0c;为了充分利用这些工具&#xff0c;需要注意一些使用中问题&#xff0c;Docker容器也是…

SpringBoot2.1.9 多数据源JDBC配置

一、配置文件 pom.xm <dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.1.10.RELEASE</version> </dependency> <dependency><groupId>mysql</groupId>…

Vs Code如何自定义设置一键代码补全

有时候是不是看到别人输入一个vue就能打印整个代码&#xff0c;感觉很神奇&#xff0c;本文就以vue为案例教你如何使用这种骚操作&#xff01;&#xff01;&#xff01; 点击文件->首选项->用户代码片段 输入vue.json&#xff08;如果没有则新建代码片段&#xff09; &…

欢乐纪中某B组赛【2019.1.30】The penultimate day

前言 TodayisthepenultimatedayToday\ is\ the penultimate dayToday is thepenultimateday 成绩 RankRankRank是有算别人的 RankRankRankPersonPersonPersonScoreScoreScoreAAABBBCCC3332017zyc2017zyc2017zyc2502502509090901001001006060601010102017xxy2017xxy2017xxy22022…

Realm发布Realm .NET,扩展支持.NET技术栈

继去年夏天Realm引入对.NET Core的支持&#xff0c;使开发者可以用C#来构建移动应用之后&#xff0c;Realm发布了Realm .NET。Realm .NET是一个可以让开发者更好地集成他们的.NET技术栈的新组件集。 其中一个组件&#xff0c;是针对.NET的Global Notifier&#xff0c;可以实现大…

SpringBoot2.1.9 多数据源Mybatis—JDBC配置

一、配置文件 pom.xm <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency><groupId>mysql</groupId><artifactId>mysql-c…