username11 发表于 2021-8-27 17:03

Redis集群配置代码

```
package com.llt.fcthcommmon.commonCode1.redis;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;
import java.time.Duration;

@SpringBootConfiguration
public class RedisConfig {
    //RedisConfig日志
    Logger logger = LoggerFactory.getLogger(RedisConfig.class);
    //环境的信息列表。
    @Resource
    Environment environment;

    //构造函数(可以不要)
    public RedisConfig() {
    }

    /**
   * 连接Redis客户端工厂
   * @return
   */
    @Bean
    public LettuceConnectionFactory lettuceConnectionFactory() {
      //默认IP地址
      String HOST = "127.0.0.1";
      //获得当前所有环境
      String[] activeProfiles = this.environment.getActiveProfiles();
      if (activeProfiles.length > 0) {
            //正式环境
            if ("pro".equals(activeProfiles)) {
                HOST = "127.0.0.1";
                //测试环境
            } else if ("dev".equals(activeProfiles)) {
                HOST = "127.0.0.1";
            }
      }

      this.logger.info("RRRRRRRRRRRRR=========Redis地址:" + HOST);
      //创建redis集群配置类
      RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
      //redis集群密码
      redisClusterConfiguration.setPassword("java");
      redisClusterConfiguration.clusterNode(HOST, 6371);
      redisClusterConfiguration.clusterNode(HOST, 6372);
      redisClusterConfiguration.clusterNode(HOST, 6373);
      redisClusterConfiguration.clusterNode(HOST, 6374);
      redisClusterConfiguration.clusterNode(HOST, 6375);
      redisClusterConfiguration.clusterNode(HOST, 6376);
      //创建连接池对象
      GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
      //最大空闲连接数
      genericObjectPoolConfig.setMaxIdle(8);
      //最大连接总数
      genericObjectPoolConfig.setMaxTotal(8);
      //最小控线连接
      genericObjectPoolConfig.setMinIdle(1);
      //最大等待时间,无限制
      genericObjectPoolConfig.setMaxWaitMillis(-1L);
      //执行命令超时时间60S
      LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofSeconds(60L));
      builder.poolConfig(genericObjectPoolConfig);
      LettuceClientConfiguration lettuceClientConfiguration = builder.build();
      LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);
      return lettuceConnectionFactory;
    }

    /**
   *
   * @Param lettuceConnectionFactory
   * @return
   */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory lettuceConnectionFactory) {
      RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
      redisTemplate.setConnectionFactory(lettuceConnectionFactory);
      redisTemplate.setKeySerializer(new StringRedisSerializer());
      return redisTemplate;
    }

    /**
   * redis监听器,redis的key如果过期了会给客户端发送信息(可以不配置)
   * @param lettuceConnectionFactory
   * @return
   */
    @Bean
    RedisMessageListenerContainer container(LettuceConnectionFactory lettuceConnectionFactory) {
      RedisMessageListenerContainer container = new RedisMessageListenerContainer();
      container.setConnectionFactory(lettuceConnectionFactory);
      return container;
    }

}
```

neoyoung 发表于 2021-8-27 22:29

有没有bash脚本一键配置呢?
页: [1]
查看完整版本: Redis集群配置代码