欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解springboot配置多個redis連接

 更新時間:2017年04月19日 11:09:33   作者:十丿四  
Spring Boot為Redis, MongoDB, Elasticsearch, Solr和Gemfire提供自動配置。本文詳細介紹了springboot配置多個redis連接,有興趣的可以了解一下。

一、springboot nosql 簡介

Spring Data提供其他項目,用來幫你使用各種各樣的NoSQL技術,包括MongoDB, Neo4J, Elasticsearch, Solr, Redis,Gemfire, Couchbase和Cassandra。Spring Boot為Redis, MongoDB, Elasticsearch, Solr和Gemfire提供自動配置。你可以充分利用其他項目,但你需要自己配置它們。

1.1、Redis

Redis是一個緩存,消息中間件及具有豐富特性的鍵值存儲系統(tǒng)。Spring Boot為Jedis客戶端庫和由Spring Data Redis提供的基于Jedis客戶端的抽象提供自動配置。 spring-boot-starter-redis 'Starter POM'為收集依賴提供一種便利的方式。
Redis添加maven依賴

   <dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-test</artifactId> 
  <scope>test</scope> 
</dependency> 
<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter</artifactId> 
  <!-- <version>1.3.5.RELEASE</version> --> 
</dependency> 
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons --> 
<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-redis</artifactId> 
  <!-- <version>1.3.6.RELEASE</version> --> 
</dependency> 

1.2連接Redis

你可以注入一個自動配置的RedisConnectionFactory,StringRedisTemplate或普通的跟其他Spring Bean相同的RedisTemplate實例。默認情況下,這個實例將嘗試使用localhost:6379連接Redis服務器。

@Component 
public class MyBean { 
private StringRedisTemplate template; 
@Autowired 
public MyBean(StringRedisTemplate template) { 
this.template = template; 
} 
// ... 
} 

如果你添加一個你自己的任何自動配置類型的@Bean,它將替換默認的(除了RedisTemplate的情況,它是根據(jù)bean的名稱'redisTemplate'而不是它的類型進行排除的)。如果在classpath路徑下存在commons-pool2,默認你會獲得一個連接池工廠。

1.3 建立多個redis連接

使用redis的默認配置可以連接到redis中的0庫中,如果指定庫連接需要配置indexdb,同時如果需要連接多個redis服務,也需要同時配置多個數(shù)據(jù)源

1.3.1、application.yml 文件 中增加:

@Component 
public class MyBean { 
private StringRedisTemplate template; 
@Autowired 
public MyBean(StringRedisTemplate template) { 
this.template = template; 
} 
// ... 
} 

1.3.2、創(chuàng)建redisconfiguration

@Configuration 
public class Redis137_11Configuration { 
 
  @Bean(name = "redis123Template") 
  public StringRedisTemplate redisTemplate( 
      @Value("${redis123.hostName}") String hostName, 
      @Value("${redis123.port}") int port, 
      @Value("${redis123.password}") String password, 
      @Value("${redis123.maxIdle}") int maxIdle, 
      @Value("${redis123.maxTotal}") int maxTotal, 
      @Value("${redis123.index}") int index, 
      @Value("${redis123.maxWaitMillis}") long maxWaitMillis, 
      @Value("${redis123.testOnBorrow}") boolean testOnBorrow) { 
    StringRedisTemplate temple = new StringRedisTemplate(); 
    temple.setConnectionFactory(connectionFactory(hostName, port, password, 
        maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow)); 
 
    return temple; 
  } 
 
  public RedisConnectionFactory connectionFactory(String hostName, int port, 
      String password, int maxIdle, int maxTotal, int index, 
      long maxWaitMillis, boolean testOnBorrow) { 
    JedisConnectionFactory jedis = new JedisConnectionFactory(); 
    jedis.setHostName(hostName); 
    jedis.setPort(port); 
    if (!StringUtils.isEmpty(password)) { 
      jedis.setPassword(password); 
    } 
    if (index != 0) { 
      jedis.setDatabase(index); 
    } 
    jedis.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis, 
        testOnBorrow)); 
    // 初始化連接pool 
    jedis.afterPropertiesSet(); 
    RedisConnectionFactory factory = jedis; 
 
    return factory; 
  } 
 
  public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, 
      long maxWaitMillis, boolean testOnBorrow) { 
    JedisPoolConfig poolCofig = new JedisPoolConfig(); 
    poolCofig.setMaxIdle(maxIdle); 
    poolCofig.setMaxTotal(maxTotal); 
    poolCofig.setMaxWaitMillis(maxWaitMillis); 
    poolCofig.setTestOnBorrow(testOnBorrow); 
    return poolCofig; 
  } 
} 

1.3.3、聲明redis抽象基類,創(chuàng)建redis的操作方法

public abstract class AbRedisConfiguration { 
  protected StringRedisTemplate temple; 
 
  public void setData(String key, String value) { 
    getTemple().opsForValue().set(key, value); 
  } 
 
  public String getData(String key) { 
    return getTemple().opsForValue().get(key); 
  } 
 
  public StringRedisTemplate getTemple() { 
    return temple; 
  } 
} 

1.3.4、根據(jù)數(shù)據(jù)源,創(chuàng)建不同的子類@Component

public class Redis123 extends AbRedisConfiguration { 
 
  @Resource(name = "redis123Template") 
  private StringRedisTemplate temple; 
 
  public StringRedisTemplate getTemple() { 
    return temple; 
  } 
} 

ps:類和子類中同時聲明了getTemple方法和 StringRedisTemple屬性,子類通過重寫父類的getTeimple方法,把子類的自己StringRedisTemple 屬性 傳給 父類,父類通過子類傳遞過來的StringRedisTemple使用不通的數(shù)據(jù)鏈接來操作緩存。至此,父類完成所有的操作方法,而當需要創(chuàng)建一個數(shù)據(jù)庫連接時,只需要在創(chuàng)建一個子類,被聲明自己的StringRedisTemple,并傳給父類即可。

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

相關文章

  • 關于springcloud集成nacos遇到的問題

    關于springcloud集成nacos遇到的問題

    這篇文章主要介紹了關于springcloud集成nacos遇到的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • JavaWeb實現(xiàn)文件上傳下載功能實例解析

    JavaWeb實現(xiàn)文件上傳下載功能實例解析

    這篇文章主要為大家詳細介紹了JavaWeb中的文件上傳和下載功能的實現(xiàn),在Web應用系統(tǒng)開發(fā)中,文件上傳和下載功能是非常常用的功能,需要的朋友可以參考下
    2015-08-08
  • 在Spring MVC中處理請求參數(shù)的方法總結

    在Spring MVC中處理請求參數(shù)的方法總結

    在Spring MVC中處理請求參數(shù)是通過使用各種注解來實現(xiàn)的,本文給大家介紹了在Spring MVC中處理不同類型請求參數(shù)的方法,并通過代碼講解的非常詳細,需要的朋友可以參考下
    2024-08-08
  • Java實現(xiàn)多個wav文件合成一個的方法示例

    Java實現(xiàn)多個wav文件合成一個的方法示例

    這篇文章主要介紹了Java實現(xiàn)多個wav文件合成一個的方法,涉及java文件流讀寫、編碼轉(zhuǎn)換、解析等相關操作技巧,需要的朋友可以參考下
    2019-05-05
  • IDEA性能優(yōu)化設置(解決卡頓問題)

    IDEA性能優(yōu)化設置(解決卡頓問題)

    在我們?nèi)粘J褂肐DEA進行開發(fā)時,可能會遇到許多卡頓的瞬間,本文主要介紹了IDEA性能優(yōu)化設置,非常具有實用價值,需要的朋友可以參考下
    2023-05-05
  • SpringBoot集成QQ第三方登陸的實現(xiàn)

    SpringBoot集成QQ第三方登陸的實現(xiàn)

    這篇文章主要介紹了SpringBoot集成QQ第三方登陸的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Java開發(fā)崗位面試被問到泛型怎么辦

    Java開發(fā)崗位面試被問到泛型怎么辦

    泛型在java中有很重要的地位,在面向?qū)ο缶幊碳案鞣N設計模式中有非常廣泛的應用。java泛型知識點也是Java開發(fā)崗位必問的一個話題,今天小編就給大家普及下Java泛型常見面試題,感興趣的朋友一起看看吧
    2021-07-07
  • java實現(xiàn)超市庫存管理系統(tǒng)

    java實現(xiàn)超市庫存管理系統(tǒng)

    這篇文章主要為大家詳細介紹了java實現(xiàn)超市庫存管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • 快速了解Java中ThreadLocal類

    快速了解Java中ThreadLocal類

    這篇文章主要介紹了快速了解Java中ThreadLocal類,介紹了ThreadLocal 是什么,ThreadLocal的作用,ThreadLocal 原理等相關內(nèi)容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Java SpringBoot啟動指定profile的8種方式詳解

    Java SpringBoot啟動指定profile的8種方式詳解

    這篇文章主要介紹了spring boot 如何指定profile啟動的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評論