我是靠谱客的博主 如意戒指,这篇文章主要介绍redistemplate 多数据源配置,亲测可用,不能用你打我,现在分享给大家,希望可以做个参考。

redistemplate 多数据源配置,网上找了很多博客,都不能用,要么代码报错,要么不报错查询不到数据,真操蛋,气死我了,自己摸索写了个示例,亲测可用

 

pom.xml 引入这两个

复制代码
1
2
3
4
5
6
7
8
9
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.cun.redis; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisClientConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; @Configuration public class RedisTemplateConfig { @Bean(name = "redisTemplate0") public RedisTemplate<String, Object> redisTemplate(@Value("${spring.redis.host}") String host, @Value("${spring.redis.password}") String password, @Value("${spring.redis.port}") int port, @Value("${spring.redis.database}") int database) { return buildRedisTemplate(buildConnectionFactory(jedisPoolConfig(), host, password, port, database)); } @Bean(name = "redisTemplate1") public RedisTemplate<String, Object> redisTemplate1(@Value("${spring.redis1.host}") String host, @Value("${spring.redis1.password}") String password, @Value("${spring.redis1.port}") int port, @Value("${spring.redis1.database}") int database) { return buildRedisTemplate(buildConnectionFactory(jedisPoolConfig(), host, password, port, database)); } protected RedisTemplate<String, Object> buildRedisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); template.setKeySerializer(stringRedisSerializer); template.setValueSerializer(stringRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); template.setHashValueSerializer(stringRedisSerializer); template.afterPropertiesSet(); return template; } @Bean public JedisPoolConfig jedisPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); //最大连接数 jedisPoolConfig.setMaxTotal(100); //最小空闲连接数 jedisPoolConfig.setMinIdle(20); //当池内没有可用的连接时,最大等待时间 jedisPoolConfig.setMaxWaitMillis(10000); return jedisPoolConfig; } public JedisConnectionFactory buildConnectionFactory(JedisPoolConfig jedisPoolConfig, String host, String password, int port, int database) { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); redisStandaloneConfiguration.setHostName(host); redisStandaloneConfiguration.setDatabase(database); redisStandaloneConfiguration.setPort(port); redisStandaloneConfiguration.setPassword(password); JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisBuilder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder(); jedisBuilder.poolConfig(jedisPoolConfig); JedisClientConfiguration jedisClientConfiguration = jedisBuilder.build(); return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration); } }

application.yml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server: port: 80 servlet: context-path: / tomcat: uri-encoding: utf-8 spring: redis: database: 0 host: 119.97.185.22 port: 9736 password: ***** redis1: database: 1 host: 119.97.185.22 port: 9736 password: ******

调用很简单,如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.cun.redis; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.Set; @RestController public class HomeControlle { @Resource(name = "redisTemplate0") private RedisTemplate redisTemplate; @Resource(name = "redisTemplate1") private RedisTemplate redisTemplate1; @RequestMapping("/hello") public String hello() { Set<String> keys = redisTemplate.keys("*"); System.out.println(keys.size()); System.out.println(redisTemplate1.keys("*").size()); return "hello"; } }

 

 

最后

以上就是如意戒指最近收集整理的关于redistemplate 多数据源配置,亲测可用,不能用你打我的全部内容,更多相关redistemplate内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部