SpringCloud Gateway nacos最简单实现动态路由,秒杀全网方案

编程入门 行业动态 更新时间:2024-10-05 17:25:22

SpringCloud Gateway nacos<a href=https://www.elefans.com/category/jswz/34/1769011.html style=最简单实现动态路由,秒杀全网方案"/>

SpringCloud Gateway nacos最简单实现动态路由,秒杀全网方案

在网上搜索了一大遍基本上实现动态路由都需要更改原路由的格式,并且代码不少。

但我非常不情愿改变原路由的配置方式,但又能根据nacos实现动态路由功能

看源码是少不了的了,看看原来springcloud是怎么注入配置路由的

1、关键是找到

GatewayProperties 核心配置文件
package org.springframework.cloud.gateway.config;/*** @author Spencer Gibb*/
@ConfigurationProperties("spring.cloud.gateway")
@Validated
public class GatewayProperties {
}

然后查看该配置在哪里注入

找到了

GatewayAutoConfiguration 自动注入配置类
/** Copyright 2013-2020 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      .0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.cloud.gateway.config;/*** @author Spencer Gibb* @author Ziemowit Stolarczyk*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)
@EnableConfigurationProperties
@AutoConfigureBefore({ HttpHandlerAutoConfiguration.class,WebFluxAutoConfiguration.class })
@AutoConfigureAfter({ GatewayLoadBalancerClientAutoConfiguration.class,GatewayClassPathWarningAutoConfiguration.class })
@ConditionalOnClass(DispatcherHandler.class)
public class GatewayAutoConfiguration {@Beanpublic GatewayProperties gatewayProperties() {return new GatewayProperties();}}

可以看到,路由的加载是一次性的,本身也不支持动态路由,那么就想办法更新GatewayProperties 即可

下面开始我的尝试,我的基础是基于@RefreshScope 的出发点,这个是nacos更新配置后能刷新注入的内容,所以我考虑新建了一个MyGatewayProperties代替

/*** 实现动态路由* @author lisonglin* @date 2021/7/30 3:17 下午*/
@Component
@ConfigurationProperties("spring.cloud.gateway")
@Validated
@RefreshScope
@Primary
public class MyGatewayProperties extends GatewayProperties {
}

经过测试,就是这么简单,完了,这就是所有的代码,而且完成支持原配置方式,比网上的那些动态配置的方法好用多了

 更新nacos配置,查看日志,最后postman查看调试头已被更新

2021-07-30 16:03:22 [com.alibaba.nacos.client.Worker.longPolling.fixed-localhost_8848-public] INFO  org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration Located property source: [BootstrapPropertySource {name='bootstrapProperties-dndc-gateway-service-dev.yml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-dndc-gateway-service.yml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-dndc-gateway-service,DEFAULT_GROUP'}]
2021-07-30 16:03:22 [com.alibaba.nacos.client.Worker.longPolling.fixed-localhost_8848-public] INFO  org.springframework.boot.SpringApplication The following profiles are active: dev
2021-07-30 16:03:22 [com.alibaba.nacos.client.Worker.longPolling.fixed-localhost_8848-public] INFO  org.springframework.boot.SpringApplication Started application in 0.123 seconds (JVM running for 142.306)
2021-07-30 16:03:22 [com.alibaba.nacos.client.Worker.longPolling.fixed-localhost_8848-public] DEBUG com.dndc.gateway.config.MyDndcGatewayProperties Routes supplied from Gateway Properties: [RouteDefinition{id='dndc-consumer', predicates=[PredicateDefinition{name='Path', args={_genkey_0=/consumer/**}}], filters=[FilterDefinition{name='StripPrefix', args={_genkey_0=1}}, FilterDefinition{name='AddRequestHeader', args={_genkey_0=x-feat, _genkey_1=feat-xxxx201127}}], uri=http://127.0.0.1:8083, order=0, metadata={}}]
2021-07-30 16:03:22 [com.alibaba.nacos.client.Worker.longPolling.fixed-localhost_8848-public] INFO  org.springframework.cloud.endpoint.event.RefreshEventListener Refresh keys changed: [spring.cloud.gateway.routes[0].filters[1]]

 

更多推荐

SpringCloud Gateway nacos最简单实现动态路由,秒杀全网方案

本文发布于:2024-02-13 14:25:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759312.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最简单   路由   方案   动态   秒杀

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!