Spring boot 配置多个redis的方法示例

时间:2021-10-24 06:27:51

spring data提供其他项目,用来帮你使用各种各样的nosql技术,包括mongodb, neo4j, elasticsearch, solr, redis,gemfire, couchbase和cassandra。spring boot为redis, mongodb, elasticsearch, solr和gemfire提供自动配置。你可以充分利用其他项目,但你需要自己配置它们。

单个 redistemplate 的配置

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
 xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelversion>4.0.0</modelversion>
 
 <groupid>me.deweixu</groupid>
 <artifactid>muti-redis</artifactid>
 <version>0.0.1-snapshot</version>
 <packaging>jar</packaging>
 
 <name>muti-redis</name>
 <description>config mutiple redis host</description>
 
 <parent>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-parent</artifactid>
  <version>2.0.4.release</version>
  <relativepath/> <!-- lookup parent from repository -->
 </parent>
 
 <properties>
  <project.build.sourceencoding>utf-8</project.build.sourceencoding>
  <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
  <java.version>1.8</java.version>
 </properties>
 
 <dependencies>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-data-redis</artifactid>
  </dependency>
 
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-test</artifactid>
   <scope>test</scope>
  </dependency>
 </dependencies>
 
 <build>
  <plugins>
   <plugin>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-maven-plugin</artifactid>
   </plugin>
  </plugins>
 </build>
</project>

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

?
1
2
spring.redis.host=localhost
spring.redis.port=6379

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 查看结果:

?
1
2
3
4
5
6
$ 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。查看其源代码是这样的

?
1
2
3
4
5
6
7
public stringredistemplate() {
 redisserializer<string> stringserializer = new stringredisserializer();
 this.setkeyserializer(stringserializer);
 this.setvalueserializer(stringserializer);
 this.sethashkeyserializer(stringserializer);
 this.sethashvalueserializer(stringserializer);
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
@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 中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@bean
jedisconnectionfactory jedisconnectionfactory() {
 return new jedisconnectionfactory();
}
 
@bean
redistemplate<string, object> firstredistemplate() {
 redistemplate<string, object> 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<string, object> redistemplate;
 
@override
public void run(string... args) throws exception {
 boundvalueoperations<string, object> 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 要引入依赖

?
1
2
3
4
5
6
7
8
9
<dependency>
 <groupid>com.fasterxml.jackson.core</groupid>
 <artifactid>jackson-databind</artifactid>
 <version>2.9.0</version>
</dependency>
<dependency>
 <groupid>redis.clients</groupid>
 <artifactid>jedis</artifactid>
</dependency>

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

?
1
2
3
127.0.0.1:6379> get person
"[\"me.deweixu.mutiredis.entity.people\",{\"name\":\"deweixu\",\"age\":26}]"
127.0.0.1:6379>

配置另外一个 redistemplate

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@bean
redistemplate<string, object> secondredistemplate() {
 redistemplate<string, object> 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 即可

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@autowired
redistemplate<string, object> firstredistemplate;
 
@autowired
redistemplate<string, object> secondredistemplate;
 
@override
public void run(string... args) throws exception {
 boundvalueoperations<string, object> op = firstredistemplate.boundvalueops("person");
 people people = new people();
 people.setage(26);
 people.setname("deweixu");
 op.set(people);
 boundvalueoperations<string, object> secondop = secondredistemplate.boundvalueops("person");
 people getpeople = (people) secondop.get();
 system.out.println(getpeople.getname() + "," + getpeople.getage());
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://segmentfault.com/a/1190000016321700