麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > Java > 正文

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

2024-07-14 08:42:19
字體:
供稿:網(wǎng)友

Spring Data提供其他項(xiàng)目,用來幫你使用各種各樣的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 中該引入的依賴是要引入的,下面的是完整的 pom.xml

<?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)的配置

spring.redis.host=localhostspring.redis.port=6379

簡(jiǎn)單的配置就是這樣的,還有一些有關(guān)連接池或其他的詳細(xì)配置可以在 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;@SpringBootApplicationpublic 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)行起來,然后去 redis 查看結(jié)果:

$ redis-cli127.0.0.1:6379> keys *1) "PERSON"127.0.0.1:6379> get PERSON"Deweixu"127.0.0.1:6379>

沒問題的,順利的把數(shù)據(jù)存入到了 redis 中。

自定義 RedisTemplate 的序列類

上面的實(shí)現(xiàn)其實(shí)有一個(gè)問題,就是 redis 的 key 和 value 只能是 String 類型的,是 redis-starter 默認(rèn)實(shí)現(xiàn)了的一個(gè) StringRedisTemplate。查看其源代碼是這樣的

 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ò)的,我們修改一下代碼試試:

 @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

這樣我們可以自己配置一下序列化類,這里我們把對(duì)象序列化為一個(gè) json 存入到 redis 中

 @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 要引入依賴

<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 中變成這樣了:

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

配置另外一個(gè) redisTemplate

 @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 讀入  // 這里是單機(jī)的配置,看看源代碼還可以配置集群和哨兵模式  RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration("localhost", 6379);  JedisConnectionFactory factory = new JedisConnectionFactory(configuration);  redisTemplate.setConnectionFactory(factory);  return redisTemplate; }

注入 secondRedisTemplate 即可

 @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í)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美高清视频一区 | 黄视频网站免费 | 久久av一区二区 | 91精品国产综合久久久欧美 | 国产亚洲精品美女久久久 | 暖暖免费观看高清完整版电影 | 狠狠干夜夜草 | 中文字幕www.| 成人444kkkk在线观看 | chinesehdxxxx无套 久久另类视频 | 在线成人亚洲 | 国产乱淫a∨片免费观看 | 日本高清com | 成人免费观看在线视频 | 久久久久久久久久性 | 成人在线视频免费看 | 欧美精品一区二区视频 | 毛片久久 | 精品久久久久久成人av | 国产正在播放 | 欧美一级黄色网 | 成人一级黄色 | 午夜在线成人 | 黄色成人av在线 | 未成年人在线观看 | 一级毛片特黄 | 国产亚洲精品久久久久久久久久 | 韩国精品视频在线观看 | 久久精品亚洲一区二区 | 中文字幕天堂在线 | 午夜精品福利在线观看 | 久久久久久片 | 国产一区二区三区精品在线观看 | 97人人草| 久久久久国产精品久久久久 | www.91成人 | 老女人碰碰在线碰碰视频 | 国产亚洲精品久久久久久大师 | 久久久久久久久久亚洲 | 日本黄色一级视频 | 九九热九九热 |