Commit 776b34ee by Nepxion

支持在网关过滤器中从远程配置中心获取路由信息配置,并全链路传递到服务端

parent 694c4528
......@@ -15,6 +15,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import com.nepxion.discovery.plugin.example.gateway.impl.MyDiscoveryEnabledStrategy;
import com.nepxion.discovery.plugin.example.gateway.impl.MyGatewayFilter;
@SpringBootApplication
@EnableDiscoveryClient
......@@ -28,10 +29,10 @@ public class DiscoveryApplicationGateway {
return new MyDiscoveryEnabledStrategy();
}
/*@Bean
@Bean
public MyGatewayFilter myGatewayFilter() {
return new MyGatewayFilter();
}*/
}
/*@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
......
......@@ -11,6 +11,7 @@ package com.nepxion.discovery.plugin.example.gateway.impl;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
......@@ -18,12 +19,24 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
import com.nepxion.discovery.common.entity.RuleEntity;
import com.nepxion.discovery.common.entity.StrategyEntity;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.strategy.gateway.constant.GatewayStrategyConstant;
public class MyGatewayFilter implements GlobalFilter, Ordered {
@Autowired
private PluginAdapter pluginAdapter;
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest newRequest = exchange.getRequest().mutate().header(DiscoveryConstant.N_D_VERSION, "{\"discovery-springcloud-example-a\":\"1.0\", \"discovery-springcloud-example-b\":\"1.0\", \"discovery-springcloud-example-c\":\"1.0;1.2\"}").build();
String routeVersion = getRouteVersionFromConfig();
// String routeVersion = getRouteVersionFromCustomer();
System.out.println("Route Version=" + routeVersion);
// 通过过滤器设置路由Header头部信息,来取代界面(Postman)上的设置,并全链路传递到服务端
ServerHttpRequest newRequest = exchange.getRequest().mutate().header(DiscoveryConstant.N_D_VERSION, routeVersion).build();
ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();
return chain.filter(newExchange);
......@@ -34,4 +47,22 @@ public class MyGatewayFilter implements GlobalFilter, Ordered {
// Order必须小于8888
return GatewayStrategyConstant.SPRING_APPLICATION_STRATEGY_GATEWAY_FILTER_ORDER_VALUE - 1;
}
// 从远程配置中心或者本地配置文件获取版本路由配置。如果是远程配置中心,则值会动态改变
protected String getRouteVersionFromConfig() {
RuleEntity ruleEntity = pluginAdapter.getRule();
if (ruleEntity != null) {
StrategyEntity strategyEntity = ruleEntity.getStrategyEntity();
if (strategyEntity != null) {
return strategyEntity.getVersionValue();
}
}
return null;
}
// 自定义版本路由配置
protected String getRouteVersionFromCustomer() {
return "{\"discovery-springcloud-example-a\":\"1.0\", \"discovery-springcloud-example-b\":\"1.0\", \"discovery-springcloud-example-c\":\"1.0;1.2\"}";
}
}
\ No newline at end of file
......@@ -16,6 +16,7 @@ import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import com.nepxion.discovery.plugin.example.zuul.impl.MyDiscoveryEnabledStrategy;
import com.nepxion.discovery.plugin.example.zuul.impl.MyZuulFilter;
@SpringBootApplication
@EnableDiscoveryClient
......@@ -30,8 +31,8 @@ public class DiscoveryApplicationZuul {
return new MyDiscoveryEnabledStrategy();
}
/*@Bean
@Bean
public MyZuulFilter myZuulFilter() {
return new MyZuulFilter();
}*/
}
}
\ No newline at end of file
......@@ -9,11 +9,19 @@ package com.nepxion.discovery.plugin.example.zuul.impl;
* @version 1.0
*/
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
import com.nepxion.discovery.common.entity.RuleEntity;
import com.nepxion.discovery.common.entity.StrategyEntity;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.strategy.zuul.filter.ZuulStrategyFilterResolver;
import com.netflix.zuul.ZuulFilter;
public class MyZuulFilter extends ZuulFilter {
@Autowired
private PluginAdapter pluginAdapter;
@Override
public String filterType() {
return "pre";
......@@ -31,8 +39,32 @@ public class MyZuulFilter extends ZuulFilter {
@Override
public Object run() {
ZuulStrategyFilterResolver.setHeader(DiscoveryConstant.N_D_VERSION, "{\"discovery-springcloud-example-a\":\"1.0\", \"discovery-springcloud-example-b\":\"1.0\", \"discovery-springcloud-example-c\":\"1.0;1.2\"}");
String routeVersion = getRouteVersionFromConfig();
// String routeVersion = getRouteVersionFromCustomer();
System.out.println("Route Version=" + routeVersion);
// 通过过滤器设置路由Header头部信息,来取代界面(Postman)上的设置,并全链路传递到服务端
ZuulStrategyFilterResolver.setHeader(DiscoveryConstant.N_D_VERSION, routeVersion);
return null;
}
// 从远程配置中心或者本地配置文件获取版本路由配置。如果是远程配置中心,则值会动态改变
protected String getRouteVersionFromConfig() {
RuleEntity ruleEntity = pluginAdapter.getRule();
if (ruleEntity != null) {
StrategyEntity strategyEntity = ruleEntity.getStrategyEntity();
if (strategyEntity != null) {
return strategyEntity.getVersionValue();
}
}
return null;
}
// 自定义版本路由配置
protected String getRouteVersionFromCustomer() {
return "{\"discovery-springcloud-example-a\":\"1.0\", \"discovery-springcloud-example-b\":\"1.0\", \"discovery-springcloud-example-c\":\"1.0;1.2\"}";
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment