各版本lettuce sentinel spring集成流程(连接池、哨兵配置)

https://blog.csdn.net/qq_36666651/article/details/82226319

 

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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
最终目标为:StringRedisTemplate或者RedisTemplate

需要 : RedisConnectionFactory

其实现为 : LettuceConnectionFactory

因实现sentinel,所以需要构造参数:

1、RedisSentinelConfiguration,其配置sentinel-master和ip:host 
2、LettuceClientConfiguration,可其配置连接池以及ssl等相关参数(使用其子接口LettucePoolingClientConfiguration)

LettucePoolingClientConfiguration.可通过static方法使用builder模式创建:

LettucePoolingClientConfiguration.builder().poolConfig(pool).build();
1
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;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
哨兵信息配置

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;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
连接池信息配置

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;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 其他线程池配置
1
2
3
4
5
6
7
8
9
10
11
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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
最终目标为: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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
北京出了一位股市怪才,炒股2年他只靠1不败铁律,赚爆了
东正金融 · 燨燚
--------------------- 
作者:wkCaeser_ 
来源:CSDN 
原文:https://blog.csdn.net/qq_36666651/article/details/82226319 
版权声明:本文为博主原创文章,转载请附上博文链接!

展开阅读全文

没有更多推荐了,返回首页