-
编写配置类
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
相关推荐
爱读源码的大都督30 分钟前
为什么有了HTTP,还需要gPRC?Lucky_Turtle1 小时前
【Java Xml】Apache Commons Digester3解析聪明的笨猪猪1 小时前
Java Redis “缓存设计”面试清单(含超通俗生活案例与深度理解)FIavor.1 小时前
我发送给Apifox是http://localhost:9002/goods/getByUserName?name=张三 为什么会是500哪里错了?ID_180079054731 小时前
京东获取整站实时商品详情数据|商品标题|数据分析提取教程微露清风2 小时前
系统性学习C++-第五讲-内存管理计算机毕业设计木哥2 小时前
计算机毕业设计选题推荐:基于SpringBoot和Vue的快递物流仓库管理系统【源码+文档+调试】235162 小时前
【LeetCode】146. LRU 缓存聪明的笨猪猪2 小时前
Java Redis “运维”面试清单(含超通俗生活案例与深度理解)FIavor.2 小时前
怎么办这是Apifox里执行http://localhost:9002/goods/getByUserName?name=“张三“为什么我改了还是500?