♻️ Refactoring code. 优化动态路由 refreshrouteevent

This commit is contained in:
qtcwxj committed 2020-09-22 12:34:42 +08:00
1 parent 58b2ba44a9
commit 5d2b6d84b4
4 files changed
+11 -60

No files matched your search

@@ -55,11 +55,6 @@ public interface CacheConstants {
*/
String ROUTE_KEY = GLOBALLY + "gateway_route_key";
/**
* redis reload 事件
*/
String ROUTE_REDIS_RELOAD_TOPIC = "gateway_redis_route_reload_topic";
/**
* 内存reload 时间
*/
@@ -18,28 +18,20 @@
package com.pig4cloud.pigx.common.gateway.configuration;
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
import com.pig4cloud.pigx.common.gateway.support.RouteCacheHolder;
import io.lettuce.core.ReadFrom;
import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.config.PropertiesRouteDefinitionLocator;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import java.time.Duration;
/**
* @author lengleng
* @date 2018/11/5
@@ -73,29 +65,10 @@ public class DynamicRouteAutoConfiguration {
container.addMessageListener((message, bytes) -> {
log.warn("接收到重新JVM 重新加载路由事件");
RouteCacheHolder.removeRouteList();
// 发送刷新路由事件
SpringContextHolder.publishEvent(new RefreshRoutesEvent(this));
}, new ChannelTopic(CacheConstants.ROUTE_JVM_RELOAD_TOPIC));
return container;
}
@Bean
@ConditionalOnProperty(value = "spring.redis.cluster.enable", havingValue = "true")
public LettuceConnectionFactory redisConnectionFactory(RedisProperties redisProperties) {
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(
redisProperties.getCluster().getNodes());
// https://github.com/lettuce-io/lettuce-core/wiki/Redis-Cluster#user-content-refreshing-the-cluster-topology-view
ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
.enablePeriodicRefresh().enableAllAdaptiveRefreshTriggers().refreshPeriod(Duration.ofSeconds(5))
.build();
ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder()
.topologyRefreshOptions(clusterTopologyRefreshOptions).build();
// https://github.com/lettuce-io/lettuce-core/wiki/ReadFrom-Settings
LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder()
.readFrom(ReadFrom.REPLICA_PREFERRED).clientOptions(clusterClientOptions).build();
return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);
}
}
@@ -72,7 +72,7 @@ public class DefaultRangeSequence implements RangeSequence {
for (;;) {
if (currentRange.isOver()) {
currentRange = seqRangeMgr.nextRange(name);
seqRangeMap.put(name,currentRange);
seqRangeMap.put(name, currentRange);
}
value = currentRange.getAndIncrement();
@@ -28,14 +28,10 @@ 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;
@@ -58,10 +54,10 @@ public class DynamicRouteInitRunner {
@Async
@Order
@EventListener({ WebServerInitializedEvent.class, DynamicRouteInitEvent.class })
@EventListener({WebServerInitializedEvent.class, DynamicRouteInitEvent.class})
public void initRoute() {
Boolean result = redisTemplate.delete(CacheConstants.ROUTE_KEY);
log.info("初始化网关路由 {} ", result);
redisTemplate.delete(CacheConstants.ROUTE_KEY);
log.info("开始初始化网关路由");
routeConfService.list().forEach(route -> {
RouteDefinitionVo vo = new RouteDefinitionVo();
@@ -79,23 +75,10 @@ public class DynamicRouteInitRunner {
redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(RouteDefinitionVo.class));
redisTemplate.opsForHash().put(CacheConstants.ROUTE_KEY, route.getRouteId(), vo);
});
// 通知网关重置路由
redisTemplate.convertAndSend(CacheConstants.ROUTE_JVM_RELOAD_TOPIC, "路由信息,网关缓存更新");
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;
}
}