最近在项目中用到了两个Redis数据源,特此记录一下
一. 添加依赖
1. <dependency>
2. <groupId>org.springframework.boot</groupId>
3. <artifactId>spring-boot-starter-data-redis</artifactId>
4. </dependency>
5.
6. <dependency>
7. <groupId>redis.clients</groupId>
8. <artifactId>jedis</artifactId>
9. </dependency>
二. application.yml 配置
例如此处,order 和 user 两个不同的 redis数据源配置信息
1. spring:
2. redis:
3. order:
4. host: 100.99.202.32
5. port: 6379
6. password: NoLife
7. user:
8. host: 100.91.221.35
9. port: 9000
10. password: NoGame
三.
注意,redisOrderTemplate 和 redisUserTemplate,注入不同的数据源
1. @Configuration
2. public class RedisTemplateConfig {
3. //order
4. @Value("${spring.redis.order.host}")
5. private String orderHost;
6.
7. @Value("${spring.redis.order.port}")
8. private String orderPort;
9.
10. @Value("${spring.redis.order.password}")
11. private String orderPassword;
12.
13. //user
14. @Value("${spring.redis.user.host}")
15. private String userHost;
16.
17. @Value("${spring.redis.user.port}")
18. private String userPort;
19.
20. @Value("${spring.redis.user.password}")
21. private String userPassword;
22.
23. private static final int MAX_IDLE = 200; //最大空闲连接数
24. private static final int MAX_TOTAL = 1024; //最大连接数
25. private static final long MAX_WAIT_MILLIS = 10000; //建立连接最长等待时间
26.
27.
28. //配置工厂
29. public RedisConnectionFactory connectionFactory(String host, int port, String password, int maxIdle,
30. int maxTotal, long maxWaitMillis, int index) {
31. JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
32. jedisConnectionFactory.setHostName(host);
33. jedisConnectionFactory.setPort(port);
34.
35. if (!StringUtils.isEmpty(password)) {
36. jedisConnectionFactory.setPassword(password);
37. }
38.
39. if (index != 0) {
40. jedisConnectionFactory.setDatabase(index);
41. }
42.
43. jedisConnectionFactory.setPoolConfig(poolConfig(maxIdle, maxTotal, maxWaitMillis, false));
44. jedisConnectionFactory.afterPropertiesSet();
45. return jedisConnectionFactory;
46. }
47.
48. //连接池配置
49. public JedisPoolConfig poolConfig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
50. JedisPoolConfig poolConfig = new JedisPoolConfig();
51. poolConfig.setMaxIdle(maxIdle);
52. poolConfig.setMaxTotal(maxTotal);
53. poolConfig.setMaxWaitMillis(maxWaitMillis);
54. poolConfig.setTestOnBorrow(testOnBorrow);
55. return poolConfig;
56. }
57.
58.
59. //------------------------------------
60. @Bean(name = "redisOrderTemplate")
61. public StringRedisTemplate redisOrderTemplate() {
62. StringRedisTemplate template = new StringRedisTemplate();
63. template.setConnectionFactory(
64. connectionFactory(orderHost, Integer.parseInt(orderPort), orderPassword, MAX_IDLE, MAX_TOTAL, MAX_WAIT_MILLIS, 0));
65. return template;
66. }
67.
68. //------------------------------------
69. @Bean(name = "redisUserTemplate")
70. public StringRedisTemplate userUserTemplate() {
71. StringRedisTemplate template = new StringRedisTemplate();
72. template.setConnectionFactory(
73. connectionFactory(userHost, Integer.parseInt(userPort), userPassword, MAX_IDLE, MAX_TOTAL, MAX_WAIT_MILLIS, 0));
74. return template;
75. }
76. }
四. 分别调用
1. @Resource(name = "redisOrderTemplate")
2. StringRedisTemplate redisOrderTemplate;
3.
4. @Resource(name = "redisUserTemplate")
5. StringRedisTemplate redisUserTemplate;
对 StringRedisTemplate 的操作极为简单
可参考如下文章即可,不再多余描述
五. 额外:当存储数据的key value为json格式时
需对key 和value 进行序列化设置
首先,在pom.xml中引入 fastjson 依赖
1. <dependency>
2. <groupId>com.alibaba</groupId>
3. <artifactId>fastjson</artifactId>
4. <version>1.2.58</version>
5. </dependency>
其次, 在 标题 三 的 RedisTemplateConfig 中,添加序列化方法. 需序列化时调用即可
1. public void redisSerializeConfig(StringRedisTemplate template) {
2. Jackson2JsonRedisSerializer<Object> redisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
3. StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
4. ObjectMapper om = new ObjectMapper();
5. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
6. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
7. redisSerializer.setObjectMapper(om);
8.
9. template.setKeySerializer(stringRedisSerializer);
10. template.setValueSerializer(redisSerializer);
11. template.setHashKeySerializer(stringRedisSerializer);
12. template.setHashValueSerializer(redisSerializer);
13. //template.afterPropertiesSet();
14. }