SpringBoot+ Redis多数据源配置
-
springboot2.4.0
-
redis6.0.8
1.pom.xml配置
<!--springboot整合redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<!-- 过滤lettuce,使用jedis作为redis客户端 -->
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- redis 依赖commons-pool -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2.application.properties文件配置
#redis-db1
spring.redis.database=1
spring.redis.port=9602
spring.redis.password=XXXXXXXXWSX
spring.redis.host=120.XX.XX.XX
#redis-db0
spring.redis2.database=0
spring.redis2.port=9602
spring.redis2.password=XXXXXXXXWSX
spring.redis2.host=120.XX.XX.XX
### \u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09
spring.redis.timeout=10000ms
### \u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09
spring.redis.jedis.pool.max-active=200
### \u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09
spring.redis.jedis.pool.max-wait=1000ms
### \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5
spring.redis.jedis.pool.max-idle=300
### \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5
spring.redis.jedis.pool.min-idle=1
3.redis配置类
package com.taiji.amc.config.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.RedisPassword;
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;
/**
*
* redis配置类
*
*/
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)// 自动获取application.yml中的配置
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfig {
private RedisProperties properties;
public RedisConfig(RedisProperties properties){
this.properties = properties;
}
// ========================= ↓↓↓↓↓↓ 默认redis配置 ↓↓↓↓↓↓ =========================
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
return this.buildRedisTemplate(factory);
}
protected RedisTemplate<String, Object> buildRedisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(factory);
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(objectMapper);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
// key采用String的序列化方式
redisTemplate.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
redisTemplate.setHashKeySerializer(stringRedisSerializer);
// valuevalue采用String序列化方式
redisTemplate.setValueSerializer(serializer);
// hash的value采用String序列化方式
redisTemplate.setHashValueSerializer(serializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
// ========================= ↓↓↓↓↓↓ 第2个redis配置 ↓↓↓↓↓↓ =========================
@Bean(name = "redisTemplate2")
public RedisTemplate<String, Object> redisTemplate2(@Value("${spring.redis2.host}") String host,
@Value("${spring.redis2.port}") int port,
@Value("${spring.redis2.database:0}") int database,
@Value("${spring.redis2.password}") String password
) {
return buildRedisTemplate(this.buildConnectionFactory(host, password, port, database, this.jedisPoolConfig()));
}
public JedisConnectionFactory buildConnectionFactory(String host, String password, int port, int database, JedisPoolConfig jedisPoolConfig) {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setDatabase(database);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisBuilder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
jedisBuilder.poolConfig(jedisPoolConfig);
JedisClientConfiguration jedisClientConfiguration = jedisBuilder.build();
return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
}
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(properties.getJedis().getPool().getMaxActive());
config.setMaxIdle(properties.getJedis().getPool().getMaxIdle());
config.setMinIdle(properties.getJedis().getPool().getMinIdle());
if (properties.getJedis().getPool().getMaxWait() != null) {
config.setMaxWaitMillis(properties.getJedis().getPool().getMaxWait().toMillis());
}
return config;
}
}
4.使用
@Controller
@Slf4j
public class TestCmsController {
@Resource(name = "redisTemplate2")
private RedisTemplate<String, Object> redisTemplate2;
@RequestMapping(value = "testInfo2")
@ResponseBody
public void testInfo2(String key) {
String accessToken = "";
if(null == key){
key = RedisKeyConstants.WECHAT_PUBLIC_ACCOUNT_ACCESS_TOKEN;
}
Object wechatAccountAccessToken = redisTemplate2.opsForValue().get(key);
System.out.println("查询到的accessToken="+wechatAccountAccessToken);
}
}
原文地址:https://blog.csdn.net/sinat_34454743/article/details/145161019
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!