上一篇文章寫了關於叢集搭建的步驟、master節點切換的相關內容, 有了叢集肯定需要程式訪問, 那麼,今天就看一下SpringBoot如何訪問Redis叢集的;
繼續Spring-redis-example框架
由於前面訪問單機版Redis已經寫好了一個maven
工程, 後面對於叢集的訪問也都在該工程上面進行了; 不瞭解的可以移步深入理解Redis之SpringBoot整合Redis;
我們在原來工程的基礎上稍微改造一下, 就能夠同時支援單節點與叢集模式的Redis訪問了.
package com.terrylmay.redis.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "spring.redis.cluster")
@ConditionalOnProperty(name = {"spring.redis.cluster.nodes"})
public class ClusterRedisConfig {
private String nodes;
private String password;
private int maxRedirects;
public String getNodes() {
return nodes;
}
public void setNodes(String nodes) {
this.nodes = nodes;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getMaxRedirects() {
return maxRedirects;
}
public void setMaxRedirects(int maxRedirects) {
this.maxRedirects = maxRedirects;
}
}
新增一個關於叢集資訊的配置類, 方便後面建立JedisConnectionFactory
的時候用到. 修改Application類
package com.terrylmay.redis.example;
import com.terrylmay.redis.example.config.ClusterRedisConfig;
import com.terrylmay.redis.example.config.StandaloneRedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.Arrays;
import java.util.Collections;
@SpringBootApplication(scanBasePackages = {"com.terrylmay.redis.example"})
public class RedisExampleApplication {
public static void main(String[] args) {
SpringApplication.run(RedisExampleApplication.class, args);
}
@Autowired(required = false)
StandaloneRedisConfig standaloneRedisConfig;
@Autowired(required = false)
ClusterRedisConfig clusterRedisConfig;
@Autowired
RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory factory = null;
if (standaloneRedisConfig != null) {
factory = new JedisConnectionFactory(new RedisStandaloneConfiguration(standaloneRedisConfig.getHost(), standaloneRedisConfig.getPort()));
return factory;
}
if (clusterRedisConfig != null) {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList(clusterRedisConfig.getNodes().split(",")));
redisClusterConfiguration.setMaxRedirects(clusterRedisConfig.getMaxRedirects());
redisClusterConfiguration.setPassword(clusterRedisConfig.getPassword());
factory = new JedisConnectionFactory(redisClusterConfiguration, jedisPoolConfig);
}
return factory;
}
@Bean
public StringRedisTemplate stringRedisTemplate() {
return new StringRedisTemplate(redisConnectionFactory);
}
}
這兩個類StandaloneRedisConfig,ClusterRedisConfig
的註解之所以用required=false
是因為這些類的Bean只有在滿足配置檔案中有特定的屬性Key的時候才會生成, 所以對於某一個特定環境, 只可能使用一種Redis模式; 在建立RedisConnectionFactory
根據Bean是否存在, 建立出來不同模式的叢集訪問類;
最後, 對工程進行一下完善, 因為原來是打算把所有模式下的redis
配置資訊放到一個配置檔案中, 然後通過註釋的方式做個演示; 後面發現多建立幾個application-xxx.properties
檔案更加方便;
建立出來的不同模式下的Redis配置資訊檔案如下:
application.properties
裡面只放置當前執行時的spring.profiles.active
資訊
spring.profiles.active=cluster
application-standalone.properties
檔案內容如下:
spring.redis.host=localhost
spring.redis.port=6379
application-cluster.properties
檔案內容如下:
spring.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,
spring.redis.cluster.password=
spring.redis.cluster.max-redirects=12
這樣, 如果測試的時候, 只需要切換application.properties
檔案中的spring.profiles.active
屬性值即可; 可選項有: standalone|cluster|sentinel
程式碼寫完之後, 可以執行一下單元測試看是否能通過; 所有關於該專案的程式碼在Github spring-redis-example倉庫中 歡迎 star & PR