redisTemplete config 常用配置

redisTemplete config 常用配置,第1张

redisTemplete config 常用配置

package cn.goswan.orient.common.data.cache;

import io.lettuce.core.ReadFrom;
import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
import lombok.AllArgsConstructor;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
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.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


// @EnableCaching TODO
@Configuration
@AllArgsConstructor
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisTemplateConfig {



	@Bean
	public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {

		RedisTemplate redisTemplate = new RedisTemplate<>();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
		redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
		redisTemplate.setConnectionFactory(redisConnectionFactory);
		return redisTemplate;
	}

	@Bean
	public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {

		StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
		stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
		stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
		return stringRedisTemplate;
	}


	@Autowired
	RedisProperties redisProperties;

	@Bean
	public GenericObjectPoolConfig poolConfig() {
		GenericObjectPoolConfig config = new GenericObjectPoolConfig();
		config.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());
		config.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());
		config.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());
		config.setMaxWaitMillis(redisProperties.getLettuce().getPool().getMaxWait().toMillis());
		return config;
	}


	@Bean
	public RedisSentinelConfiguration configuration() {
		RedisSentinelConfiguration redisConfig = new RedisSentinelConfiguration();
		redisConfig.setMaster(redisProperties.getSentinel().getMaster());
		if(redisProperties.getSentinel().getNodes()!=null) {
			List sentinelNode=new ArrayList();
			for(String sen : redisProperties.getSentinel().getNodes()) {
				String[] arr = sen.split(":");
				sentinelNode.add(new RedisNode(arr[0],Integer.parseInt(arr[1])));
			}
			redisConfig.setDatabase(redisProperties.getDatabase());
			redisConfig.setPassword(redisProperties.getPassword());
			redisConfig.setSentinelPassword(redisConfig.getPassword());
			redisConfig.setSentinels(sentinelNode);
		}
		return redisConfig;
	}

    @Bean
	public RedisConnectionFactory redisConnectionFactory(@Qualifier("poolConfig") GenericObjectPoolConfig config,
															 @Qualifier("configuration") RedisSentinelConfiguration redisConfig) {//注意传入对象名和类型RedisSentinelConfiguration
		LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
		return new LettuceConnectionFactory(redisConfig, clientConfiguration);
	}



	@Bean
	@ConditionalOnProperty(value = "spring.redis.cluster.enable",havingValue = "true")
	public RedisConnectionFactory redisConnectionFactory(RedisProperties redisProperties) {
		RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(redisProperties.getCluster().getNodes());

		ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
				.enablePeriodicRefresh()
				.enableAllAdaptiveRefreshTriggers()
				.refreshPeriod(Duration.ofSeconds(5))
				.build();

		ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder()
				.topologyRefreshOptions(clusterTopologyRefreshOptions).build();

		LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder()
				.readFrom(ReadFrom.REPLICA_PREFERRED)
				.clientOptions(clusterClientOptions).build();
		redisClusterConfiguration.setPassword(redisProperties.getPassword());
		return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5694668.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存