Commit 776b34ee by Nepxion

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

parent 694c4528
...@@ -15,6 +15,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; ...@@ -15,6 +15,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import com.nepxion.discovery.plugin.example.gateway.impl.MyDiscoveryEnabledStrategy; import com.nepxion.discovery.plugin.example.gateway.impl.MyDiscoveryEnabledStrategy;
import com.nepxion.discovery.plugin.example.gateway.impl.MyGatewayFilter;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
...@@ -28,10 +29,10 @@ public class DiscoveryApplicationGateway { ...@@ -28,10 +29,10 @@ public class DiscoveryApplicationGateway {
return new MyDiscoveryEnabledStrategy(); return new MyDiscoveryEnabledStrategy();
} }
/*@Bean @Bean
public MyGatewayFilter myGatewayFilter() { public MyGatewayFilter myGatewayFilter() {
return new MyGatewayFilter(); return new MyGatewayFilter();
}*/ }
/*@Bean /*@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) { public RouteLocator routeLocator(RouteLocatorBuilder builder) {
......
...@@ -11,6 +11,7 @@ package com.nepxion.discovery.plugin.example.gateway.impl; ...@@ -11,6 +11,7 @@ package com.nepxion.discovery.plugin.example.gateway.impl;
import reactor.core.publisher.Mono; 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.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
...@@ -18,12 +19,24 @@ import org.springframework.http.server.reactive.ServerHttpRequest; ...@@ -18,12 +19,24 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import com.nepxion.discovery.common.constant.DiscoveryConstant; 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; import com.nepxion.discovery.plugin.strategy.gateway.constant.GatewayStrategyConstant;
public class MyGatewayFilter implements GlobalFilter, Ordered { public class MyGatewayFilter implements GlobalFilter, Ordered {
@Autowired
private PluginAdapter pluginAdapter;
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { 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(); ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();
return chain.filter(newExchange); return chain.filter(newExchange);
...@@ -34,4 +47,22 @@ public class MyGatewayFilter implements GlobalFilter, Ordered { ...@@ -34,4 +47,22 @@ public class MyGatewayFilter implements GlobalFilter, Ordered {
// Order必须小于8888 // Order必须小于8888
return GatewayStrategyConstant.SPRING_APPLICATION_STRATEGY_GATEWAY_FILTER_ORDER_VALUE - 1; 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; ...@@ -16,6 +16,7 @@ import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import com.nepxion.discovery.plugin.example.zuul.impl.MyDiscoveryEnabledStrategy; import com.nepxion.discovery.plugin.example.zuul.impl.MyDiscoveryEnabledStrategy;
import com.nepxion.discovery.plugin.example.zuul.impl.MyZuulFilter;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
...@@ -30,8 +31,8 @@ public class DiscoveryApplicationZuul { ...@@ -30,8 +31,8 @@ public class DiscoveryApplicationZuul {
return new MyDiscoveryEnabledStrategy(); return new MyDiscoveryEnabledStrategy();
} }
/*@Bean @Bean
public MyZuulFilter myZuulFilter() { public MyZuulFilter myZuulFilter() {
return new MyZuulFilter(); return new MyZuulFilter();
}*/ }
} }
\ No newline at end of file
...@@ -9,11 +9,19 @@ package com.nepxion.discovery.plugin.example.zuul.impl; ...@@ -9,11 +9,19 @@ package com.nepxion.discovery.plugin.example.zuul.impl;
* @version 1.0 * @version 1.0
*/ */
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.common.constant.DiscoveryConstant; 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.nepxion.discovery.plugin.strategy.zuul.filter.ZuulStrategyFilterResolver;
import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.ZuulFilter;
public class MyZuulFilter extends ZuulFilter { public class MyZuulFilter extends ZuulFilter {
@Autowired
private PluginAdapter pluginAdapter;
@Override @Override
public String filterType() { public String filterType() {
return "pre"; return "pre";
...@@ -31,8 +39,32 @@ public class MyZuulFilter extends ZuulFilter { ...@@ -31,8 +39,32 @@ public class MyZuulFilter extends ZuulFilter {
@Override @Override
public Object run() { 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; 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