转自:https://blog.csdn.net/bengbuguang4321/article/details/121951650
在启动项目时,Redisson 自己会启动一个Redisson连接池,尝试连接redis,这时候如果遇到网络不通就会出现问题,因为redis连接不上,导致项目启动不了
解决方法是:
1、重新空实现了一个RedissonClient
/** * @ClassName RedissonClientTemporary * @Decription 只是在初始化时候使用一下,之后就会被替换 */ public class RedissonClientTemporary implements RedissonClient{ }
2、然后将这个空实现注入到spring容器
@Configuration public class RedissonConfig { /** * 配置一个临时的对象到spring容器中,不使用 * @return 一个RedissonClient的实现 */ @Bean public RedissonClient redissonClient() { RedissonClient redissonClient = new RedissonClientTemporary(); return redissonClient; } }
3、在项目启动后使用监听事件,放入Redisson替换RedissonClient的实现,然后初始化一下
注:这里可能还是会连接报错,但是项目已经启动,这里再连接报错的话可以考虑重试机制等补偿方案
/** * @ClassName ApplicationLoadRedissonListener * @Decription 项目启动完成,增加一个监听器,替换spring容器里面的redissonClient的对象,进行切换成redisson */ @Component public class ApplicationLoadRedissonListener implements ApplicationListener<ApplicationReadyEvent> { private static final Logger log = LoggerFactory.getLogger(ApplicationLoadRedissonListener.class); @Autowired ConfigurableApplicationContext configurableApplicationContext; @Autowired private RedisProperties redisProperties; @Value("${spring.redis.redisson.singleServerConfig.subscriptionsPerConnection}") private Integer subscriptionsPerConnection; @Value("${spring.redis.redisson.singleServerConfig.connectionPoolSize}") private Integer connectionPoolSize; @Value("${spring.redis.redisson.singleServerConfig.connectionMinimumIdleSize}") private Integer connectionMinimumIdleSize; @Value("${spring.redis.redisson.singleServerConfig.subscriptionConnectionPoolSize}") private Integer subscriptionConnectionPoolSize; @Value("${spring.redis.redisson.singleServerConfig.subscriptionConnectionMinimumIdleSize}") private Integer subscriptionConnectionMinimumIdleSize; @Override public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { try{ //加载一些基本的redis基础配置 Config config = new Config(); String address = "redis://" + redisProperties.getHost() + ":" + redisProperties.getPort(); SingleServerConfig serverConfig = config.useSingleServer(); serverConfig.setAddress(address); serverConfig.setDatabase(redisProperties.getDatabase()); if (!StringUtils.isEmpty(redisProperties.getPassword())) { serverConfig.setPassword(redisProperties.getPassword()); } serverConfig.setTimeout((int)redisProperties.getTimeout().toMillis()); //加载redisson一些特殊配置 serverConfig.setConnectionPoolSize(connectionPoolSize); serverConfig.setConnectionMinimumIdleSize(connectionMinimumIdleSize); serverConfig.setSubscriptionConnectionMinimumIdleSize(subscriptionConnectionMinimumIdleSize); serverConfig.setSubscriptionConnectionPoolSize(subscriptionConnectionPoolSize); serverConfig.setSubscriptionsPerConnection(subscriptionsPerConnection); log.info("加载 redisson配置信息 {}", JsonUtil.of(serverConfig)); BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Redisson.class); beanDefinitionBuilder.addConstructorArgValue(config); String redissonClientName = RedissonClient.class.getSimpleName().substring(0,1).toLowerCase() + RedissonClient.class.getSimpleName().substring(1); Object redissonClient = configurableApplicationContext.getBean(redissonClientName); log.info("初次放入的redissonClient实现对象:{}", redissonClient.getClass().getName());; //创建一个Redisson对象 BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) configurableApplicationContext; beanDefinitionRegistry.registerBeanDefinition(redissonClientName, beanDefinitionBuilder.getBeanDefinition()); //这里相当于初始化加载使用 redissonClient = configurableApplicationContext.getBean(redissonClientName); log.info("最终放入的redissonClient实现对象:{}", redissonClient.getClass().getName()); }catch (Exception e){ log.info("ApplicationLoadRedissonListener/onApplicationEvent/RedissonClient/Exception:[{}]", e.getMessage()); } } }写在最后: redisson 连接配置
# redisson 连接配置 # 单个连接最大订阅数量 spring.redis.redisson.singleServerConfig.subscriptionsPerConnection=5 # 连接池大小 spring.redis.redisson.singleServerConfig.connectionPoolSize=8 # 最小空闲连接数 spring.redis.redisson.singleServerConfig.connectionMinimumIdleSize=4 # 发布和订阅连接池大小 spring.redis.redisson.singleServerConfig.subscriptionConnectionPoolSize=8 # 发布和订阅连接的最小空闲连接数 spring.redis.redisson.singleServerConfig.subscriptionConnectionMinimumIdleSize=1
标签:Redisson,Java,spring,redis,singleServerConfig,serverConfig,redissonClient,rediss From: https://www.cnblogs.com/z5337/p/18057816