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

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

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

瀏覽:2日期: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
相關文章:
主站蜘蛛池模板: 旺苍县| 陆良县| 胶南市| 泰和县| 尼勒克县| 晴隆县| 云龙县| 易门县| 长武县| 合阳县| 安新县| 大化| 扶风县| 望城县| 东阿县| 石嘴山市| 清镇市| 沙田区| 郴州市| 河东区| 齐齐哈尔市| 罗江县| 延长县| 西贡区| 弥渡县| 米泉市| 汾阳市| 郓城县| 阳新县| 钟山县| 内江市| 邛崃市| 峨眉山市| 淮安市| 玉龙| 墨竹工卡县| 错那县| 寻乌县| 应城市| 齐河县| 晋中市|