各版本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>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在yml配置文件中,配置Lettuce连接池的方式如下: ```yaml spring: redis: host: localhost # Redis服务器地址 port: 6379 # Redis服务器端口 password: password # Redis密码,如果没有设置可以省略 database: 0 # Redis数据库编号,默认为0 lettuce: pool: max-active: 8 # 连接池最大连接数,默认为8 max-idle: 8 # 连接池最大空闲连接数,默认为8 min-idle: 0 # 连接池最小空闲连接数,默认为0 max-wait: -1ms # 连接池获取连接的最大等待时间,默认为-1ms(无限等待) time-between-eviction-runs: -1ms # 连接池空闲连接的检测周期时间,默认为-1ms(不检测) min-evictable-idle-time: 1800000ms # 连接池中连接的最小空闲时间,超过此时间的连接将被回收,默认为1800000ms(30分钟) validation-query: "SELECT 1" # 连接池检测连接有效性的SQL语句,默认为null ``` 在以上配置中,`lettuce`属性用于配置Lettuce连接池相关的属性,`pool`属性用于配置连接池相关的属性,具体含义和默认值在注释中已经说明。 ### 回答2: YML配置是一种常用的配置方式,可以通过YML文件来配置Lettuce连接池Lettuce是一种高性能的Redis客户端,可以与Redis服务器进行交互。 首先,在YML文件中配置Lettuce连接池的相关参数。以下是一份示例配置: ```yml spring: redis: lettuce: pool: max-active: 8 # 最大连接数 max-wait: -1 # 最大等待时间,-1表示无限制 max-idle: 8 # 最小空闲连接数 min-idle: 0 # 最大空闲连接数 timeout: 5000 # 连接超时时间(单位:毫秒) ``` 在这个示例中,我们配置Lettuce连接池的一些常用参数。`max-active`表示最大连接数,即连接池能够同时支持的最大连接数。`max-wait`表示最大等待时间,当连接池达到最大连接数时,后续的连接请求将会等待,直到有连接被释放或者到达最大等待时间为止。`max-idle`表示最小空闲连接数,即连接池中保持的最小空闲连接数。`min-idle`表示最大空闲连接数,即连接池中保持的最大空闲连接数。`timeout`表示连接超时时间,即在连接Redis服务器时的超时时间。 在配置完成后,可以通过`LettucePoolingClientConfiguration`类来获取Lettuce连接工厂实例,并将该实例作为参数传递给`LettuceConnectionFactory`对象。通过这种方式,我们可以将YML配置连接池参数应用到Lettuce连接池中。 总结起来,通过YML配置可以很方便地配置Lettuce连接池的相关参数,使得我们能够灵活地控制连接池的大小、空闲连接数以及超时时间等。这样就可以确保与Redis服务器进行连接时的性能和稳定性。 ### 回答3: YML (YAML) 是一种用于配置文件的格式,它易于阅读和编写。在使用Lettuce配置连接池时,可以使用YML格式的配置文件来定义连接池的参数和属性。 下面是一个YML配置文件的示例,用于配置Lettuce连接池: ```yml spring: redis: host: localhost # Redis服务器主机名 port: 6379 # Redis服务器端口号 password: # Redis服务器密码 lettuce: pool: max-idle: 10 # 最大空闲连接数 min-idle: 5 # 最小空闲连接数 max-active: 20 # 最大活跃连接数 max-wait: -1 # 等待连接超时时间(-1表示无限等待) ``` 在这个配置文件中,我们使用了`spring.redis`前缀来定义连接Redis的相关属性。`host`属性定义了Redis服务器的主机名,`port`属性定义了Redis服务器的端口号,`password`属性是Redis服务器的密码(如果有的话)。 在`lettuce`下面的`pool`部分,我们可以定义连接池的一些属性。`max-idle`表示最大空闲连接数,`min-idle`表示最小空闲连接数,`max-active`表示最大活跃连接数。`max-wait`表示等待连接超时的时间,如果设置为-1则表示无限等待。 通过这个YML配置文件,我们可以定义Lettuce连接池的各种参数,从而实现对连接池的灵活配置
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值