感謝大佬的參考文檔: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