1. 程式人生 > >各版本lettuce sentinel spring整合流程(連線池、哨兵配置)

各版本lettuce sentinel spring整合流程(連線池、哨兵配置)

spring-data-2與其上一個版本1.8是一個分水嶺,2.0用的是io.lettuce:lettuce-core,2.0之前的spring-data用的是biz.paluch.redis:lettuce

spring-data-2.0以上版本配置

spring-data-redis版本2.0.9.RELEASE
io.lettuce:lettuce-core版本5.0.4.RELEASE

依賴:

    <!-- https://mvnrepository.com/artifact/io.lettuce/lettuce-core -->
    <dependency>
<groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>5.0.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis --> <dependency> <groupId
>
org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.0.9.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 --> <dependency> <groupId>
org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.0</version> </dependency>

最終目標為:StringRedisTemplate或者RedisTemplate

需要 : RedisConnectionFactory

其實現為 : LettuceConnectionFactory

因實現sentinel,所以需要構造引數:

1、RedisSentinelConfiguration,其配置sentinel-master和ip:host
2、LettuceClientConfiguration,可其配置連線池以及ssl等相關引數(使用其子介面LettucePoolingClientConfiguration)

LettucePoolingClientConfiguration.可通過static方法使用builder模式建立:

LettucePoolingClientConfiguration.builder().poolConfig(pool).build();

pool型別GenericObjectPoolConfig為common-pool2執行緒池

實現程式碼:

bean配置(連線池,spring-template等)

/**
 * @author wkCae
 */
@Configuration
@PropertySource(value = {"classpath:redis.properties"})
@ComponentScan(basePackages = "com")
public class RedisClientConfiguration {

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
        //todo 定製化配置
        return stringRedisTemplate;
    }

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //todo 定製化配置
        return redisTemplate;
    }

    @Bean
    public LettuceConnectionFactory lettuceConnectionFactory(RedisSentinelConfiguration redisSentinelConfiguration, LettuceClientConfiguration lettuceClientConfiguration) {
        return new LettuceConnectionFactory(redisSentinelConfiguration, lettuceClientConfiguration);
    }

    /**
     * 配置哨兵叢集資訊 master和host:ip
     * @param sentinelProperties  叢集Properties
     * @return redisSentinelConfiguration
     */
    @Bean
    public RedisSentinelConfiguration redisSentinelConfiguration(SentinelProperties sentinelProperties) {
        return new RedisSentinelConfiguration(sentinelProperties.getMaster(), sentinelProperties.getHosts());
    }

    /**
     * 配置LettuceClientConfiguration 包括執行緒池配置和安全項配置
     * @param genericObjectPoolConfig common-pool2執行緒池
     * @return lettuceClientConfiguration
     */
    @Bean
    public LettuceClientConfiguration lettuceClientConfiguration(GenericObjectPoolConfig genericObjectPoolConfig) {
        return LettucePoolingClientConfiguration.builder()
                .poolConfig(genericObjectPoolConfig)
                .build();
    }

    @Bean
    public GenericObjectPoolConfig genericObjectPoolConfig(CommonPool2Properties commonPool2Properties) {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxIdle(commonPool2Properties.getMaxIdle());
        poolConfig.setMinIdle(commonPool2Properties.getMinIdle());
        poolConfig.setMaxTotal(commonPool2Properties.getMaxTotal());
        //todo 其他配置
        return poolConfig;
    }
}

哨兵資訊配置

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author wkCae
 */
@Component
public class SentinelProperties {
    @Value("${lettuce.sentinel.master}")
    private String master;
    @Value("${lettuce.sentinel.nodes}")
    private String nodes;

    private Set<String> hosts;

    @PostConstruct
    public void hosts() {
        hosts = new HashSet<>();
        hosts.addAll(Arrays.asList(nodes.split(",")));
    }

    public String getMaster() {
        return master;
    }

    public void setMaster(String master) {
        this.master = master;
    }

    public String getNodes() {
        return nodes;
    }

    public void setNodes(String nodes) {
        this.nodes = nodes;
    }

    public Set<String> getHosts() {
        return hosts;
    }

    public void setHosts(Set<String> hosts) {
        this.hosts = hosts;
    }
}

連線池資訊配置

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
public class CommonPool2Properties {
    @Value("${lettuce.pool.maxTotal}")
    private Integer maxTotal;

    @Value("${lettuce.pool.maxIdle}")
    private Integer maxIdle;

    @Value("${lettuce.pool.minIdle}")
    private Integer minIdle;

    //TODO 其他屬性

    public Integer getMaxTotal() {
        return maxTotal;
    }

    public void setMaxTotal(Integer maxTotal) {
        this.maxTotal = maxTotal;
    }

    public Integer getMaxIdle() {
        return maxIdle;
    }

    public void setMaxIdle(Integer maxIdle) {
        this.maxIdle = maxIdle;
    }

    public Integer getMinIdle() {
        return minIdle;
    }

    public void setMinIdle(Integer minIdle) {
        this.minIdle = minIdle;
    }
}

redis.properties

lettuce.sentinel.master=mymaster
lettuce.sentinel.nodes=127.0.0.1:63791,127.0.0.1:63792,127.0.0.1:63793
lettuce.sentinel.node1=127.0.0.1:63791
lettuce.sentinel.node2=127.0.0.1:63792
lettuce.sentinel.node3=127.0.0.1:63793


lettuce.pool.maxTotal=5
lettuce.pool.maxIdle=2
lettuce.pool.minIdle=2
# todo 其他執行緒池配置

spring-data-2.0以下版本配置

spring-data-redis版本1.8.7.RELEASE
io.lettuce:lettuce-core版本4.2.2.RELEASE

依賴:

    <!-- https://mvnrepository.com/artifact/biz.paluch.redis/lettuce -->
    <dependency>
      <groupId>biz.paluch.redis</groupId>
      <artifactId>lettuce</artifactId>
      <version>4.2.2.Final</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.8.7.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
      <version>2.6.0</version>
    </dependency>

最終目標為:StringRedisTemplate或者RedisTemplate

需要 : RedisConnectionFactory

其實現為 : LettuceConnectionFactory

需要構造引數:

LettucePool介面,其預設實現為DefaultLettucePool。

DefaultLettucePool可接收RedisSentinelConfiguration型別構造引數,然後可以為其設定GenericObjectPoolConfig用於配置common-pool2的連線池。

spring-data-2.0.x與之前版本還有一個區別就是ssl等配置放到了LettuceConnectionFactory中。

直接xml實現完整配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--引入redis配置檔案 注意spring不予許多處引入properties檔案,找到專案中引入properties檔案的位置,將此配置合併進去,或者通過逗號分隔在相應位置引入多檔案-->
    <context:property-placeholder location="classpath*:redis.properties"/>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="lettuceConnectionFactory"/>
        <!-- 其他配置 -->
    </bean>

    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="lettuceConnectionFactory"/>
        <!-- 其他配置 -->
    </bean>

    <!--redisConnectionFactory-->
    <bean id="lettuceConnectionFactory" class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory">
        <constructor-arg name="pool" ref="defaultLettucePool"/>
    </bean>

    <!--連線池配置-->
    <bean id="defaultLettucePool" class="org.springframework.data.redis.connection.lettuce.DefaultLettucePool">
        <constructor-arg name="sentinelConfiguration" ref="redisSentinelConfiguration"/>
        <property name="poolConfig" ref="genericObjectPoolConfig"/>
    </bean>

    <!--哨兵叢集資訊配置-->
    <bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <constructor-arg name="master" value="${lettuce.sentinel.master}"/>
        <constructor-arg name="sentinelHostAndPorts">
            <set>
                <value type="java.lang.String">${lettuce.sentinel.node1}</value>
                <value type="java.lang.String">${lettuce.sentinel.node2}</value>
                <value type="java.lang.String">${lettuce.sentinel.node3}</value>
            </set>
        </constructor-arg>
    </bean>
    <!--common-pool2執行緒池配置-->
    <bean id="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
        <property name="maxIdle" value="${lettuce.pool.maxIdle}"/>
        <property name="minIdle" value="${lettuce.pool.minIdle}"/>
        <property name="maxTotal" value="${lettuce.pool.maxTotal}"/>
        <!-- 其他相關配置 -->
    </bean>

</beans>