zoukankan      html  css  js  c++  java
  • Lettuce连接池——解决“MXBean already registered with name org.apache.commons.pool2:type=GenericObjectPool,name=pool”

    LettuceConfig:
    package com.youdao.outfox.interflow.config;
    
    import io.lettuce.core.support.ConnectionPoolSupport;
    import io.lettuce.core.RedisURI;
    import io.lettuce.core.cluster.RedisClusterClient;
    import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
    import org.apache.commons.pool2.impl.GenericObjectPool;
    import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    @Configuration
    @ConfigurationProperties(prefix = "redis")
    public class LettuceConfig {
        private List<String> nodes;
    
        public List<String> getNodes() {
            return nodes;
        }
    
        public void setNodes(List<String> nodes) {
            this.nodes = nodes;
        }
    
        @Bean
        public RedisClusterClient redisClient() {
            List<RedisURI> uriList = new ArrayList<>();
            nodes.forEach(node -> {
                String[] addrStr = node.split(":");
                String host = addrStr[0];
                int port = Integer.parseInt(addrStr[1]);
    
                RedisURI redisUri = RedisURI.Builder.redis(host).withPort(port).build();
                uriList.add(redisUri);
            });
            RedisClusterClient redisClient = RedisClusterClient.create(uriList);
            return redisClient;
        }
    
        @Bean
        public GenericObjectPoolConfig genericObjectPoolConfig() {
            GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
            genericObjectPoolConfig.setJmxEnabled(false);
            return genericObjectPoolConfig;
        }
    
        @Bean(name = "redisConnectionPool")
        public GenericObjectPool<StatefulRedisClusterConnection<String, String>> redisConnectionPool(
                RedisClusterClient redisClient, GenericObjectPoolConfig genericObjectPoolConfig) {
            GenericObjectPool<StatefulRedisClusterConnection<String, String>> redisPool = ConnectionPoolSupport.
                    createGenericObjectPool(() -> redisClient.connect(), genericObjectPoolConfig);
            return redisPool;
        }
    
    }

    这里的bean GenericObjectPoolConfig中有一个genericObjectPoolConfig.setJmxEnabled(false); 关掉监控 这个异常就不会抛出了~

    还有要注意 在每次使用完连接之后 要调用close()或者使用try(){}的方式,会自动调用close(),很优雅~

    eg:

        @Override
        public <T> T get(String key, Class<T> clazz) {
            try (StatefulRedisClusterConnection connect = redisPool.borrowObject()){
                RedisAdvancedClusterCommands<String, String> redisCommands = connect.sync();
                String value = redisCommands.get(key);
                return JSONObject.parseObject(value, clazz);
            } catch (Exception e) {
                logger.warn("Redis get() failed! key=" + key, e);
                return null;
            }
        }
  • 相关阅读:
    Java-使用IO流对大文件进行分割和分割后的合并
    Java-单向链表算法
    Java-二分查找算法
    Java-二叉树算法
    Java-对象比较器
    Android中Activity的四种开发模式
    Struts2工作原理
    C++实现单例模式
    数组中有一个数字出现的次数超过数组的一半,请找出这个数字
    c++ enum用法【转】
  • 原文地址:https://www.cnblogs.com/gaoquanquan/p/11347843.html
Copyright © 2011-2022 走看看