zoukankan      html  css  js  c++  java
  • springboot java方式配置Lettuce连接池

    package com.zsk.springboot.redis.redis;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
    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.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializer;
    
    /**
     * https://blog.csdn.net/qq_36666651/article/details/82226319*/
    @Configuration
    @PropertySource("classpath:redis.properties")
    public class RedisTemplateConfig {
        @Value("${redis.lettuce.pool.maxIdle}")
        private int maxIdle;
        @Value("${redis.lettuce.pool.minIdle}")
        private int minIdle;
        @Value("${redis.lettuce.pool.maxTotal}")
        private int maxTotal;
        @Value("${redis.server.host}")
        private String host;
        @Value("${redis.server.port}")
        private int port;
    
        @Bean
        public GenericObjectPoolConfig genericObjectPoolConfig() {
            GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
            poolConfig.setMaxIdle(maxIdle);
            poolConfig.setMinIdle(minIdle);
            poolConfig.setMaxTotal(maxTotal);
            return poolConfig;
        }
    
        @Bean
        public LettuceClientConfiguration lettuceClientConfiguration(GenericObjectPoolConfig genericObjectPoolConfig) {
            return LettucePoolingClientConfiguration.builder()
                    .poolConfig(genericObjectPoolConfig)
                    .build();
        }
    
        @Bean
        public RedisStandaloneConfiguration redisSentinelConfiguration() {
            RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, port);
            redisStandaloneConfiguration.setDatabase(0);
            return redisStandaloneConfiguration;
        }
    
        @Bean
        public LettuceConnectionFactory lettuceConnectionFactory(RedisStandaloneConfiguration redisSentinelConfiguration, LettuceClientConfiguration lettuceClientConfiguration) {
            return new LettuceConnectionFactory(redisSentinelConfiguration, lettuceClientConfiguration);
        }
    
    
        @Bean(name = "redisTemplate")
        @Primary
        public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
            return getStringStringRedisTemplate(factory);
        }
    
        @Bean(name = "redisTemplate2")
        public RedisTemplate<String, Object> redisTemplate2(RedisConnectionFactory factory) {
            return getObjectRedisTemplate(factory);
        }
    
    
        private RedisTemplate<String, String> getStringStringRedisTemplate(RedisConnectionFactory factory) {
            StringRedisTemplate template = new StringRedisTemplate(factory);
            template.setKeySerializer(RedisSerializer.string());
            template.setValueSerializer(RedisSerializer.string());
            template.setHashKeySerializer(RedisSerializer.string());
            template.setHashValueSerializer(RedisSerializer.string());
            return template;
        }
    
        private RedisTemplate<String, Object> getObjectRedisTemplate(RedisConnectionFactory factory) {
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            template.setConnectionFactory(factory);
            Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = createJackson2JsonRedisSerializer();
            template.setKeySerializer(RedisSerializer.string());
            template.setValueSerializer(jackson2JsonRedisSerializer);
            template.setHashKeySerializer(RedisSerializer.string());
            template.setHashValueSerializer(jackson2JsonRedisSerializer);
            return template;
        }
    
        private Jackson2JsonRedisSerializer<Object> createJackson2JsonRedisSerializer() {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =
                    new Jackson2JsonRedisSerializer<>(Object.class);
            jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
            return jackson2JsonRedisSerializer;
        }
    }
  • 相关阅读:
    python 获取浏览器窗口句柄
    实现远程连接 Win10的Ubuntu子系统下的MySQL数据库
    Postman 测试微信小程序后台接口
    使用Postman获取小程序码时如何解决47001报错
    富文本编辑框比较
    PIL 生成随机验证码图片
    哪里买书合算
    在文件中读取列表功能
    python函数01
    修改文件内容
  • 原文地址:https://www.cnblogs.com/zincredible/p/15418960.html
Copyright © 2011-2022 走看看