首頁  >  文章  >  Java  >  深度解析springboot如何配置多個redis連接

深度解析springboot如何配置多個redis連接

Y2J
Y2J原創
2017-05-04 10:18:303809瀏覽

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是一個快取,訊息中間件及具有豐富特性的鍵值儲存系統。 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的情況,它是根據bean的名稱'redisTemplate'而不是它的類型進行排除的)。如果在classpath路徑下存在commons-pool2,預設你會獲得一個連接池工廠。

1.3 建立多個redis連線

使用redis的預設設定可以連線到redis中的0程式庫中,如果指定程式庫連線需要設定indexdb,同時如果需要連接多個redis服務,也需要同時配置多個資料來源

1.3.1、application.yml 檔案中增加:

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

1.3.2、建立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抽象基類,建立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、根據資料來源,建立不同的子類別@Component

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

ps:類別和子類別中同時宣告了getTemple方法和StringRedisTemple屬性,子類別透過重寫父類別的getTeimple方法,把子類別的自己StringRedisTemple 屬性傳給父類,父類別透過子類別傳遞過來的StringRedisTemple使用不通的資料連結來操作快取。至此,父類完成所有的操作方法,而當需要建立一個資料庫連接時,只需要在建立子類,被宣告自己的StringRedisTemple,並傳給父類別即可。

以上是深度解析springboot如何配置多個redis連接的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn