SpringBoot 集成 Codis: 使用 CodisConnectionFactory 配置集群
要使用 SpringBoot 的 CodisConnectionFactory 配置 Codis 集群,需要进行以下步骤:
- 在 pom.xml 文件中添加以下依赖:
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-jmx</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.sohu</groupId>
<artifactId>tv.jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.taobao.metamorphosis</groupId>
<artifactId>metamorphosis-client</artifactId>
<version>1.4.5</version>
</dependency>
- 创建一个 CodisConnectionFactory 类,继承自 JedisConnectionFactory,并实现 ClusterCodisConnectionFactory 接口:
import com.sohu.tv.jedis.CodisConnectionFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.data.redis.JedisClientConfigurationBuilderCustomizer;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Cluster;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Jedis;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Ssl;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.X509;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Pool;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Ssl;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Cluster;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Replica;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Sentinel;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.ClientResources;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Cluster.Refresh;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Ssl;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.X509;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Cluster;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Jedis;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Ssl;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.X509;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Pool;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Ssl;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Cluster;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Replica;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Sentinel;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.ClientResources;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Cluster.Refresh;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Ssl;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.X509;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Pool;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Ssl;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Cluster;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Replica;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Sentinel;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.ClientResources;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Cluster.Refresh;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Ssl;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.X509;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Pool;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Ssl;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Cluster;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Replica;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Sentinel;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.ClientResources;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Lettuce.Cluster.Refresh;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Ssl;
import redis.clients.jedis.JedisPoolConfig;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
@ConditionalOnClass(CodisConnectionFactory.class)
public class CodisConnectionFactory extends JedisConnectionFactory implements ClusterCodisConnectionFactory {
public CodisConnectionFactory() {
super();
}
public CodisConnectionFactory(JedisPoolConfig poolConfig, String host, int port) {
super(poolConfig);
setHostName(host);
setPort(port);
}
@Override
public void setClusterNodes(List<String> clusterNodes) {
RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration(clusterNodes);
setClusterConfiguration(clusterConfiguration);
}
@Override
public void setPassword(String password) {
// 设置密码
RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
standaloneConfiguration.setPassword(RedisPassword.of(password));
setStandaloneConfiguration(standaloneConfiguration);
}
@Override
public void setDatabase(int database) {
RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
standaloneConfiguration.setDatabase(database);
setStandaloneConfiguration(standaloneConfiguration);
}
@Override
public void setMaxRedirects(int maxRedirects) {
RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration();
clusterConfiguration.setMaxRedirects(maxRedirects);
setClusterConfiguration(clusterConfiguration);
}
}
- 在 application.properties 文件中添加以下配置:
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
spring.redis.timeout=10000
spring.redis.lettuce.shutdown-timeout=10000
spring.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381
spring.redis.cluster.max-redirects=3
- 在启动类上添加
@EnableRedisRepositories注解:
@SpringBootApplication
@EnableRedisRepositories
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
现在,你可以在其他类中注入 CodisConnectionFactory 来使用 Codis 集群了。
原文地址: https://www.cveoy.top/t/topic/pcPH 著作权归作者所有。请勿转载和采集!