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;
            }
        }
  • 相关阅读:
    What is OpenOCD?
    OpenOCD Debug Adapter Configuration
    OpenOCD 0.9.0 release
    ARM DEBUGGER FOR NEARLY ONE DOLLAR
    SW-DP (Serial Wire Debug Port) Analyzer plugin for the Saleae Logic
    SWD Connect/Transfer Source Code
    Dapper Miser implementation of CMSIS-DAP, MC HCK as SWD Adapter
    STM32定时器级联 -- AN2592
    Training JTAG Interface
    JTAG Simplified
  • 原文地址:https://www.cnblogs.com/gaoquanquan/p/11347843.html
Copyright © 2011-2022 走看看