前言
在集群环境的情况下连接多个Redis数据库是很正常的情况,因为平时都是使用本地环境的单Redis情况比较多,在这里用代码总结一下连接多个数据库的情况(主要是不同ip,同一个ip的不通数据库修改不通地方即可),这里还是使用的springboot提供的spring-boot-starter-data-redis工具包,具体介绍如下:
1.引入redis相关的jar
org.springframework.boot spring-boot-starter-parent1.5.6.RELEASE org.springframework.boot spring-boot-starter-data-redisorg.springframework.boot spring-boot-starter-testorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-configuration-processortrue
2.在配置文件application.properties文件中设置redis相关配置,这里我使用了一个本地的redis数据库,一个远程的redis数据库,这里只假设了ip地址不同,其他的配置都相同:
#配置缓存redis spring.redis.database=8 # Redis服务器地址 spring.redis.host=127.0.0.1 # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password= # 连接池最大连接数(使用负值表示没有限制) spring.redis.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.pool.max-wait=-1 # 连接池中的最大空闲连接 spring.redis.pool.max-idle=8 # 连接池中的最小空闲连接 spring.redis.pool.min-idle=0 # 连接超时时间(毫秒) spring.redis.keytimeout=1000 spring.redis.timeout=0 #配置第二个redis数据库地址 spring.redis.host2=172.19.3.150
3.添加RedisTemplate的Bean:
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.EnableAutoConfiguration;
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.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String hostName;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String passWord;
@Value("${spring.redis.pool.max-idle}")
private int maxIdl;
@Value("${spring.redis.pool.min-idle}")
private int minIdl;
@Value("${spring.redis.database}")
private int database;
@Value("${spring.redis.keytimeout}")
private long keytimeout;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.host2}")
private String hostName2;
@Bean
public RedisConnectionFactory redisConnectionFactory(){
JedisPoolConfig poolConfig=new JedisPoolConfig();
poolConfig.setMaxIdle(maxIdl);
poolConfig.setMinIdle(minIdl);
poolConfig.setTestonBorrow(true);
poolConfig.setTestonReturn(true);
poolConfig.setTestWhileIdle(true);
poolConfig.setNumTestsPerEvictionRun(10);
poolConfig.setTimeBetweenEvictionRunsMillis(60000);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);
jedisConnectionFactory.setHostName(hostName);
if(!passWord.isEmpty()){
jedisConnectionFactory.setPassword(passWord);
}
jedisConnectionFactory.setPort(port);
jedisConnectionFactory.setDatabase(database);
return jedisConnectionFactory;
}
@Bean
public RedisConnectionFactory redisConnectionFactory2(){
JedisPoolConfig poolConfig=new JedisPoolConfig();
poolConfig.setMaxIdle(maxIdl);
poolConfig.setMinIdle(minIdl);
poolConfig.setTestonBorrow(true);
poolConfig.setTestonReturn(true);
poolConfig.setTestWhileIdle(true);
poolConfig.setNumTestsPerEvictionRun(10);
poolConfig.setTimeBetweenEvictionRunsMillis(60000);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);
jedisConnectionFactory.setHostName(hostName2);
if(!passWord.isEmpty()){
jedisConnectionFactory.setPassword(passWord);
}
jedisConnectionFactory.setPort(port);
jedisConnectionFactory.setDatabase(database);
return jedisConnectionFactory;
}
@Bean(name = "redisTemplate1")
public RedisTemplate redisTemplateObject() throws Exception {
RedisTemplate redisTemplateObject = new RedisTemplate();
redisTemplateObject.setConnectionFactory(redisConnectionFactory());
setSerializer(redisTemplateObject);
redisTemplateObject.afterPropertiesSet();
return redisTemplateObject;
}
@Bean(name = "redisTemplate2")
public RedisTemplate redisTemplateObject2() throws Exception {
RedisTemplate redisTemplateObject = new RedisTemplate();
redisTemplateObject.setConnectionFactory(redisConnectionFactory2());
setSerializer(redisTemplateObject);
redisTemplateObject.afterPropertiesSet();
return redisTemplateObject;
}
private void setSerializer(RedisTemplate template) {
Jackson2JsonRedisSerializer
4.App启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String [] args){
SpringApplication.run(App.class);
}
}
5.测试多个Redis数据库连接:
import com.springRedis.App;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class PipelineTest {
@Autowired
@Resource(name = "redisTemplate1")
private RedisTemplate redisTemplate1;
@Autowired
@Resource(name = "redisTemplate2")
private RedisTemplate redisTemplate2;
@Test
public void testPipeLine(){
redisTemplate1.opsForValue().set("a",1);
redisTemplate1.opsForValue().set("b",2);
System.out.println("b:"+redisTemplate1.opsForValue().get("b"));
System.out.println("a:"+redisTemplate1.opsForValue().get("a"));
redisTemplate2.opsForValue().set("m",5);
redisTemplate2.opsForValue().set("n",6);
System.out.println("m:"+redisTemplate2.opsForValue().get("m"));
System.out.println("n:"+redisTemplate2.opsForValue().get("n"));
}
以上就是连接2个Redis数据库的例子,在这里还有一个需要注意的是不能将
private RedisTemplateredisTemplate1
代码中的redisTemplate1修改为redisTemplate,因为这个redisTemplate可能是程序中默认的全局变量,具体的代码逻辑没有去查看,如果修改为了redisTemplate的话会出现以下错误:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate, ?>' available: expected single matching bean but found 2: redisTemplate1,redisTemplate2 at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:173) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ... 33 more
如果在设置RedisConnectionFactory的连接工厂时,一定要保留一个如下的代码:
public RedisConnectionFactory redisConnectionFactory()
否则会出现以下错误:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' available: expected single matching bean but found 2: redisConnectionFactory1,redisConnectionFactory2 at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:173) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ... 47 more
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



