開發環境: SpringBoot: 2.6.5 SpringCloud: 2021.0.0 SpringCloudAlibaba: 2021.0.1.0 Nacos: 2.1.0 代碼: @Slf4j @Component public class MyInMemoryRouteDefinition ...
開發環境:
SpringBoot: 2.6.5
SpringCloud: 2021.0.0
SpringCloudAlibaba: 2021.0.1.0
Nacos: 2.1.0
代碼:
@Slf4j
@Component
public class MyInMemoryRouteDefinitionRepository implements RouteDefinitionRepository {
private final Map<String, RouteDefinition> routes = Collections.synchronizedMap(new LinkedHashMap<>());
@Override
public Flux<RouteDefinition> getRouteDefinitions() {
Map<String, RouteDefinition> routesSafeCopy = new LinkedHashMap(this.routes);
return Flux.fromIterable(routesSafeCopy.values());
}
@Override
public Mono<Void> save(Mono<RouteDefinition> route) {
return route.flatMap((r) -> {
if (ObjectUtils.isEmpty(r.getId())) {
return Mono.error(new IllegalArgumentException("id may not be empty"));
} else {
this.routes.put(r.getId(), r);
return Mono.empty();
}
});
}
@Override
public Mono<Void> delete(Mono<String> routeId) {
return routeId.flatMap((id) -> {
if (this.routes.containsKey(id)) {
this.routes.remove(id);
} else {
log.warn("RouteDefinition not found: " + routeId);
}
return Mono.empty();
});
}
}
@Slf4j
@Component
public class DynamicRouteUtil implements ApplicationEventPublisherAware {
@Resource
private MyInMemoryRouteDefinitionRepository routeDefinitionRepository;
private ApplicationEventPublisher publisher;
@Override
public void setApplicationEventPublisher(@NotNull ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
public void deleteRoute(String id) {
try {
log.info("gateway delete route id {}", id);
this.routeDefinitionRepository.delete(Mono.just(id)).subscribe();
this.publisher.publishEvent(new RefreshRoutesEvent(this));
} catch (Exception e) {
log.error("{}:刪除路由失敗", id);
}
}
public void updateRoutes(List<RouteDefinition> definitions) {
log.info("gateway update routes {}", definitions);
// 獲取存在路由列表
List<RouteDefinition> routeDefinitionsExits = this.routeDefinitionRepository
.getRouteDefinitions()
.buffer()
.blockFirst();
// 刪除路由
if (CollectionUtils.isNotEmpty(routeDefinitionsExits)) {
for (RouteDefinition routeDefinitionsExit : routeDefinitionsExits) {
deleteRoute(routeDefinitionsExit.getId());
}
}
// 更新路由
definitions.forEach(this::updateRoute);
}
public void updateRoute(RouteDefinition definition) {
// 先刪
log.info("gateway delete route {}", definition);
this.routeDefinitionRepository.delete(Mono.just(definition.getId()));
// 後增
this.routeDefinitionRepository.save(Mono.just(definition)).subscribe();
this.publisher.publishEvent(new RefreshRoutesEvent(this));
}
public void addRoutes(List<RouteDefinition> definitions) {
if (CollectionUtils.isEmpty(definitions)) {
return;
}
for (RouteDefinition definition : definitions) {
log.info("add route:{}", definition.getId());
this.routeDefinitionRepository.save(Mono.just(definition)).subscribe();
}
this.publisher.publishEvent(new RefreshRoutesEvent(this));
}
}
Component
public class DynamicRouteHandle {
@Resource
private DynamicRouteUtil dynamicRouteUtil;
@Resource
private NacosConfigProperties nacosConfigProperties;
private ConfigService configService;
public static final String ROUTE_DATA_ID = "gateway-router.json";
public static final long DEFAULT_TIMEOUT = 30000;
@PostConstruct
public void init() {
log.info("gateway route init...");
try {
configService = initConfigService();
if (configService == null) {
log.warn("initConfigService fail");
return;
}
String configInfo = configService.getConfig(ROUTE_DATA_ID, nacosConfigProperties.getGroup(), DEFAULT_TIMEOUT);
log.info("獲取網關當前配置:{}", configInfo);
List<RouteDefinition> definitionList = JSON.parseArray(configInfo, RouteDefinition.class);
log.info("獲取網關數量:{}", definitionList.size());
dynamicRouteUtil.addRoutes(definitionList);
} catch (Exception e) {
log.error("初始化網關路由時發生錯誤", e);
}
// 添加監聽
dynamicRouteByNacosListener(ROUTE_DATA_ID, nacosConfigProperties.getGroup());
}
public void dynamicRouteByNacosListener(String dataId, String group) {
try {
configService.addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
log.info("進行網關更新:\n\r{}", configInfo);
List<RouteDefinition> definitionList = JSON.parseArray(configInfo, RouteDefinition.class);
log.info("update route : {}", definitionList.toString());
dynamicRouteUtil.updateRoutes(definitionList);
}
@Override
public Executor getExecutor() {
log.info("getExecutor\n\r");
return null;
}
});
} catch (NacosException e) {
log.error("從nacos接收動態路由配置出錯!!!", e);
}
}
private ConfigService initConfigService() {
try {
Properties properties = new Properties();
properties.setProperty("serverAddr", nacosConfigProperties.getServerAddr());
properties.setProperty("namespace", nacosConfigProperties.getNamespace());
properties.setProperty("username", nacosConfigProperties.getUsername());
properties.setProperty("password", nacosConfigProperties.getPassword());
return NacosFactory.createConfigService(properties);
} catch (Exception e) {
log.error("初始化網關路由時發生錯誤", e);
return null;
}
}
}
配置:
gateway-router.json
[{
"id": "user-route",
"order": 0,
"predicates": [{
"name": "Path",
"args": {
"_genkey_0": "/userApp/**"
}
}],
"filters": [],
"uri": "lb://user"
},{
"id": "product-route",
"order": 0,
"predicates": [{
"name": "Path",
"args": {
"_genkey_0": "/productApp/**"
}
}],
"filters": [],
"uri": "lb://product"
}]
註: 如果服務設置了context-path並且與服務名稱相同會有問題, 詳情: https://blog.csdn.net/weixin_43303455/article/details/122279447, GatewayDiscoveryClientAutoConfiguration.java會為每一個服務創建一個預設路由, 此路由有一個RewritePathGatewayFilter, 會將context-path與serviceId(服務名稱)相同的進行置空。
註: 通過 網關服務:ip/actuator/gateway/routes, 可以查看具體的路由信息, 前提是要開啟配置
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always