Springboot配置多个redis的方法示例

Spring Data提供其他项目,用来帮你使用各种各样的NOSQL技术,包括MongoDB, Neo4J, Elasticsearch, Solr, redis,Gemfire, Couchbase和Cassandra。Spring Boot为Redis, MongoDB, Elasticsearch, Solr和Gemfire提供自动配置。你可以充分利用其他项目,但你需要自己配置它们。

创新互联建站长期为成百上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为沾益企业提供专业的成都做网站、成都网站建设,沾益网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。

单个 RedisTemplate 的配置

使用 spring-boot-starter-data-redis 配置一个 redis 是很简单的。

pom.xml 中该引入的依赖是要引入的,下面的是完整的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>

 4.0.0

 me.deweixu
 muti-redis
 0.0.1-SNAPSHOT
 jar

 muti-redis
 config mutiple redis host

 
  org.springframework.boot
  spring-boot-starter-parent
  2.0.4.RELEASE
   
 

 
  UTF-8
  UTF-8
  1.8
 

 
  
   org.springframework.boot
   spring-boot-starter-data-redis
  

  
   org.springframework.boot
   spring-boot-starter-test
   test
  
 

 
  
   
    org.springframework.boot
    spring-boot-maven-plugin
   
  
 


application.properties 文件增加相关的配置

spring.redis.host=localhost
spring.redis.port=6379

简单的配置就是这样的,还有一些有关连接池或其他的详细配置可以在 common-application-properties 中参考,或者直接查看 RedisProperties 类。

这里使用 redis 的例子我就直接在主类中示例了。

package me.deweixu.mutiredis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;

@SpringBootApplication
public class MutiRedisApplication implements CommandLineRunner {

 @Autowired
 StringRedisTemplate stringRedisTemplate;

 @Override
 public void run(String... args) throws Exception {
  BoundValueOperations op = stringRedisTemplate.boundValueOps("PERSON");
  op.set("Deweixu");
 }

 public static void main(String[] args) {
  SpringApplication.run(MutiRedisApplication.class, args);
 }
}

直接运行起来,然后去 redis 查看结果:

$ redis-cli
127.0.0.1:6379> keys *
1) "PERSON"
127.0.0.1:6379> get PERSON
"Deweixu"
127.0.0.1:6379>

没问题的,顺利的把数据存入到了 redis 中。

自定义 RedisTemplate 的序列类

上面的实现其实有一个问题,就是 redis 的 key 和 value 只能是 String 类型的,是 redis-starter 默认实现了的一个 StringRedisTemplate。查看其源代码是这样的

 public StringRedisTemplate() {
  RedisSerializer stringSerializer = new StringRedisSerializer();
  this.setKeySerializer(stringSerializer);
  this.setValueSerializer(stringSerializer);
  this.setHashKeySerializer(stringSerializer);
  this.setHashValueSerializer(stringSerializer);
 }

如果使用 StringRedisTemplate 存入一个对象是要报错的,我们修改一下代码试试:

 @Autowired
 StringRedisTemplate stringRedisTemplate;

 @Override
 public void run(String... args) throws Exception {
  BoundValueOperations op = stringRedisTemplate.boundValueOps("PERSON");
  op.set(new Person("Deiweixu", 22));
 }

 public static void main(String[] args) {
  SpringApplication.run(MutiRedisApplication.class, args);
 }

运行就报错如下:

Caused by: java.lang.ClassCastException: me.deweixu.mutiredis.MutiRedisApplication$Person cannot be cast to java.base/java.lang.String
    at org.springframework.data.redis.serializer.StringRedisSerializer.serialize(StringRedisSerializer.java:35) ~[spring-data-redis-2.0.9.RELEASE.jar:2.0.9.RELEASE]
    at org.springframework.data.redis.core.AbstractOperations.rawValue(AbstractOperations.java:126) ~[spring-data-redis-2.0.9.RELEASE.jar:2.0.9.RELEASE]
    at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:197) ~[spring-data-redis-2.0.9.RELEASE.jar:2.0.9.RELEASE]
    at org.springframework.data.redis.core.DefaultBoundValueOperations.set(DefaultBoundValueOperations.java:110) ~[spring-data-redis-2.0.9.RELEASE.jar:2.0.9.RELEASE]
    at me.deweixu.mutiredis.MutiRedisApplication.run(MutiRedisApplication.java:19) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    ... 5 common frames omitted

这样我们可以自己配置一下序列化类,这里我们把对象序列化为一个 json 存入到 redis 中

 @Bean
 JedisConnectionFactory jedisConnectionFactory() {
  return new JedisConnectionFactory();
 }

 @Bean
 RedisTemplate firstRedisTemplate() {
  RedisTemplate redisTemplate = new RedisTemplate<>();
  ObjectMapper om = new ObjectMapper();
  om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(om);
  redisTemplate.setKeySerializer(new StringRedisSerializer());
  redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
  redisTemplate.setHashKeySerializer(new StringRedisSerializer());
  redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
  redisTemplate.setConnectionFactory(jedisConnectionFactory());
  return redisTemplate;
 }

 @Autowired
 RedisTemplate redisTemplate;

 @Override
 public void run(String... args) throws Exception {
  BoundValueOperations op = redisTemplate.boundValueOps("PERSON");
  People people = new People();
  people.setAge(26);
  people.setName("Deweixu");
  op.set(people);
  People getPeople = (People) op.get();
  System.out.println(getPeople.getName() + "," + getPeople.getAge());
 }

上面使用了 Jedis 的配置和 GenericJackson2JsonRedisSerializer 类,所以 maven 要引入依赖


 com.fasterxml.jackson.core
 jackson-databind
 2.9.0


 redis.clients
 jedis

这样运行可以看到 redis 中变成这样了:

127.0.0.1:6379> get PERSON
"[\"me.deweixu.mutiredis.entity.People\",{\"name\":\"Deweixu\",\"age\":26}]"
127.0.0.1:6379>

配置另外一个 redisTemplate

 @Bean
 RedisTemplate secondRedisTemplate() {
  RedisTemplate redisTemplate = new RedisTemplate<>();
  ObjectMapper om = new ObjectMapper();
  om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(om);
  redisTemplate.setKeySerializer(new StringRedisSerializer());
  redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
  redisTemplate.setHashKeySerializer(new StringRedisSerializer());
  redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
  // 这里的 redis 信息也是可以写入配置文件,用 @Value 读入
  // 这里是单机的配置,看看源代码还可以配置集群和哨兵模式
  RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration("localhost", 6379);
  JedisConnectionFactory factory = new JedisConnectionFactory(configuration);
  redisTemplate.setConnectionFactory(factory);
  return redisTemplate;
 }

注入 secondRedisTemplate 即可

 @Autowired
 RedisTemplate firstRedisTemplate;

 @Autowired
 RedisTemplate secondRedisTemplate;

 @Override
 public void run(String... args) throws Exception {
  BoundValueOperations op = firstRedisTemplate.boundValueOps("PERSON");
  People people = new People();
  people.setAge(26);
  people.setName("Deweixu");
  op.set(people);
  BoundValueOperations secondOp = secondRedisTemplate.boundValueOps("PERSON");
  People getPeople = (People) secondOp.get();
  System.out.println(getPeople.getName() + "," + getPeople.getAge());
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


文章标题:Springboot配置多个redis的方法示例
分享地址:http://csdahua.cn/article/gciheh.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流