springboot(8)-多数据源-同一个redis地址的两个database
感谢大佬的参考文档:http://blog.51cto.com/13954634/2170900
https://blog.csdn.net/lc199408/article/details/77154375
总结:
需要连接几个redis数据源,都可以只创建1个JedisConnectionFactory,其中JedisConnectionFactory使用 setdatabase、sethostName、setport等方法指定了访问Redis的哪个数据库。然后再创建多个RedisTemplate、StringRedisTemplate(两者的区别:https://blog.csdn.net/notsaltedfish/article/details/75948281)。使用@Autowired注解时,会自动创建一个实例,所以我们采用@Resource(name)会指定对应名称的@bean注解。
@Autowired
@Resource(name="secondStringRedisTemplate")
private StringRedisTemplate mStringRedisTemplate;
在运行上图时,由于碰到@Autowired,所以springboot会自动查找对应名称的@Bean:
@Bean(name="secondStringRedisTemplate")
public StringRedisTemplate secondStringRedisTemplate() {
Object template;
template = new StringRedisTemplate();
((StringRedisTemplate) template).setConnectionFactory(secondConnectionFactory());
return (StringRedisTemplate) template;
}
public JedisConnectionFactory secondConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory(jedisPoolConfig());
((JedisConnectionFactory)factory).setHostName(RedisProperty.getHostName);
((JedisConnectionFactory)factory).setPort(RedisProperty);
((JedisConnectionFactory)factory).setDatabase(RedisProperty);
((JedisConnectionFactory)factory).setPoolConfig(PoolConfig());
return factory;
}
然后生成对应的template,上面的代码中通过prefix自动匹配到数据源,而@Autowired会自动将配置项set方法自动设置RedisProperty(需要创建这个类):
@ConfigurationProperties(prefix = "spring.redis")//不确定需不需要加prefix,可以试试
//如果加上这个会匹配到spring.redis配置并set各属性,但是jedisConnectionfactory(template不能覆盖)的prefix=spring.redis2可以覆盖这里的redis,会将redis2的配置set到各属性中。
public class RedisProperty {
private String hostName;
private int port;
private String password;
public String getHostName() {
return this.hostName;
}
public int getPort() {
return this.port;
}
public int getDatabase() {
return this.database;
}
}
然后又运行到setConnectionFactory(secondConnectionFactory()),其中secondConnectionFactory()函数指定了对应地址的redis(通过RedisProperty.getdatabase等方法),这样就可以实现使用不同的redis数据源。
实战一:
application.properties:
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=xxx
## Redis服务器连接端口
spring.redis.port=6379
# 连接超时时间(毫秒)
spring.redis.timeout=5000
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis2.database=1
## Redis服务器地址
spring.redis2.host=xxx
## Redis服务器连接端口
spring.redis2.port=6379
# 连接超时时间(毫秒)
spring.redis2.timeout=5000
然后创建redisProperty.class:在这里匹配了redis的数据源地址1
@ConfigurationProperties(prefix = "spring.redis")//不确定需不需要加prefix,可以试试
//如果加上这个会匹配到spring.redis配置并set各属性,但是jedisConnectionfactory(template不能覆盖)的prefix=spring.redis2可以覆盖这里的redis,会将redis2的配置set到各属性中。
public class RedisProperty {
private String hostName;
private int port;
private String password;
public String getHostName() {
return this.hostName;
}
public int getPort() {
return this.port;
}
public int getDatabase() {
return this.database;
}
}
然后创建redisConfig:
创建StringredisTemplate,设置beanname(一定要设置,因为创建实例时,需要匹配beanname):
@Bean(name="RedisTemplate")
@ConditionalOnBean({RedisConnectionFactory.class})
public RedisTemplate<Object, Object> RedisTemplate() {
Object template;
template = new RedisTemplate();
((RedisTemplate) template).setConnectionFactory(RedisConnectionFactory());
return (RedisTemplate) template;
}
@Bean(name="StringRedisTemplate")
public StringRedisTemplate StringRedisTemplate() {
Object template;
template = new StringRedisTemplate();
((StringRedisTemplate) template).setConnectionFactory(RedisConnectionFactory());
return (StringRedisTemplate) template;
}
创建RedisConnectionFactory:
public JedisConnectionFactory RedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory(jedisPoolConfig());
((JedisConnectionFactory)factory).setHostName(RedisProperty.getHostName);
((JedisConnectionFactory)factory).setPort(RedisProperty);
((JedisConnectionFactory)factory).setDatabase(RedisProperty);
((JedisConnectionFactory)factory).setPoolConfig(PoolConfig());
return factory;
}
然后创建一个controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@RestController
public class Getxxx {
@Autowired
@Resource(name="StringRedisTemplate")
private StringRedisTemplate mStringRedisTemplate;
@GetMapping(value = xxx)
public String getxxx(
mStringRedisTemplate.delete(key);
mStringRedisTemplate.opsForValue().set(key,value)
}
}
使用第二个redis操作:
在redisConfig创建SecondStringredisTemplate,设置beanname(一定要设置,因为创建实例时,需要匹配beanname):
@Bean(name="SecondRedisTemplate")
@ConditionalOnBean({RedisConnectionFactory.class})
public RedisTemplate<Object, Object> RedisTemplate() {
Object template;
template = new RedisTemplate();
((RedisTemplate) template).setConnectionFactory(SecondRedisConnectionFactory());
return (RedisTemplate) template;
}
@Bean(name="SecondStringRedisTemplate")
public StringRedisTemplate StringRedisTemplate() {
Object template;
template = new StringRedisTemplate();
((StringRedisTemplate) template).setConnectionFactory(SecondRedisConnectionFactory());
return (StringRedisTemplate) template;
}
创建SecondRedisConnectionFactory:
@Bean({"secondRedisConnectionFactory"})
@ConfigurationProperties(prefix="spring.redis2")//匹配redis2,会覆盖RedisProperty的prefix
public JedisConnectionFactory secondRedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory(jedisPoolConfig());
((JedisConnectionFactory)factory).setHostName(RedisProperty.getHostName);
((JedisConnectionFactory)factory).setPort(RedisProperty);
((JedisConnectionFactory)factory).setDatabase(RedisProperty);
((JedisConnectionFactory)factory).setPoolConfig(PoolConfig());
return factory;
}
然后创建一个新的controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@RestController
public class Getxxx {
@Autowired
@Resource(name="SecondStringRedisTemplate")
private StringRedisTemplate mStringRedisTemplate;
@GetMapping(value = xxx)
public String getxxx(
mStringRedisTemplate.delete(key);
mStringRedisTemplate.opsForValue().set(key,value)
}
}
这样使用不同的
@Autowired
@Resource(name="SecondStringRedisTemplate")
private StringRedisTemplate mStringRedisTemplate;
就可以达到使用不同的redis地址。
方法二:(比较科学)
创建RedisConfig.class:Template匹配redis配置源地址,然后传给ConnectionFactory函数中:
@Configuration
public class RedisDevConfiguration {
@Bean(name = "redisTemplate")
public StringRedisTemplate redisTemplate(
//可以把这一步的@value放在RedisProperty.class进行,设置setter和getter
@Value("${spring.redis.host}") String hostName,
@Value("${spring.redis.port}") int port, @Value("${spring.redis.password}") String password,
@Value("${spring.redis.database}") int index {
StringRedisTemplate temple = new StringRedisTemplate();
temple.setConnectionFactory(
connectionFactory(hostName, port, password,index));
return temple;
}
public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle,
int maxTotal, int index, long maxWaitMillis, boolean testOnBorrow) {
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, testOnBorrow));
// 初始化连接pool
jedis.afterPropertiesSet();
RedisConnectionFactory factory = jedis;
return factory;
}
public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
JedisPoolConfig poolCofig = new JedisPoolConfig();
poolCofig.setMaxIdle(maxIdle);
poolCofig.setMaxTotal(maxTotal);
poolCofig.setMaxWaitMillis(maxWaitMillis);
poolCofig.setTestOnBorrow(testOnBorrow);
return poolCofig;
}
}
如果需要加上另一个redis(需要几个redis,就创建几个redisTemplate):
就添加:
@Bean(name = "redis2Template")
public StringRedisTemplate secondredisTemplate(@Value("${spring.redis2.host}") String hostName,
@Value("${spring.redis.port}") int port, @Value("${spring.redis.password}") String password,
@Value("${spring.redis2.database}") int index {
StringRedisTemplate temple = new StringRedisTemplate();
temple.setConnectionFactory(
connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow));
return temple;
}
同理,创建Controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@RestController
public class Getxxx {
@Autowired
@Resource(name="redisTemplate")//或者是redis2Template
private StringRedisTemplate mStringRedisTemplate;
@GetMapping(value = xxx)
public String getxxx(
mStringRedisTemplate.delete(key);
mStringRedisTemplate.opsForValue().set(key,value)
}
}
这样使用不同的
@Autowired
@Resource(name="SecondStringRedisTemplate")
private StringRedisTemplate mStringRedisTemplate;
就可以达到使用不同的redis地址。
原文链接:https://blog.csdn.net/qq_38204134/article/details/84859875
点赞
————————————————
版权声明:本文为CSDN博主「那些年的代码」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44018338/article/details/99948448