♻️ Refactoring code. 优化动态路由支持 Redis 信道重复加载

This commit is contained in:
冷冷 committed 2020-09-29 13:17:15 +08:00
1 parent 54e52f1c60
commit f0eeee7c44
4 files changed
+28 -4

No files matched your search

@@ -60,6 +60,11 @@ public interface CacheConstants {
*/
String ROUTE_JVM_RELOAD_TOPIC = "gateway_jvm_route_reload_topic";
/**
* redis 重新加载 路由信息
*/
String ROUTE_REDIS_RELOAD_TOPIC = "upms_redis_route_reload_topic";
/**
* 公众号 reload
*/
@@ -97,7 +97,7 @@ public class RedisCacheAutoConfiguration {
}
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
@@ -28,10 +28,14 @@ import lombok.extern.slf4j.Slf4j;
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;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.scheduling.annotation.Async;
@@ -81,4 +85,20 @@ public class DynamicRouteInitRunner {
log.debug("初始化网关路由结束 ");
}
/**
* redis 监听配置,监听 gateway_redis_route_reload_topic,重新加载Redis
* @param redisConnectionFactory redis 配置
* @return
*/
@Bean
public RedisMessageListenerContainer redisContainer(RedisConnectionFactory redisConnectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(redisConnectionFactory);
container.addMessageListener((message, bytes) -> {
log.warn("接收到重新Redis 重新加载路由事件");
initRoute();
}, new ChannelTopic(CacheConstants.ROUTE_REDIS_RELOAD_TOPIC));
return container;
}
}
@@ -30,7 +30,6 @@ import com.pig4cloud.pigx.common.gateway.support.DynamicRouteInitEvent;
import com.pig4cloud.pigx.common.gateway.vo.RouteDefinitionVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.context.ApplicationEventPublisher;
@@ -141,8 +140,8 @@ public class SysRouteConfServiceImpl extends ServiceImpl<SysRouteConfMapper, Sys
this.saveBatch(routeConfList);
log.debug("更新网关路由结束 ");
this.applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this));
redisTemplate.convertAndSend(CacheConstants.ROUTE_JVM_RELOAD_TOPIC, "UPMS路由信息,网关缓存更新");
// 通知网关重置路由
redisTemplate.convertAndSend(CacheConstants.ROUTE_JVM_RELOAD_TOPIC, "路由信息,网关缓存更新");
}
catch (Exception e) {
log.error("路由配置解析失败", e);