久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

SpringBoot2整合Redis多數據源步驟詳解

瀏覽:5日期:2023-05-27 09:06:40

redis是一個基于內存的高性能key-value數據庫,具有極高的讀寫速度。本文介紹 SpringBoot 和 Redis 的整合,以及如何在項目中具體應用

配置文件屬性

spring: redis: database: 1 host: 192.168.50.144 port: 6379 password: timeout: 600 #Springboot2.0 不能設置為0 lettuce: pool: max-active: 50 max-wait: -1 max-idle: 8 min-idle: 0 redis2: database: 2 host: 192.168.50.144 port: 6379 password: timeout: 600

配置類

@EnableCaching@Configurationpublic class RedisDevConfiguration { @Bean(name = 'redisDevTemplate') public StringRedisTemplate redisTemplate(@Value('${spring.redis.host}') String hostName, @Value('${spring.redis.port}') int port, @Value('${spring.redis.password}') String password, @Value('${spring.redis.lettuce.pool.max-idle}') int maxIdle, @Value('${spring.redis.lettuce.pool.max-active}') int maxTotal, @Value('${spring.redis.database}') int index, @Value('${spring.redis.lettuce.pool.max-wait}') long maxWaitMillis,@Value('${spring.redis.lettuce.pool.min-idle}') int minIdle) { StringRedisTemplate temple = new StringRedisTemplate(); temple.setConnectionFactory(connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis,minIdle)); return temple; } @Bean(name = 'redisUatTemplate') public StringRedisTemplate redisUatTemplate(@Value('${spring.redis2.host}') String hostName, @Value('${spring.redis2.port}') int port, @Value('${spring.redis2.password}') String password, @Value('${spring.redis.lettuce.pool.max-idle}') int maxIdle, @Value('${spring.redis.lettuce.pool.max-active}') int maxTotal, @Value('${spring.redis2.database}') int index, @Value('${spring.redis.lettuce.pool.max-wait}') long maxWaitMillis,@Value('${spring.redis.lettuce.pool.min-idle}') int minIdle) { StringRedisTemplate temple = new StringRedisTemplate(); temple.setConnectionFactory(connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis,minIdle)); return temple; } public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle, int maxTotal, int index, long maxWaitMillis,int minIdle) { JedisConnectionFactory jedis = new JedisConnectionFactory(); jedis.setHostName(hostName); jedis.setPort(port); if (StringUtils.isNotEmpty(password)) { jedis.setPassword(password); } if (index != 0) { jedis.setDatabase(index); } jedis.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis,minIdle)); // 初始化連接pool jedis.afterPropertiesSet(); RedisConnectionFactory factory = jedis; return factory; } public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis,int minIdle) { JedisPoolConfig poolCofig = new JedisPoolConfig(); poolCofig.setMaxIdle(maxIdle); poolCofig.setMaxTotal(maxTotal); poolCofig.setMaxWaitMillis(maxWaitMillis); poolCofig.setMinIdle(minIdle); return poolCofig; }}

如何使用

@Resource(name = 'redisDevTemplate')private StringRedisTemplate template;@Resource(name = 'redisUatTemplate')private StringRedisTemplate lockTemplate;

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 乐陵市| 万盛区| 东源县| 蒲江县| 宜兴市| 屯昌县| 富阳市| 乌拉特后旗| 通州区| 通化市| 马鞍山市| 惠东县| 安顺市| 建德市| 南投市| 呼玛县| 山东| 黔西县| 五指山市| 崇义县| 轮台县| 厦门市| 周口市| 白银市| 峨山| 新竹县| 驻马店市| 新乐市| 林州市| 贺州市| 南岸区| 洛宁县| 高密市| 宜兰县| 汾阳市| 玛沁县| 伊金霍洛旗| 金寨县| 铜陵市| 保康县| 报价|