吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1290|回复: 1
收起左侧

[Java 转载] Redis集群配置代码

[复制链接]
username11 发表于 2021-8-27 17:03
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[0])) {
                HOST = "127.0.0.1";
                //测试环境
            } else if ("dev".equals(activeProfiles[0])) {
                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脚本一键配置呢?
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 14:29

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表