gateway可以默认通过配置开启,服务自动注册服务名为网关路由地址,比如:服务sc-xxx,可以通过:http://gateway/sc-xx/** 进行访问,开启对应的配置如下:
spring:
cloud:
gateway:
routes:
discovery:
locator:
enabled: true
但有时候我们不想这个样做,原因:
新建一个动态路由操作类 DynamicRouteManager 因为要发布 RefreshRoutesEvent 事件,所以需要实现spring 的 ApplicationEventPublisherAware 接口,提供如下方法:
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import java.util.List;
/**
* 动态路由OP类
*/
@Component
public class DynamicRouteManager implements ApplicationEventPublisherAware {
private final RouteDefinitionWriter routeDefinitionWriter;
private ApplicationEventPublisher publisher;
public DynamicRouteManager(RouteDefinitionWriter routeDefinitionWriter) {
this.routeDefinitionWriter = routeDefinitionWriter;
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
/**
* 增加路由
*/
public void add(RouteDefinition definition) {
routeDefinitionWriter.save(Mono.just(definition)).subscribe();
publisher.publishEvent(new RefreshRoutesEvent(this));
}
/**
* 更新路由
*/
public void update(RouteDefinition definition) {
routeDefinitionWriter.delete(Mono.just(definition.getId()));
routeDefinitionWriter.save(Mono.just(definition)).subscribe();
publisher.publishEvent(new RefreshRoutesEvent(this));
}
/**
* 批量更新路由
*/
public void updateList(List<RouteDefinition> routeDefinitions) {
routeDefinitions.forEach(this::update);
}
/**
* 删除路由
*/
public void delete(String id) {
routeDefinitionWriter.delete(Mono.just(id));
}
}
新建一个 NacosDynamicRouteListener 监听类nacos路由信息变化
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executor;
/**
* 动态路由监听器
*
* @author Chill
*/
@Order
@Slf4j
@Component
public class NacosDynamicRouteListener implements InitializingBean {
private final DynamicRouteManager dynamicRouteManager;
private final NacosDiscoveryProperties nacosDiscoveryProperties;
private final NacosConfigProperties nacosConfigProperties;
private final DynamicRouteProperties dynamicRouteProperties;
public NacosDynamicRouteListener(DynamicRouteManager dynamicRouteManager,
NacosDiscoveryProperties nacosDiscoveryProperties,
NacosConfigProperties nacosConfigProperties,
DynamicRouteProperties dynamicRouteProperties) {
this.dynamicRouteManager = dynamicRouteManager;
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
this.nacosConfigProperties = nacosConfigProperties;
this.dynamicRouteProperties = dynamicRouteProperties;
}
/**
* 监听Nacos的动态路由配置
*/
private void dynamicRouteListener() {
try {
String dataId = dynamicRouteProperties.getDataId();
String group = nacosConfigProperties.getGroup();
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.NAMESPACE, nacosDiscoveryProperties.getNamespace());
properties.setProperty(PropertyKeyConst.SERVER_ADDR, nacosDiscoveryProperties.getServerAddr());
ConfigService configService = NacosFactory.createConfigService(properties);
configService.addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
log.info("---监听到路由信息刷新---\n{}\n---------------------------------------", configInfo);
List<RouteDefinition> routeDefinitions = JSON.parseArray(configInfo, RouteDefinition.class);
dynamicRouteManager.updateList(routeDefinitions);
}
@Override
public Executor getExecutor() {
return null;
}
});
String configInfo = configService.getConfig(dataId, group, 1000 * 10);
if (configInfo != null) {
List<RouteDefinition> routeDefinitions = JSON.parseArray(configInfo, RouteDefinition.class);
dynamicRouteManager.updateList(routeDefinitions);
}
} catch (NacosException e) {
log.error("动态路由监听异常", e);
}
}
@Override
public void afterPropertiesSet() throws Exception {
dynamicRouteListener();
}
}
监听路由配置信息:xxx.xxx.xxx.json 配置文件,初始配置如下:
[
{
"id":"service-xxx",
"uri":"lb://service-xxx/",
"predicates":[
{
"name":"Path",
"args":{
"pattern1":"/service-xxx/**"
}
}
],
"order":1
}
]
访问地址:http://localhost:8000/service-xxx ,响应结果报错500说明服务路由识别成功,并没有404,可以用一个正常的接口测试就不会500了。
{
"msg": "Failed to handle request [GET http://localhost:8002/service-xxx]: ",
"code": 500,
"data": null
}
修改一下Naco对应json配置,pattern1的service-xxx1 改成 service-xxx1/如下:
[
{
"id":"service-xxx",
"uri":"lb://service-xxx/",
"predicates":[
{
"name":"Path",
"args":{
"pattern1":"/service-xxx1/**"
}
}
],
"order":1
}
]
日志打印:
2022-06-20 18:24:14.362 [xxx-gateway] INFO 509450 [ternal.notifier] o.s.v.g.d.NacosDynamicRouteListener 76 [TID: N/A] : ---监听到路由信息刷新---
[
{
"id":"service-xxx",
"uri":"lb://service-xxx/",
"predicates":[
{
"name":"Path",
"args":{
"pattern1":"/service-xxx1/**"
}
}
],
"order":1
}
]
再次访问原接口:http://localhost:8000/service-xxx ,404 报错,路由信息已经改变了。
{
"msg": "Failed to handle request [GET http://localhost:8002/service-xxx]: 404 NOT_FOUND",
"code": 404,
"data": null
}