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