情景:
一般情况下,不需要动态路由,前端请求进入网关后,路由分发到fegin项目,在这里在进行分发到各个业务项目。
此时另一种情况导致了动态路由的出现,加入有一个网关,两套系统controller,在网关里面就要分配一次,进入各自系统后,在进行分发各自的;
实现一:不借助nacos配置中心
eureka:
client:
service-url:
#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
defaultZone: http://127.0.0.1:8761/eureka/
server:
port: 8888
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: product-app-api
uri: lb://product-app-api
order: 1
predicates:
- Path=/app/product-app-api/**
filters:
- name: AppAuth
args:
name: app
#- RewritePath=/product-app-api/(?<segment>.*), /$\{segment}
- StripPrefix=2
- id: product-web-api
uri: lb://product-web-api
order: 1
predicates:
- Path=/web/product-app-api/**
filters:
- name: WebAuth
args:
name: app
#- RewritePath=/product-app-api/(?<segment>.*), /$\{segment}
- StripPrefix=2
- id: index-app-api
uri: lb://index-app-api
order: 1
predicates:
- Path=/app/index-app-api/**
filters:
- name: AppAuth
args:
name: app
#- RewritePath=/product-app-api/(?<segment>.*), /$\{segment}
- StripPrefix=2
- id: index-web-api
uri: lb://index-web-api
order: 1
predicates:
- Path=/web/index-app-api/**
filters:
- name: WebAuth
args:
name: app
#- RewritePath=/product-app-api/(?<segment>.*), /$\{segment}
- StripPrefix=2
在这里配置了三套路由分发,其中
- id: index-web-api
uri: lb://index-web-api
order: 1
predicates:
- Path=/web/index-app-api/**
filters:
- name: WebAuth
args:
name: app
#- RewritePath=/product-app-api/(?<segment>.*), /$\{segment}
- StripPrefix=2
为一组
实现二:借助nacos实现动态路由分发
1.监听器监听nacos
Spring Cloud Gateway本身还不支持直接从Nacos动态加载路由配置表,需要自己编写监听器监听配置变化并刷新路由表。
NacosDynamicRouteService.java
@Component
public class NacosDynamicRouteService implements ApplicationEventPublisherAware {
private String dataId = "gateway-router";
private String group = "DEFAULT_GROUP";
@Value("${spring.cloud.nacos.config.server-addr}")
private String serverAddr;
@Autowired
private RouteDefinitionWriter routeDefinitionWriter;
private ApplicationEventPublisher applicationEventPublisher;
private static final List<String> ROUTE_LIST = new ArrayList<>();
@PostConstruct
public void dynamicRouteByNacosListener() {
try {
ConfigService configService = NacosFactory.createConfigService(serverAddr);
configService.getConfig(dataId, group, 5000);
configService.addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
clearRoute();
try {
List<RouteDefinition> gatewayRouteDefinitions = JSONObject.parseArray(configInfo, RouteDefinition.class);
for (RouteDefinition routeDefinition : gatewayRouteDefinitions) {
addRoute(routeDefinition);
}
publish();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Executor getExecutor() {
return null;
}
});
} catch (NacosException e) {
e.printStackTrace();
}
}
private void clearRoute() {
for(String id : ROUTE_LIST) {
this.routeDefinitionWriter.delete(Mono.just(id)).subscribe();
}
ROUTE_LIST.clear();
}
private void addRoute(RouteDefinition definition) {
try {
routeDefinitionWriter.save(Mono.just(definition)).subscribe();
ROUTE_LIST.add(definition.getId());
} catch (Exception e) {
e.printStackTrace();
}
}
private void publish() {
this.applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this.routeDefinitionWriter));
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
}
2.新增配置
代码中监听的配置ID为gateway-router,按此ID在Nacos中创建配置
假设上面图片下面的配置文件信息为:
[{
"id": "account-router",
"order": 0,
"predicates": [{
"args": {
"pattern": "/acc/**"
},
"name": "Path"
}],
"uri": "lb://account-service"
},{
"id": "payment-router",
"order": 2,
"predicates": [{
"args": {
"pattern": "/pay/**"
},
"name": "Path"
}],
"uri": "lb://payment-service"
}]
就可以根据请求来进行分发了
三:案例:
http://127.0.0.1:9001//acc/basic-manage/selectAllRiskDesc
请求会根据acc分发到acc的项目
http://127.0.0.1:9001//pay/basic-manage/selectAllRiskDesc
请求会根据pay分发到pay的项目