♻️ Refactoring code. close #3596 【重构】重构redis 信道配置,支持 afterPropertiesSet 统一配置
This commit is contained in:
1 parent
57ec2a5e1e
commit
dbc3b9e05e
4 files changed
+54
-15
No files matched your search
+26
@@ -0,0 +1,26 @@
|
||||
package com.pig4cloud.pigx.common.data.cache;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2022/2/4
|
||||
*
|
||||
* redis message 信道相关配置
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class RedisMessageConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public RedisMessageListenerContainer redisContainer(RedisConnectionFactory redisConnectionFactory) {
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(redisConnectionFactory);
|
||||
return container;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.pig4cloud.pigx.common.data.cache.RedisTemplateConfig,\
|
||||
com.pig4cloud.pigx.common.data.cache.RedisMessageConfiguration,\
|
||||
com.pig4cloud.pigx.common.data.cache.RedisCacheManagerConfig,\
|
||||
com.pig4cloud.pigx.common.data.cache.RedisCacheAutoConfiguration,\
|
||||
com.pig4cloud.pigx.common.data.tenant.PigxTenantConfigProperties,\
|
||||
|
||||
+17
-1
@@ -7,11 +7,14 @@ import com.pig4cloud.pigx.common.core.constant.CacheConstants;
|
||||
import com.pig4cloud.pigx.common.data.tenant.TenantBroker;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.boot.web.context.WebServerInitializedEvent;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.listener.ChannelTopic;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -25,10 +28,12 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@SuppressWarnings("all")
|
||||
public class ClientDetailsInitRunner {
|
||||
public class ClientDetailsInitRunner implements InitializingBean {
|
||||
|
||||
private final SysOauthClientDetailsService clientDetailsService;
|
||||
|
||||
private final RedisMessageListenerContainer listenerContainer;
|
||||
|
||||
private final SysTenantService tenantService;
|
||||
|
||||
private final RedisTemplate redisTemplate;
|
||||
@@ -68,4 +73,15 @@ public class ClientDetailsInitRunner {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* redis 监听配置,监听 upms_redis_client_reload_topic,重新加载Redis
|
||||
*/
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
listenerContainer.addMessageListener((message, bytes) -> {
|
||||
log.warn("接收到重新Redis 重新加载客户端配置事件");
|
||||
initClientDetails();
|
||||
}, new ChannelTopic(CacheConstants.CLIENT_REDIS_RELOAD_TOPIC));
|
||||
}
|
||||
|
||||
}
|
||||
+10
-14
@@ -23,16 +23,15 @@ import com.pig4cloud.pigx.admin.service.SysRouteConfService;
|
||||
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
|
||||
import com.pig4cloud.pigx.common.gateway.support.DynamicRouteInitEvent;
|
||||
import com.pig4cloud.pigx.common.gateway.vo.RouteDefinitionVo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.boot.web.context.WebServerInitializedEvent;
|
||||
import org.springframework.cloud.gateway.filter.FilterDefinition;
|
||||
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.listener.ChannelTopic;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
@@ -50,13 +49,15 @@ import java.util.Map;
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
public class DynamicRouteInitRunner {
|
||||
@RequiredArgsConstructor
|
||||
public class DynamicRouteInitRunner implements InitializingBean {
|
||||
|
||||
private final RedisTemplate redisTemplate;
|
||||
|
||||
private final SysRouteConfService routeConfService;
|
||||
|
||||
private final RedisMessageListenerContainer listenerContainer;
|
||||
|
||||
@Async
|
||||
@Order
|
||||
@EventListener({ WebServerInitializedEvent.class, DynamicRouteInitEvent.class })
|
||||
@@ -87,19 +88,14 @@ public class DynamicRouteInitRunner {
|
||||
}
|
||||
|
||||
/**
|
||||
* redis 监听配置,监听 gateway_redis_route_reload_topic,重新加载Redis
|
||||
* @param redisConnectionFactory redis 配置
|
||||
* @return
|
||||
* redis 监听配置,监听 upms_redis_route_reload_topic,重新加载Redis
|
||||
*/
|
||||
@Bean
|
||||
public RedisMessageListenerContainer redisContainer(RedisConnectionFactory redisConnectionFactory) {
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(redisConnectionFactory);
|
||||
container.addMessageListener((message, bytes) -> {
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
listenerContainer.addMessageListener((message, bytes) -> {
|
||||
log.warn("接收到重新Redis 重新加载路由事件");
|
||||
initRoute();
|
||||
}, new ChannelTopic(CacheConstants.ROUTE_REDIS_RELOAD_TOPIC));
|
||||
return container;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user