当前位置: 首页 > news >正文

成都微信微网站建设室内设计的网站

成都微信微网站建设,室内设计的网站,广州11个区排名,网站鼠标移上去显示层项目场景#xff1a; Spring Boot集成Redis集群#xff0c;使用lettuce连接Cluster集群实例。 问题描述 redis其中一个节点挂了之后#xff0c;springboot集成redis集群配置信息没有及时刷新#xff0c;出现读取操作报错。 java.lang.IllegalArgumentException: Connec…项目场景 Spring Boot集成Redis集群使用lettuce连接Cluster集群实例。 问题描述 redis其中一个节点挂了之后springboot集成redis集群配置信息没有及时刷新出现读取操作报错。 java.lang.IllegalArgumentException: Connection to 127.0.0.1:6379 not allowed. This connection point is not known in the cluster view exceptionStackTrace io.lettuce.core.cluster.PooledClusterConnectionProvider.getConnectionAsync(PooledClusterConnectionProvider.java:359) io.lettuce.core.cluster.ClusterDistributionChannelWriter.write(ClusterDistributionChannelWriter.java:93) io.lettuce.core.cluster.ClusterCommand.complete(ClusterCommand.java:56) io.lettuce.core.protocol.CommandHandler.decode(CommandHandler.java:563) io.lettuce.core.protocol.CommandHandler.channelRead(CommandHandler.java:516)原因分析 lettuce默认是没有开始拓扑更新及读写分离导致的 解决方案 这里分为几种情况 springboot 1.x之前版本默认使用jedis无需要手动开启刷新springboot 2.x默认为Lettuce需要代码设置开启刷新节点拓扑策略springboot 2.3.0开始支持集群拓扑刷新功能属性配置开启即可 第一种情况springboot1.x版本环境 springboot1.x之前版本默认使用jedis无需手动开启动态刷新。 第二种情况springboot2.0~2.3版本环境 springboot2.0-2.3版本默认使用lettuce默认不支持属性配置集群拓扑刷新。使用lettuce需要增加配置类需要手动开启刷新。 配置类如下 package com.test.config;import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import io.lettuce.core.cluster.ClusterClientOptions; import io.lettuce.core.cluster.ClusterTopologyRefreshOptions; import lombok.extern.java.Log; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; 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.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.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;import java.lang.reflect.Method; import java.time.Duration;Log Configuration EnableCaching // 开启缓存支持 public class RedisConfig {Autowiredprivate RedisProperties redisProperties;Bean(destroyMethod destroy) //销毁这个bean之前调用这个destroy回调方法释放资源public LettuceConnectionFactory redisConnectionFactory() {// redis单节点if (null redisProperties.getCluster() || null redisProperties.getCluster().getNodes()) {RedisStandaloneConfiguration configuration new RedisStandaloneConfiguration(redisProperties.getHost(),redisProperties.getPort());configuration.setPassword(redisProperties.getPassword()); //RedisPassword.of(redisProperties.getPassword())configuration.setDatabase(redisProperties.getDatabase());return new LettuceConnectionFactory(configuration);}// redis集群RedisClusterConfiguration redisClusterConfiguration new RedisClusterConfiguration(redisProperties.getCluster().getNodes());redisClusterConfiguration.setPassword(redisProperties.getPassword()); //RedisPassword.of(redisProperties.getPassword())redisClusterConfiguration.setMaxRedirects(redisProperties.getCluster().getMaxRedirects());GenericObjectPoolConfig genericObjectPoolConfig new GenericObjectPoolConfig();genericObjectPoolConfig.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());genericObjectPoolConfig.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());genericObjectPoolConfig.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());genericObjectPoolConfig.setMaxWaitMillis(redisProperties.getLettuce().getPool().getMaxWait().getSeconds());// 支持自适应集群拓扑刷新和动态刷新源ClusterTopologyRefreshOptions clusterTopologyRefreshOptions ClusterTopologyRefreshOptions.builder().enableAllAdaptiveRefreshTriggers()// 开启自适应刷新.enableAdaptiveRefreshTrigger()// 开启定时刷新.enablePeriodicRefresh(Duration.ofSeconds(5)).build();ClusterClientOptions clusterClientOptions ClusterClientOptions.builder().topologyRefreshOptions(clusterTopologyRefreshOptions).build();LettuceClientConfiguration lettuceClientConfiguration LettucePoolingClientConfiguration.builder().poolConfig(genericObjectPoolConfig) //如果使用默认配置可以注释genericObjectPoolConfig // .readFrom(ReadFrom.SLAVE_PREFERRED) //读写分离主写从读模式配置.clientOptions(clusterClientOptions).build();LettuceConnectionFactory lettuceConnectionFactory new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);lettuceConnectionFactory.setShareNativeConnection(false);// 是否允许多个线程操作共用同一个缓存连接默认 truefalse 时每个操作都将开辟新的连接lettuceConnectionFactory.resetConnection();// 重置底层共享连接, 在接下来的访问时初始化return lettuceConnectionFactory;}/*** RedisTemplate配置*/Beanpublic RedisTemplateObject, Object redisTemplate(LettuceConnectionFactory factory) {RedisTemplateObject, Object redisTemplate new RedisTemplate();redisTemplate.setConnectionFactory(factory);// 使用注解Bean返回RedisTemplate的时候同时配置hashkey和hashValue的序列虎方式// key采用String的序列化方式redisTemplate.setKeySerializer(keySerializer());// value使用jackson序列化方式redisTemplate.setValueSerializer(valueSerializer());// hash的key采用String的序列化方式redisTemplate.setHashKeySerializer(keySerializer());// hash的value使用jackson序列化方式redisTemplate.setHashValueSerializer(valueSerializer());/**必须执行这个函数,初始化RedisTemplate*/// 需要先调用afterPropertiesSet方法,此方法是应该是初始化参数和初始化工作。redisTemplate.afterPropertiesSet();log.info(序列化完成);return redisTemplate;}Beanpublic KeyGenerator keyGenerator() {return new KeyGenerator() {Overridepublic Object generate(Object target, Method method, Object... params) {StringBuffer sb new StringBuffer();sb.append(target.getClass().getName());sb.append(method.getName());for (Object obj : params) {sb.append(obj.toString());}return sb.toString();}};}/*** key键序列化方式** return RedisSerializer*/private RedisSerializerString keySerializer() {return new StringRedisSerializer();}/*** value值序列化方式** return*/private Jackson2JsonRedisSerializer valueSerializer() {Jackson2JsonRedisSerializer jackson2JsonRedisSerializer new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);return jackson2JsonRedisSerializer;} }配置文件 #Redis Configuration spring.redis.cluster.max-redirects10 spring.redis.cluster.nodes127.0.0.1:8001,127.0.0.1:8002 spring.redis.timeout60000ms spring.redis.password spring.redis.lettuce.pool.max-active10 spring.redis.lettuce.pool.max-idle8 spring.redis.lettuce.pool.min-idle0 spring.redis.lettuce.pool.max-wait-1ms 注意注入LettuceConnectionFactory后一定要记得注入RedisTemplate并 redisTemplate.setConnectionFactory(factory); apache commons-pool2 包提供了一个通用的对象池技术的实现。可以很方便的基于它来实现自己的对象池比如 DBCP 和 Jedis 他们的内部对象池的实现就是依赖于 commons-pool2 。 dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactIdversion2.8.0/version /dependency第三种情况springboot2.3之后版本环境 springboot2.3之后版本默认使用lettuce默认支持属性配置开启集群拓扑刷新其解决方案属性配置开启即可。 spring.redis.lettuce.cluster.refresh.adaptive true spring.redis.lettuce.cluster.refresh.period30000 # 30秒自动刷新一次 关联文章Spring Boot集成Redis集群报错UnsupportedOperationException-CSDN博客
http://www.zqtcl.cn/news/751380/

相关文章:

  • 制作网站要步骤湖北省建设厅网站上岗证查询
  • 网站建设制作公司都选万维科技制作网站需要注意什么
  • jsp小型网站开发wordpress微博插件
  • app充值网站开发怎么去做网站
  • 合肥建站网站模板word上下页纸张方向
  • 大学跳蚤市场网站建设哈尔滨网站建设
  • 网站开发合同中的知识产权条款怎么给公司建网站
  • 网站代维护wordpress 主题中心
  • 中铁广州建设有限公司网站临安做企业网站的公司
  • 国内可访问的海外网站和应用重庆好玩还是成都好玩
  • 定制开发小程序天津做网站优化的公司
  • 公司网站首页怎么做在线二级域名子域名查询
  • 淮南网站优化公司国内什么网站用asp.net
  • 做数据网站带网站的图片素材
  • 大方县住房城乡建设局网站wordpress 连接flickr
  • 国家建设部网站倪虹邢台新闻最新事件
  • 杭州网站 建设广州金将令做网站怎么样
  • 苏州科建设交通学院网站地方网站类型
  • 怎样做投资理财网站城乡建设部网站第35号令
  • 南昌集团网站建设wordpress去掉rss订阅
  • 郑州做网站的外包公司有哪些宁波 电商平台网站建设
  • 网站平台开发多少钱wordpress文章展示
  • 汕尾市企业网站seo点击软件建设一个网站的步骤有哪些
  • 备案上个人网站和企业网站的区别实名认证域名可以做电影网站吗
  • 顾氏网站建设有限公司怎么样memcache安装wordpress
  • 邯郸网站建设渠道免费做全网解析电影网站赚钱
  • 中铁中基建设集团网站东莞网络优化哪家公司好
  • wordpress免费建站合肥关键词网站排名
  • 中铁建设集团门户网登录网站自己可以建设环保公益网站吗
  • 国内电子商务网站有哪些网站升级中html