最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2018年09月10日 10:20:37   作者:Simeone_xu  
這篇文章主要介紹了Spring boot 配置多個redis的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Spring Data提供其他項目,用來幫你使用各種各樣的NoSQL技術(shù),包括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

<?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=localhost
spring.redis.port=6379

簡單的配置就是這樣的,還有一些有關(guān)連接池或其他的詳細配置可以在 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 查看結(jié)果:

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

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

自定義 RedisTemplate 的序列類

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

 public StringRedisTemplate() {
  RedisSerializer<String> 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<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>

這樣運行可以看到 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<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 即可

 @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());
 }

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Java中final的用法

    詳解Java中final的用法

    本文主要介紹了Java中final的使用方法,final是java的關(guān)鍵字,本文就詳細說明一下它的使用方法,需要的朋友可以參考下
    2015-08-08
  • SpringBoot2種單元測試方法解析

    SpringBoot2種單元測試方法解析

    這篇文章主要介紹了SpringBoot2種單元測試方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10
  • Java結(jié)構(gòu)型模式之橋接模式詳解

    Java結(jié)構(gòu)型模式之橋接模式詳解

    橋接模式是一種很實用的結(jié)構(gòu)型模式,如果系統(tǒng)中某個類存在兩個獨立變化的維度,通過橋接模式將這兩個維度分離出來,使兩者可以獨立擴展
    2023-02-02
  • Java mysql特殊形式的查詢語句詳解

    Java mysql特殊形式的查詢語句詳解

    這篇文章主要介紹了Java mysql特殊形式的查詢,包括子查詢和聯(lián)合查詢、自身連接查詢問題,本文通過sql語句給大家介紹的非常詳細,需要的朋友可以參考下
    2022-02-02
  • 利用Java如何實現(xiàn)將二維數(shù)組轉(zhuǎn)化為鏈式儲存

    利用Java如何實現(xiàn)將二維數(shù)組轉(zhuǎn)化為鏈式儲存

    鏈式結(jié)構(gòu)不要求邏輯上相鄰的節(jié)點在物理位置上也相鄰,節(jié)點間的邏輯關(guān)系是由附加的指針字段表示的,通常借助于程序設(shè)計中的指針結(jié)構(gòu)來實現(xiàn),這篇文章主要給大家介紹了關(guān)于利用Java如何實現(xiàn)將二維數(shù)組轉(zhuǎn)化為鏈式儲存的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • SpringBoot加載靜態(tài)資源的方式

    SpringBoot加載靜態(tài)資源的方式

    本篇文章主要介紹了SpringBoot加載靜態(tài)資源的方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Java大文件分片上傳超詳細教程(minio版)

    Java大文件分片上傳超詳細教程(minio版)

    Minio是一個開源的分布式對象存儲系統(tǒng),它允許用戶在存儲服務(wù)上存儲和檢索數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Java大文件分片上傳(minio版)的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-08-08
  • PageHelper插件實現(xiàn)一對多查詢時的分頁問題

    PageHelper插件實現(xiàn)一對多查詢時的分頁問題

    這篇文章主要介紹了PageHelper插件實現(xiàn)一對多查詢時的分頁問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • Java實現(xiàn)注冊登錄跳轉(zhuǎn)

    Java實現(xiàn)注冊登錄跳轉(zhuǎn)

    這篇文章主要為大家詳細介紹了Java實現(xiàn)注冊登錄跳轉(zhuǎn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 詳解SpringBoot如何優(yōu)雅的進行全局異常處理

    詳解SpringBoot如何優(yōu)雅的進行全局異常處理

    在SpringBoot的開發(fā)中,為了提高程序運行的魯棒性,我們經(jīng)常需要對各種程序異常進行處理,但是如果在每個出異常的地方進行單獨處理的話,這會引入大量業(yè)務(wù)不相關(guān)的異常處理代碼,這篇文章帶大家了解一下如何優(yōu)雅的進行全局異常處理
    2023-07-07

最新評論

慈利县| 台江县| 繁昌县| 兰州市| 额济纳旗| 新龙县| 伊吾县| 萝北县| 蒙阴县| 扶风县| 塔城市| 隆昌县| 年辖:市辖区| 吐鲁番市| 牙克石市| 安福县| 宜都市| 巫山县| 丹巴县| 邹平县| 射洪县| 京山县| 监利县| 抚松县| 永胜县| 子长县| 灵璧县| 应用必备| 礼泉县| 泰安市| 卓资县| 嘉峪关市| 佛教| 河源市| 安化县| 达日县| 深圳市| 汉中市| 阳原县| 大田县| 定州市|