Spring-Cloud-Gateway 源码解析 —— 网关管理 HTTP API

x33g5p2x  于2021-12-20 转载在 其他  
字(4.1k)|赞(0)|评价(0)|浏览(400)

1. 概述

本文主要分享网关管理 HTTP API

org.springframework.cloud.gateway.actuate.GatewayWebfluxEndpoint ,提供管理网关的 HTTP API 。构造方法,代码如下:

@RestController
@RequestMapping("${management.context-path:/application}/gateway")
public class GatewayWebfluxEndpoint implements ApplicationEventPublisherAware {

	private static final Log log = LogFactory.getLog(GatewayWebfluxEndpoint.class);

    /**
     * 路由定义定位器
     */
	private RouteDefinitionLocator routeDefinitionLocator;
    /**
     * 全局过滤器
     */
	private List<GlobalFilter> globalFilters;
    /**
     * 网关过滤器工厂
     */
	private List<GatewayFilterFactory> gatewayFilters;
    /**
     * 存储器 RouteDefinitionLocator 对象
     */
	private RouteDefinitionWriter routeDefinitionWriter;
    /**
     * 路由定位器
     */
	private RouteLocator routeLocator;
    /**
     * 应用事件发布器
     */
	private ApplicationEventPublisher publisher;

	public GatewayWebfluxEndpoint(RouteDefinitionLocator routeDefinitionLocator, List<GlobalFilter> globalFilters,
								  List<GatewayFilterFactory> GatewayFilters, RouteDefinitionWriter routeDefinitionWriter,
								  RouteLocator routeLocator) {
		this.routeDefinitionLocator = routeDefinitionLocator;
		this.globalFilters = globalFilters;
		this.gatewayFilters = GatewayFilters;
		this.routeDefinitionWriter = routeDefinitionWriter;
		this.routeLocator = routeLocator;
	}
}

GatewayWebfluxEndpoint 提供两类 HTTP API :

  • 过滤器 HTTP API
  • 路由 HTTP API

2. 过滤器 HTTP API

2.1 全局过滤器列表

@GetMapping("/globalfilters")
public Mono<HashMap<String, Object>> globalfilters() {
	return getNamesToOrders(this.globalFilters);
}

private <T> Mono<HashMap<String, Object>> getNamesToOrders(List<T> list) {
	return Flux.fromIterable(list).reduce(new HashMap<>(), this::putItem);
}

private HashMap<String, Object> putItem(HashMap<String, Object> map, Object o) {
	Integer order = null;
	if (o instanceof Ordered) {
		order = ((Ordered)o).getOrder();
	}
	//filters.put(o.getClass().getName(), order);
	map.put(o.toString(), order);
	return map;
}

2.2 路由过滤器工厂列表 

@GetMapping("/routefilters")
public Mono<HashMap<String, Object>> routefilers() {
	return getNamesToOrders(this.gatewayFilters);
}

3. 路由 HTTP API

2.1 路由列表

@GetMapping("/routes")
public Mono<Map<String, List>> routes() {
	Mono<List<RouteDefinition>> routeDefs = this.routeDefinitionLocator.getRouteDefinitions().collectList();
	Mono<List<Route>> routes = this.routeLocator.getRoutes().collectList();
	return Mono.zip(routeDefs, routes).map(tuple -> {
		Map<String, List> allRoutes = new HashMap<>();
		allRoutes.put("routeDefinitions", tuple.getT1());
		allRoutes.put("routes", tuple.getT2());
		return allRoutes;
	});
}

2.2 单个路由信息 

@GetMapping("/routes/{id}")
public Mono<ResponseEntity<RouteDefinition>> route(@PathVariable String id) {
	//TODO: missing RouteLocator
	return this.routeDefinitionLocator.getRouteDefinitions()
			.filter(route -> route.getId().equals(id))
			.singleOrEmpty()
			.map(route -> ResponseEntity.ok(route))
			.switchIfEmpty(Mono.just(ResponseEntity.notFound().build()));
}
  • 从 TODO: missing RouteLocator ,我们可以看到,目前不支持从 RouteLocator 获取 Route ,只返回 RouteDefinition 。等待未来的版本支持。

2.3 单个路由的过滤器

@GetMapping("/routes/{id}/combinedfilters")
public Mono<HashMap<String, Object>> combinedfilters(@PathVariable String id) {
	//TODO: missing global filters
	return this.routeLocator.getRoutes()
			.filter(route -> route.getId().equals(id))
			.reduce(new HashMap<>(), this::putItem);
}
  • 从 TODO: missing global filters ,我们可以看到,目前返回的过滤器不包括 GlobalFilter ,可以调用 /globalfilters 查看。等待未来的版本支持。

2.4 添加/修改单个路由

在 《Spring-Cloud-Gateway 源码解析 —— 路由(1.3)之 RouteDefinitionRepository 存储器》「5. GatewayWebfluxEndpoint」 有详细解析。

2.5 删除单个路由

在 《Spring-Cloud-Gateway 源码解析 —— 路由(1.3)之 RouteDefinitionRepository 存储器》「5. GatewayWebfluxEndpoint」 有详细解析。

2.6 刷新路由缓存

在 《Spring-Cloud-Gateway 源码解析 —— 路由(2.1)之 RouteLocator 一览》「5. CachingRouteLocator」 有详细解析。

相关文章