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><artifactId>jedis</artifactId><!--注意:只有2.x版本才支持,3.x版本不兼容报错NoClassDefFoundError:redis/clients/util/SafeEncoder --><version>2.9.3</version>
</dependency>

application.xml 

spring:application:name: double-redis-jedisprofiles:active: devjackson:date-format: yyyy-MM-dd HH:mm:sstime-zone: GMT+8server:port: 8008sys:redis:one:    host: 192.168.1.2port: 5009timeout: 6000jedis:pool:max-active: 30max-idle: 10max-wait: 6000slave:host: 192.168.1.3port: 5010database: 1two:timeout: 12000jedis: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

Jedis配置基类

public abstract class AbstractJedisRedisConfig {public JedisConnectionFactory createJedisConnectionFactory(RedisProperties properties) {JedisClientConfiguration clientConfiguration = getJedisClientConfiguration(properties);RedisSentinelConfiguration sentinelConf = getSentinelConfig(properties);if (sentinelConf != null) {return new JedisConnectionFactory(sentinelConf, clientConfiguration);}RedisClusterConfiguration clusterConf = getClusterConfiguration(properties);if (clusterConf != null) {return new JedisConnectionFactory(clusterConf, clientConfiguration);}return new JedisConnectionFactory(getStandaloneConfig(properties), clientConfiguration);}private JedisClientConfiguration getJedisClientConfiguration(RedisProperties properties) {JedisClientConfiguration.JedisClientConfigurationBuilder builder = applyProperties(JedisClientConfiguration.builder(), properties);RedisProperties.Pool pool = properties.getJedis().getPool();if (pool != null) {applyPooling(pool, builder);}if (StringUtils.hasText(properties.getUrl())) {customizeConfigurationFromUrl(builder, properties);}return builder.build();}private JedisClientConfiguration.JedisClientConfigurationBuilder applyProperties(JedisClientConfiguration.JedisClientConfigurationBuilder builder,RedisProperties properties) {if (properties.isSsl()) {builder.useSsl();}if (properties.getTimeout() != null) {Duration timeout = properties.getTimeout();builder.readTimeout(timeout).connectTimeout(timeout);}return builder;}private void applyPooling(RedisProperties.Pool pool,JedisClientConfiguration.JedisClientConfigurationBuilder builder) {builder.usePooling().poolConfig(jedisPoolConfig(pool));}private JedisPoolConfig jedisPoolConfig(RedisProperties.Pool pool) {JedisPoolConfig config = new JedisPoolConfig();config.setMaxTotal(pool.getMaxActive());config.setMaxIdle(pool.getMaxIdle());config.setMinIdle(pool.getMinIdle());if (pool.getTimeBetweenEvictionRuns() != null) {config.setTimeBetweenEvictionRunsMillis(pool.getTimeBetweenEvictionRuns().toMillis());}if (pool.getMaxWait() != null) {config.setMaxWaitMillis(pool.getMaxWait().toMillis());}return config;}private void customizeConfigurationFromUrl(JedisClientConfiguration.JedisClientConfigurationBuilder builder, RedisProperties properties) {ConnectionInfo connectionInfo = parseUrl(properties.getUrl());if (connectionInfo.isUseSsl()) {builder.useSsl();}}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 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 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 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;}}
}

(1)第一个Redis配置

public class OneJedisRedisConfig  extends AbstractJedisRedisConfig{@Beanpublic StringRedisTemplate oneStringRedisTemplate(@Autowired @Qualifier("oneRedisConnectionFactory")RedisConnectionFactory redisConnectionFactory){StringRedisTemplate template = new StringRedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template;}@Beanpublic JedisConnectionFactory oneRedisConnectionFactory(@Autowired @Qualifier("oneRedisProperties") RedisProperties properties){return createJedisConnectionFactory(properties);}@Bean@ConfigurationProperties("sys.redis.one")public RedisProperties oneRedisProperties() {return new RedisProperties();}
}

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

public class TwoJedisRedisConfig extends AbstractJedisRedisConfig{@Primary@Beanpublic StringRedisTemplate twoStringRedisTemplate(@Autowired @Qualifier("twoRedisConnectionFactory")RedisConnectionFactory redisConnectionFactory) {StringRedisTemplate template = new StringRedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template;}@Primary@Beanpublic JedisConnectionFactory twoRedisConnectionFactory(@Autowired @Qualifier("twoRedisProperties") RedisProperties properties){return createJedisConnectionFactory(properties);}@Primary@Bean@ConfigurationProperties("sys.redis.two")public RedisProperties twoRedisProperties() {return new RedisProperties();}
}

(3)redis导入配置

@Configuration
@Import({OneRedisConfig.class, TwoRedisConfig.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/322816.shtml

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

相关文章

.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…

居中对齐

自绝父相 现在有一个父盒子和一个子盒子 要想让子盒子相对父盒子居中很简单&#xff0c;在子盒子上加上属性即可 margin: 0 auto;如果说要变成水平垂直居中呢&#xff0c;加上两个auto?结果肯定是不行的 得这样 用到所谓得子绝父相 首先父亲要相对定位 然后儿子设置成这样 l…

P3128-最大流Max Flow【树上差分,LCA】

正题 题目大意 一棵树 若干条路径&#xff0c;哪个点经过的路径最多&#xff0c;求路径条数。 解题思路 对于每条路径计算一次LCALCALCA&#xff0c;然后树上差分就好了。 codecodecode #include<cstdio> #include<queue> #include<cmath> using namespac…

IdentityServer4(10)- 添加对外部认证的支持之QQ登录

前言 前面我们提到过IdentityServer4是可以添加外部认证的&#xff0c;如果外部认证支持OAuth2&#xff0c;那么添加到IdentityServer4是非常简单的&#xff0c;在ASP.NET Core下提供了非常多的外部认证实现&#xff0c;比如Google&#xff0c;Facebook&#xff0c;Twitter&…

css字体样式

以下写几个常用的字体样式&#xff0c;方便以后使用&#xff1a; font-family: FangSong;font-weight: bold;font-size: 20px;font-style: italic;text-decoration: underline;letter-spacing: 1em;line-height: 2em;简写&#xff1a;font:bold 20px FangSong样式作用font-fami…

springboot 多数据源mybatis的两种整合方法

转载自 springboot-mybatis多数据源的两种整合方法 简介&#xff1a; 随着并发量的不断增加&#xff0c;显然单个数据库已经承受不了高并发带来的压力。一个项目使用多个数据库&#xff08;无论是主从复制- - 读写分离还是分布式数据库结构&#xff09;的重要性变得越来越明显…

P1373-小a和uim之大逃离【dp】

正题 题目大意 n∗mn*mn∗m的矩阵有不同的权值&#xff0c;每次只可以往下走或往右走。要求走奇数次&#xff0c;要求偶数次经过的点和奇数次经过的点同余KKK 解题思路 设fi,j,k,0/1f_{i,j,k,0/1}fi,j,k,0/1​表示在第iii行jjj列&#xff0c;两个数只差为kkk&#xff0c;是奇数…

项目实战+感慨

已经好久没写博客了&#xff0c;自从接触了项目以后&#xff0c;发现很难再挤出时间来写博客&#xff0c;大部分时间都是在关注项目的进展&#xff0c;以及查阅相关的资料。我想这也是很多程序员入职以后就很少写博客的原因。 前言 最近一段时间我接触了很多前端的东西&#x…

解决mybatis generator无法覆盖XML

转载自 解决mybatis generator无法覆盖XML 今天发现mybatis generator maven plugin在重复生成的时候xml文件只会merge&#xff0c;不会覆盖。 明明在pom.xml中配置了如下&#xff1a; <configuration><configurationFile>src/main/resources/mybatis/generato…

laravel构造器的CURD

这里主要记一下关于用laravel写相关接口的一些知识点&#xff0c;方便以后查阅&#xff0c;基本都是Controller里面的一些操作 查询数据&#xff1a; // 查询数据$query DB::table(think_data)->where([ [status,>,500]])->get();插入数据&#xff1a; // 新增单条数…

亿些模板【图论】

文章目录前言图论模板最短路-Floyd最短路-SPFA最短路-Dij堆优化最小生成树-Kruskal最小生成树-Prim堆优化最大匹配-匈牙利算法tarjan求割点tarjan缩点LCA-树上倍增LCA-tarjan并查集优化网络流-最大流dinic网络流-最小费用最大流负环虚树2-SATKruskal重构树静态仙人掌前言 因为…

中间件中渲染Razor视图

前言 上一篇文章《ASP.NET Core 奇技淫巧&#xff08;1&#xff09;&#xff1a;中间件实现服务端静态化缓存》中介绍了中间件的使用方法、以及使用中间件实现服务端静态化缓存的功能。本系列文章取名“奇技淫巧”不是没道理的&#xff0c;因为这写技巧都是我最近在做的公司实际…