一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Spring boot 配置多個(gè)redis的方法示例

Spring boot 配置多個(gè)redis的方法示例

2021-05-30 17:03Simeone_xu Java教程

這篇文章主要介紹了Spring boot 配置多個(gè)redis的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

spring data提供其他項(xiàng)目,用來(lái)幫你使用各種各樣的nosql技術(shù),包括mongodb, neo4j, elasticsearch, solr, redis,gemfire, couchbase和cassandra。spring boot為redis, mongodb, elasticsearch, solr和gemfire提供自動(dòng)配置。你可以充分利用其他項(xiàng)目,但你需要自己配置它們。

單個(gè) redistemplate 的配置

使用 spring-boot-starter-data-redis 配置一個(gè) redis 是很簡(jiǎn)單的。

pom.xml 中該引入的依賴(lài)是要引入的,下面的是完整的 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 文件增加相關(guān)的配置

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

簡(jiǎn)單的配置就是這樣的,還有一些有關(guān)連接池或其他的詳細(xì)配置可以在 common-application-properties 中參考,或者直接查看 redisproperties 類(lèi)。

這里使用 redis 的例子我就直接在主類(lèi)中示例了。

?
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);
 }
}

直接運(yùn)行起來(lái),然后去 redis 查看結(jié)果:

?
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>

沒(méi)問(wèn)題的,順利的把數(shù)據(jù)存入到了 redis 中。

自定義 redistemplate 的序列類(lèi)

上面的實(shí)現(xiàn)其實(shí)有一個(gè)問(wèn)題,就是 redis 的 key 和 value 只能是 string 類(lèi)型的,是 redis-starter 默認(rèn)實(shí)現(xiàn)了的一個(gè) 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 存入一個(gè)對(duì)象是要報(bào)錯(cuò)的,我們修改一下代碼試試:

?
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);
}

運(yùn)行就報(bào)錯(cuò)如下:

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

這樣我們可以自己配置一下序列化類(lèi),這里我們把對(duì)象序列化為一個(gè) 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 類(lèi),所以 maven 要引入依賴(lài)

?
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>

這樣運(yùn)行可以看到 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>

配置另外一個(gè) 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 信息也是可以寫(xiě)入配置文件,用 @value 讀入
 // 這里是單機(jī)的配置,看看源代碼還可以配置集群和哨兵模式
 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());
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 边吃胸边膜下刺激免费男对女 | 嫩草香味| 我的青梅竹马是消防员2季未增删免费 | 精品高潮呻吟99AV无码 | 国产精品毛片高清在线完整版 | 全日本爽视频在线 | 日本69av | 楚乔传第二部免费观看全集完整版 | 啊啊啊好大好爽视频 | japonensis日本护士18 | 美女一级ba大片免色 | 日本中文字幕不卡在线一区二区 | 日产欧产va高清 | 成人国产第一区在线观看 | 青青网在线视频 | 日韩大片免费看 | 亚洲精品国产综合久久一线 | 国产91免费| a毛片免费观看完整 | 国产二区精品视频 | 男女男在线精品网站免费观看 | 秋霞一级成人欧美理论 | 亚洲欧洲色图 | 欧美生活一级片 | 欧美sq| 天使萌痴汉在线中文字幕 | 五月天婷婷网亚洲综合在线 | 大乳孕妇一级毛片 | 国产成人免费观看在线视频 | 99午夜高清在线视频在观看 | 动漫精品午夜在线播放 | 韩国美女激情vip | 丁香六月色婷婷综合网 | 狠狠干综合网 | 69日本xxxxxxxxx98 69人成网站色www | 百合漫画咱啪全彩抚慰 | 男人桶女下面60分钟视频 | 免费观看视频网站 | 黄色a站 | 免费特黄视频 | 婚色阿花在线全文免费笔 |