-
编写配置类
java@Component public class NacosConfig { public static String GROUP_ID ; public static String DATA_ID ; @Value("${nacos.gateway.route.config.group}") public void setGroupId(String groupId){ GROUP_ID = groupId ; } @Value("${nacos.gateway.route.config.data-id}") public void setDataId(String dataId){ DATA_ID = dataId ; } } -
properties添加配置
propertiesnacos.gateway.route.config.group=DEFAULT_GROUP nacos.gateway.route.config.data-id=hello-gateway-router.json -
自定义RouteDefinitionLocator
java@Slf4j @Component public class NcosRouteDefinitionLocator implements RouteDefinitionLocator, InitializingBean { private volatile List<RouteDefinition> routeDefinitions = new CopyOnWriteArrayList<>() ; @Autowired private NacosConfigManager nacosConfigManager ; @Autowired private ObjectMapper objectMapper ; @Autowired private ApplicationEventPublisher eventPublisher ; private ConfigService configService ; @Override public Flux<RouteDefinition> getRouteDefinitions() { return Flux.fromIterable(routeDefinitions); } @Override public void afterPropertiesSet() throws Exception { // 从nacos获取routDefinition configService = nacosConfigManager.getConfigService(); // 读取配置并装载 this.initNacosConfig(); // 添加listener,当数据变化时接收通知 this.initNacosListener(); } private void initNacosConfig(){ try { String content = configService.getConfig( NacosConfig.DATA_ID, NacosConfig.GROUP_ID, 3000L ); CollectionType collectionType = objectMapper.getTypeFactory() .constructCollectionType(ArrayList.class, RouteDefinition.class); routeDefinitions = objectMapper.readValue(content, collectionType); }catch (NacosException e){ log.info("nacos config NacosException ", e); } catch (JsonProcessingException e) { log.info("nacos config JsonProcessingException ", e); } } private void initNacosListener() throws NacosException { configService.addListener( NacosConfig.DATA_ID, NacosConfig.GROUP_ID, new DataChangeListener()); } class DataChangeListener implements Listener{ @Override public Executor getExecutor() { return null; } @Override public void receiveConfigInfo(String content) { try { log.info("gateway config change : {}", content); CollectionType collectionType = objectMapper.getTypeFactory() .constructCollectionType(ArrayList.class, RouteDefinition.class); routeDefinitions = objectMapper.readValue(content, collectionType); // 当数据发生变化后发布刷新事件,通知CachingRouteLocator需要重新加载route定义 eventPublisher.publishEvent(new RefreshRoutesEvent(content)); }catch (JsonProcessingException e) { log.info("nacos config JsonProcessingException ", e); } } } } -
编写GatewayDynamicConfiguration配置类
java@Configuration public class GatewayDynamicConfiguration { @Bean @ConditionalOnProperty(value = "spring.cloud.gateway.dynamic.config.enable", matchIfMissing = true) public NacosRouteDefinitionLocator routeDefinitionLocator(){ return new NacosRouteDefinitionLocator() ; } }
SpringCloud-Gateway路由动态配置Nacos实现
yicj2023-10-03 17:42
相关推荐
今天多喝热水2 分钟前
SpEL(Spring Expression Language) 表达式wasp5203 分钟前
Hudi 客户端实现分析学海无涯书山有路3 分钟前
Android LiveData + MVVM 新手入门教程(基于 XML+Java)Hello.Reader4 分钟前
Flink 2.0 从 flink-conf.yaml 到 config.yaml 的正确打开方式(含迁移与最佳实践)李慕婉学姐5 分钟前
【开题答辩过程】以《基于uni-app的手账记录小程序的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看福大大架构师每日一题6 分钟前
milvus v2.6.9 发布:支持主键搜索、段重开机制、日志性能全面提升!独自破碎E6 分钟前
【滑动窗口】最长无重复子数组GIOTTO情6 分钟前
Infoseek 媒介投放系统技术实现:基于与辉同行风波的风险防控架构设计木井巳7 分钟前
【Java】数据类型及运算符重点总结码农水水7 分钟前
美团Java面试被问:Netty的ByteBuf引用计数和内存释放