Springboot使⽤多个RedisTemplate
问题描述
想在⼀个JAVA Class 向同⼀个Redis实例的不同 dbindex 写⼊数据,⾮常类似于StackOverflowe上的[。在中描述了如何使⽤Spring boot访问Redis,在创建JedisConnectionFactory的时候指定dbindex:
JedisConnectionFactory factory = new JedisConnectionFactory();
...
factory.setDatabase(databaseId);//set dbindex
因此,⼤概思路是配置2个RedisTemplate,其中⼀个RedisTemplate负责访问dbindex=1的数据库;另⼀个RedisTemplate负责访问dbindex=3的数据库。
根据,因此通过 @Bean(name=) ⽣成多个RedisTemplate。但是由于⽣成RedisTemplate需要传⼊JedisConnectionFactory实例,⽽我们是在JedisConnectionFactory中指定了访问Redis的哪个数据库(dbindex)。因此,就在创建JedisConnectionFactory实例的时候,使⽤
@Scope(scopeName = "prototype") 注解,这样的话Jedis连接⼯⼚就不再是单例模式了。因此,就有两个JedisConnectionFactory实例,每个实例通过jedisConnectionFactory.setDatabase()设置不同的dbindex。这种⽅式可能⾮常愚蠢,会引起严重的性能问题。
下⾯,来看看具体是怎么配置的:
@Scope(scopeName = "prototype")
public JedisConnectionFactory jedisConnectionFactory() {
JedisPoolConfig config = getRedisConfig();
JedisConnectionFactory factory = new JedisConnectionFactory(config);
factory.setUsePool(true);
factory.setHostName(host);
factory.setPort(port);
return factory;
}
每调⽤⼀次jedisConnectionFactory() 返回⼀个新的JedisConnectionFactory实例。
然后定义2个RedisTemplate Bean,jedisConnectionFactory.setDatabase() ⽅法分别设置不同的dbindex
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.Scope;
import org.tion.jedis.JedisConnectionFactory;
import org.RedisTemplate;适合新手的spring boot
import org.dis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import java.util.Map;
/**
* Created by Administrator on 2018/4/9.
*/
@Configuration
public class LoginMacRedisConfig {
private static final Logger logger = Logger(LoginMacRedisConfig.class);
@Value("1")
private int logmacDatabaseId;
@Value("3")
private int mobmaskDatabaseId;
@Bean
public JedisPoolConfig getRedisConfig() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(8);
config.setMinIdle(0);
return config;
}
@Scope(scopeName = "prototype")
public JedisConnectionFactory jedisConnectionFactory() {
JedisPoolConfig config = getRedisConfig();
JedisConnectionFactory factory = new JedisConnectionFactory(config);
factory.setUsePool(true);
factory.setHostName(host);
factory.setPort(port);
return factory;
}
@Bean(name = "login_mac")
public RedisTemplate<String, Map<String, String>> logmacRedisTemplate() {
final RedisTemplate<String, Map<String, String>> template = new RedisTemplate<>();
JedisConnectionFactory jedisConnectionFactory = jedisConnectionFactory();
jedisConnectionFactory.setDatabase(logmacDatabaseId);
template.setConnectionFactory(jedisConnectionFactory);
logger.info("host:{}, port:{}, database:{}", HostName(),Port(), Database()); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setHashValueSerializer(stringRedisSerializer);
return template;
}
@Bean(name = "mobile_mask")
public RedisTemplate<String, Map<String, String>> mobileMaskRedisTemplate() {
final RedisTemplate<String, Map<String, String>> template = new RedisTemplate<>();
JedisConnectionFactory jedisConnectionFactory = jedisConnectionFactory();
jedisConnectionFactory.setDatabase(mobmaskDatabaseId);
template.setConnectionFactory(jedisConnectionFactory);
logger.info("host:{}, port:{}, database:{}", HostName(),Port(), Database()); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setHashValueSerializer(stringRedisSerializer);
return template;
}
}
最后,再写⼀个Service类,就可以同时注⼊这两个RedisTemplate,操作同⼀个Redis服务器上的不同的dbindex了。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.HashOperations;
import org.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2018/4/10.
*/
@Service
public class RedisTestService {
@Autowired
@Qualifier("login_mac")
private RedisTemplate<String, Map<String, String>> template1;
@Autowired
@Qualifier("mobile_mask")
private RedisTemplate<String, Map<String, String>> template2;
public void write2Redis() {
HashOperations<String, String, String> hashOperations = template1.opsForHash();
Map<String, String> values = new HashMap<>();
values.put("dbindex", "1");
hashOperations.putAll("123", values);
template2.opsForHash().put("123", "dbindex", "3");
}
}
Application.java 启动类
@SpringBootApplication
public class Application implements CommandLineRunner{
@Autowired
private RedisTestService redisTestService;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void strings) throws Exception {
redisTestService.write2Redis();
}
}
在redisTestService对象中:有两个RedisTemplate实例:
两个RedisTemplate实例分别封装了两个JedisConnectionFactory:
调试结果下:
2018-04-10 20:18:34.754  INFO 13512 --- [          main] dis.LoginMacRedisConfig        : host:192.168.107.253, port:6379, database:1
2018-04-10 20:19:06.972  INFO 13512 --- [          main] dis.LoginMacRedisConfig        : host:192.168.107.253, port:6379, database:3
最终查看写⼊Redis结果,可以看出:dbindex 1 和 dbindex 3 都分别成功写⼊了数据。
redis 192.168.107.253:6379> SELECT 1
OK
redis 192.168.107.253:6379[1]> KEYS *
1) "123"
redis 192.168.107.253:6379[1]> HGET 123 dbindex
"1"
redis 192.168.107.253:6379[1]> SELECT 3
OK
redis 192.168.107.253:6379[3]> KEYS *
1) "123"
redis 192.168.107.253:6379[3]> HGET 123 dbindex
"3"
额外补充
其实要在同⼀个应⽤中访问不同的dbindex,⼀种⽅式是使⽤JedisPool,JedisPool创建Jedis,然后调⽤select⽅法选择dbindex。具体实现可参考。但这样的话,就不能使⽤RedisTemplate的各种⽅便的接⼝读写Redis了。
@Bean
public JedisPool redisPoolFactory() {
JedisPool jedisPool = new JedisPool(jedisPoolConfig(), host, port);
Jedis jedis = Resource();
jedis.select(3);
return jedisPool;
}
其实是可以像说的:通过RedisConnectionCommand的 select ⽅法来选择dbindex的,但是还是同样的问题,⽤不了RedisTemplate。
RedisConnection redisConnection = ConnectionFactory().getConnection();
DefaultStringRedisConnection stringRedisConnection = new  DefaultStringRedisConnection(redisConnection);
stringRedisConnection.select(2);
stringRedisConnection.set("test", "test");
另外这⾥也有⼀篇,也可以参考⼀下。Spring Boot 兼Redis新⼿,只能这样了。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。