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
 *
 *      https://www.apache.org/licenses/LICENSE-2.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 {

	

	@Bean
	public 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]]

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值