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;
}
}

Lettuce连接池——解决“MXBean already registered with name org.apache.commons.pool2:type=GenericObjectPool,name=pool”的更多相关文章

  1. Java--对象池化技术 org.apache.commons.pool2.ObjectPool

    org.apache.commons.pool2.ObjectPool提供了对象池,开发的小伙伴们可以直接使用来构建一个对象池 使用该对象池具有两个简单的步骤: 1.创建对象工厂,org.apache ...

  2. springboot使用lettuce连接池

    springboot对连接池的使用非常智能,配置文件中添加lettuce.pool相关配置,则会使用到lettuce连接池,并将相关配置设置为连接池相关参数,(前提是这些参数是springboot配置 ...

  3. SpringBoot 基于lettuce 连接池 配置redis多数据源操作 生产配置

    添加pom<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons- ...

  4. redis连接池 jedis-2.9.0.jar+commons-pool2-2.4.2.jar

    java使用Redis连接池  jar包为 jedis-2.9.0.jar+commons-pool2-2.4.2.jar jar下载地址 package com.test; import redis ...

  5. jedis2.4.2连接池配置说明

    大多数情况使用jedis时用的都是默认配置,但有的时候为了调优,提供应用程序的性能,那我们就要知道jedis中参数的意义. JedisPoolConfig config = new JedisPool ...

  6. jedis连接池参数minEvictableIdleTimeMillis和softMinEvictableIdleTimeMillis探索

    我们通常在使用JedisPoolConfig进行连接池配置的时候,minEvictableIdleTimeMillis和softMinEvictableIdleTimeMillis这两个参数经常会不懂 ...

  7. FTP连接池

    我们项目使用的是 Apache的(commons-net-3.2.jar) FTPClient,但是系统偶尔会有异常,趁着刚解决完,总结一下. 日志中提示是类似 java.lang.Exception ...

  8. grpc(三)之grpc客户端使用连接池

    本文使用commons-pool2来实现连接池应用 1.定义一个产生连接池的工厂,需要继承BasePooledObjectFactory,其用处是生产和销毁连接池中保存的对象.根据需求,现在池子里保存 ...

  9. common-pool2 学习:thrift连接池的另一种实现

    对象池是一种很实用的技术,经典的例子就是数据库连接池.去年曾经从零开始写过一个thrift客户端连接池.如果不想重造轮子,可以直接在apache开源项目commons-pool的基础上开发. 步骤: ...

随机推荐

  1. SQL 批量添加的语法

    .--添加一条记录 . insert into tableName(col1,col2,col3) values (val1,val2,val3) .--添加多条记录 . insert into ta ...

  2. 卸载pkg安装包

    1 基本原理 1.1 查看某个pkg安装包安装的所有文件 第一,查看所有安装的pkg $ pkgutil --pkgs 第二,查看指定pkg的所有安装文件 $ pkgutil --files the- ...

  3. Spring(十一)-- Spring代理生成器

    Spring代理生成器 1.创建需要的dao接口 2.创建需要的daoImpl实现类 3.创建前置增强类 4.创建spring.xml文件 <!-- 这个案例 需要解决的问题: 一个代理工厂 配 ...

  4. git diff 命令介绍

    https://www.jianshu.com/p/6e1f7198e76a https://www.jianshu.com/p/5b6a014ac3db https://blog.csdn.net/ ...

  5. Windows下使用SVN版本控制工具

    一.SVN工作原理 SVN(SubVersion)的基本工作思路是这样的:在一台服务器上建立一个源代码库,库里可以存放许多不同项目的源程序,由源代码库管理员统一管理这些源程序. 每个用户在使用源代码库 ...

  6. python PEP8常用规范(看完你会感谢我的!)

    完整的规范移步传送门 pep8规范 官方文档:[https://www.python.org/dev/peps/pep-0008/](https://www.python.org/dev/peps/p ...

  7. Django创建数据库常用字段及参数

    Django创建数据库常用字段及参数 常用字段 1.models.AutoField 自增列= int(11) 如果没有的话,默认会生成一个名称为 id 的列,如果要显示的自定义一个自增列,必须将给列 ...

  8. CentOS7 安装ffmpeg

    安装EPEL Release,因为安装需要使用其他的repo源,所以需要EPEL支持:yum install -y epel-release#如果出现缺少Code提示,可以: sudo rpm --i ...

  9. expdp使用

    原文:https://blog.csdn.net/zftang/article/details/6387325 ORACLE EXPDP命令使用详细相关参数以及导出示例: 1. DIRECTORY指定 ...

  10. kill指定用户所有进程

    在linux系统管理中,我们有时候需要kill某个用户的所有进程,这里有以下几种方法,以heboan用为例 pkill方式 pkill -u heboan killall方式 killall -u h ...