Commit 38677a95 by Nepxion

增加配置中心配置路由规则映射在网关过滤器的映射

parent ee774edd
...@@ -10,6 +10,6 @@ package com.nepxion.discovery.plugin.strategy.gateway.constant; ...@@ -10,6 +10,6 @@ package com.nepxion.discovery.plugin.strategy.gateway.constant;
*/ */
public class GatewayStrategyConstant { public class GatewayStrategyConstant {
public static final String SPRING_APPLICATION_STRATEGY_GATEWAY_FILTER_ORDER = "spring.application.strategy.gateway.filter.order"; public static final String SPRING_APPLICATION_STRATEGY_GATEWAY_ROUTE_FILTER_ORDER = "spring.application.strategy.gateway.route.filter.order";
public static final int SPRING_APPLICATION_STRATEGY_GATEWAY_FILTER_ORDER_VALUE = 8888; public static final int SPRING_APPLICATION_STRATEGY_GATEWAY_ROUTE_FILTER_ORDER_VALUE = 9000;
} }
\ No newline at end of file
...@@ -40,6 +40,6 @@ public class GatewayStrategyFilter implements GlobalFilter, Ordered { ...@@ -40,6 +40,6 @@ public class GatewayStrategyFilter implements GlobalFilter, Ordered {
@Override @Override
public int getOrder() { public int getOrder() {
return environment.getProperty(GatewayStrategyConstant.SPRING_APPLICATION_STRATEGY_GATEWAY_FILTER_ORDER, Integer.class, GatewayStrategyConstant.SPRING_APPLICATION_STRATEGY_GATEWAY_FILTER_ORDER_VALUE); return environment.getProperty(GatewayStrategyConstant.SPRING_APPLICATION_STRATEGY_GATEWAY_ROUTE_FILTER_ORDER, Integer.class, GatewayStrategyConstant.SPRING_APPLICATION_STRATEGY_GATEWAY_ROUTE_FILTER_ORDER_VALUE) + 1;
} }
} }
\ No newline at end of file
package com.nepxion.discovery.plugin.example.gateway.impl; package com.nepxion.discovery.plugin.strategy.gateway.filter;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
...@@ -11,10 +11,12 @@ package com.nepxion.discovery.plugin.example.gateway.impl; ...@@ -11,10 +11,12 @@ package com.nepxion.discovery.plugin.example.gateway.impl;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; 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;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
...@@ -24,36 +26,44 @@ import com.nepxion.discovery.common.entity.StrategyEntity; ...@@ -24,36 +26,44 @@ import com.nepxion.discovery.common.entity.StrategyEntity;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter; 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 GatewayStrategyRouteFilter implements GlobalFilter, Ordered {
@Autowired
private ConfigurableEnvironment environment;
@Autowired @Autowired
private PluginAdapter pluginAdapter; private PluginAdapter pluginAdapter;
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { public int getOrder() {
String routeVersion = getRouteVersionFromConfig(); return environment.getProperty(GatewayStrategyConstant.SPRING_APPLICATION_STRATEGY_GATEWAY_ROUTE_FILTER_ORDER, Integer.class, GatewayStrategyConstant.SPRING_APPLICATION_STRATEGY_GATEWAY_ROUTE_FILTER_ORDER_VALUE);
// String routeVersion = getRouteVersionFromCustomer(); }
String routeRegion = getRouteRegionFromConfig();
// String routeRegion = getRouteRegionFromCustomer();
System.out.println("Route Version=" + routeVersion); @Override
System.out.println("Route Region=" + routeRegion); public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String routeVersion = getRouteVersion();
String routeRegion = getRouteRegion();
String routeAddress = getRouteAddress();
// 通过过滤器设置路由Header头部信息,来取代界面(Postman)上的设置,并全链路传递到服务端 // 通过过滤器设置路由Header头部信息,来取代界面(Postman)上的设置,并全链路传递到服务端
ServerHttpRequest newRequest = exchange.getRequest().mutate().header(DiscoveryConstant.N_D_VERSION, routeVersion).header(DiscoveryConstant.N_D_REGION, routeRegion).build(); ServerHttpRequest.Builder requestBuilder = exchange.getRequest().mutate();
if (StringUtils.isNotEmpty(routeVersion)) {
requestBuilder.header(DiscoveryConstant.N_D_VERSION, routeVersion);
}
if (StringUtils.isNotEmpty(routeRegion)) {
requestBuilder.header(DiscoveryConstant.N_D_REGION, routeRegion);
}
if (StringUtils.isNotEmpty(routeAddress)) {
requestBuilder.header(DiscoveryConstant.N_D_ADDRESS, routeAddress);
}
ServerHttpRequest newRequest = requestBuilder.build();
ServerWebExchange newExchange = exchange.mutate().request(newRequest).build(); ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();
return chain.filter(newExchange); return chain.filter(newExchange);
} }
@Override
public int getOrder() {
// Order必须小于8888
return GatewayStrategyConstant.SPRING_APPLICATION_STRATEGY_GATEWAY_FILTER_ORDER_VALUE - 1;
}
// 从远程配置中心或者本地配置文件获取版本路由配置。如果是远程配置中心,则值会动态改变 // 从远程配置中心或者本地配置文件获取版本路由配置。如果是远程配置中心,则值会动态改变
protected String getRouteVersionFromConfig() { protected String getRouteVersion() {
RuleEntity ruleEntity = pluginAdapter.getRule(); RuleEntity ruleEntity = pluginAdapter.getRule();
if (ruleEntity != null) { if (ruleEntity != null) {
StrategyEntity strategyEntity = ruleEntity.getStrategyEntity(); StrategyEntity strategyEntity = ruleEntity.getStrategyEntity();
...@@ -66,7 +76,7 @@ public class MyGatewayFilter implements GlobalFilter, Ordered { ...@@ -66,7 +76,7 @@ public class MyGatewayFilter implements GlobalFilter, Ordered {
} }
// 从远程配置中心或者本地配置文件获取区域路由配置。如果是远程配置中心,则值会动态改变 // 从远程配置中心或者本地配置文件获取区域路由配置。如果是远程配置中心,则值会动态改变
protected String getRouteRegionFromConfig() { protected String getRouteRegion() {
RuleEntity ruleEntity = pluginAdapter.getRule(); RuleEntity ruleEntity = pluginAdapter.getRule();
if (ruleEntity != null) { if (ruleEntity != null) {
StrategyEntity strategyEntity = ruleEntity.getStrategyEntity(); StrategyEntity strategyEntity = ruleEntity.getStrategyEntity();
...@@ -78,13 +88,16 @@ public class MyGatewayFilter implements GlobalFilter, Ordered { ...@@ -78,13 +88,16 @@ public class MyGatewayFilter implements GlobalFilter, Ordered {
return null; return null;
} }
// 自定义版本路由配置 // 从远程配置中心或者本地配置文件获取IP地址和端口路由配置。如果是远程配置中心,则值会动态改变
protected String getRouteVersionFromCustomer() { protected String getRouteAddress() {
return "{\"discovery-springcloud-example-a\":\"1.0\", \"discovery-springcloud-example-b\":\"1.0\", \"discovery-springcloud-example-c\":\"1.0;1.2\"}"; RuleEntity ruleEntity = pluginAdapter.getRule();
} if (ruleEntity != null) {
StrategyEntity strategyEntity = ruleEntity.getStrategyEntity();
if (strategyEntity != null) {
return strategyEntity.getAddressValue();
}
}
// 自定义区域路由配置 return null;
protected String getRouteRegionFromCustomer() {
return "{\"discovery-springcloud-example-a\":\"qa;dev\", \"discovery-springcloud-example-b\":\"dev\", \"discovery-springcloud-example-c\":\"qa\"}";
} }
} }
\ No newline at end of file
package com.nepxion.discovery.plugin.strategy.zuul.constant;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
public class ZuulStrategyConstant {
public static final String SPRING_APPLICATION_STRATEGY_ZUUL_ROUTE_FILTER_ORDER = "spring.application.strategy.zuul.route.filter.order";
public static final int SPRING_APPLICATION_STRATEGY_ZUUL_ROUTE_FILTER_ORDER_VALUE = 0;
}
\ No newline at end of file
package com.nepxion.discovery.plugin.example.zuul.impl; package com.nepxion.discovery.plugin.strategy.zuul.filter;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
...@@ -9,16 +9,21 @@ package com.nepxion.discovery.plugin.example.zuul.impl; ...@@ -9,16 +9,21 @@ package com.nepxion.discovery.plugin.example.zuul.impl;
* @version 1.0 * @version 1.0
*/ */
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
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.RuleEntity;
import com.nepxion.discovery.common.entity.StrategyEntity; import com.nepxion.discovery.common.entity.StrategyEntity;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter; import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.strategy.zuul.filter.ZuulStrategyFilterResolver; import com.nepxion.discovery.plugin.strategy.zuul.constant.ZuulStrategyConstant;
import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.ZuulFilter;
public class MyZuulFilter extends ZuulFilter { public class ZuulStrategyRouteFilter extends ZuulFilter {
@Autowired
private ConfigurableEnvironment environment;
@Autowired @Autowired
private PluginAdapter pluginAdapter; private PluginAdapter pluginAdapter;
...@@ -29,7 +34,7 @@ public class MyZuulFilter extends ZuulFilter { ...@@ -29,7 +34,7 @@ public class MyZuulFilter extends ZuulFilter {
@Override @Override
public int filterOrder() { public int filterOrder() {
return 0; return environment.getProperty(ZuulStrategyConstant.SPRING_APPLICATION_STRATEGY_ZUUL_ROUTE_FILTER_ORDER, Integer.class, ZuulStrategyConstant.SPRING_APPLICATION_STRATEGY_ZUUL_ROUTE_FILTER_ORDER_VALUE);
} }
@Override @Override
...@@ -39,24 +44,26 @@ public class MyZuulFilter extends ZuulFilter { ...@@ -39,24 +44,26 @@ public class MyZuulFilter extends ZuulFilter {
@Override @Override
public Object run() { public Object run() {
String routeVersion = getRouteVersionFromConfig(); String routeVersion = getRouteVersion();
// String routeVersion = getRouteVersionFromCustomer(); String routeRegion = getRouteRegion();
String routeAddress = getRouteAddress();
String routeRegion = getRouteRegionFromConfig();
// String routeRegion = getRouteRegionFromCustomer();
System.out.println("Route Version=" + routeVersion);
System.out.println("Route Region=" + routeRegion);
// 通过过滤器设置路由Header头部信息,来取代界面(Postman)上的设置,并全链路传递到服务端 // 通过过滤器设置路由Header头部信息,来取代界面(Postman)上的设置,并全链路传递到服务端
ZuulStrategyFilterResolver.setHeader(DiscoveryConstant.N_D_VERSION, routeVersion); if (StringUtils.isNotEmpty(routeVersion)) {
ZuulStrategyFilterResolver.setHeader(DiscoveryConstant.N_D_REGION, routeRegion); ZuulStrategyFilterResolver.setHeader(DiscoveryConstant.N_D_VERSION, routeVersion);
}
if (StringUtils.isNotEmpty(routeRegion)) {
ZuulStrategyFilterResolver.setHeader(DiscoveryConstant.N_D_REGION, routeRegion);
}
if (StringUtils.isNotEmpty(routeAddress)) {
ZuulStrategyFilterResolver.setHeader(DiscoveryConstant.N_D_ADDRESS, routeAddress);
}
return null; return null;
} }
// 从远程配置中心或者本地配置文件获取版本路由配置。如果是远程配置中心,则值会动态改变 // 从远程配置中心或者本地配置文件获取版本路由配置。如果是远程配置中心,则值会动态改变
protected String getRouteVersionFromConfig() { protected String getRouteVersion() {
RuleEntity ruleEntity = pluginAdapter.getRule(); RuleEntity ruleEntity = pluginAdapter.getRule();
if (ruleEntity != null) { if (ruleEntity != null) {
StrategyEntity strategyEntity = ruleEntity.getStrategyEntity(); StrategyEntity strategyEntity = ruleEntity.getStrategyEntity();
...@@ -69,7 +76,7 @@ public class MyZuulFilter extends ZuulFilter { ...@@ -69,7 +76,7 @@ public class MyZuulFilter extends ZuulFilter {
} }
// 从远程配置中心或者本地配置文件获取区域路由配置。如果是远程配置中心,则值会动态改变 // 从远程配置中心或者本地配置文件获取区域路由配置。如果是远程配置中心,则值会动态改变
protected String getRouteRegionFromConfig() { protected String getRouteRegion() {
RuleEntity ruleEntity = pluginAdapter.getRule(); RuleEntity ruleEntity = pluginAdapter.getRule();
if (ruleEntity != null) { if (ruleEntity != null) {
StrategyEntity strategyEntity = ruleEntity.getStrategyEntity(); StrategyEntity strategyEntity = ruleEntity.getStrategyEntity();
...@@ -81,13 +88,16 @@ public class MyZuulFilter extends ZuulFilter { ...@@ -81,13 +88,16 @@ public class MyZuulFilter extends ZuulFilter {
return null; return null;
} }
// 自定义版本路由配置 // 从远程配置中心或者本地配置文件获取IP地址和端口路由配置。如果是远程配置中心,则值会动态改变
protected String getRouteVersionFromCustomer() { protected String getRouteAddress() {
return "{\"discovery-springcloud-example-a\":\"1.0\", \"discovery-springcloud-example-b\":\"1.0\", \"discovery-springcloud-example-c\":\"1.0;1.2\"}"; RuleEntity ruleEntity = pluginAdapter.getRule();
} if (ruleEntity != null) {
StrategyEntity strategyEntity = ruleEntity.getStrategyEntity();
if (strategyEntity != null) {
return strategyEntity.getAddressValue();
}
}
// 自定义区域路由配置 return null;
protected String getRouteRegionFromCustomer() {
return "{\"discovery-springcloud-example-a\":\"qa;dev\", \"discovery-springcloud-example-b\":\"dev\", \"discovery-springcloud-example-c\":\"qa\"}";
} }
} }
\ No newline at end of file
...@@ -15,7 +15,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; ...@@ -15,7 +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; import com.nepxion.discovery.plugin.strategy.gateway.filter.GatewayStrategyRouteFilter;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
...@@ -25,13 +25,13 @@ public class DiscoveryApplicationGateway { ...@@ -25,13 +25,13 @@ public class DiscoveryApplicationGateway {
} }
@Bean @Bean
public MyDiscoveryEnabledStrategy myDiscoveryEnabledStrategy() { public GatewayStrategyRouteFilter gatewayStrategyRouteFilter() {
return new MyDiscoveryEnabledStrategy(); return new GatewayStrategyRouteFilter();
} }
@Bean @Bean
public MyGatewayFilter myGatewayFilter() { public MyDiscoveryEnabledStrategy myDiscoveryEnabledStrategy() {
return new MyGatewayFilter(); return new MyDiscoveryEnabledStrategy();
} }
/*@Bean /*@Bean
......
...@@ -73,5 +73,5 @@ spring.boot.admin.client.url=http://localhost:5555 ...@@ -73,5 +73,5 @@ spring.boot.admin.client.url=http://localhost:5555
# spring.application.strategy.control.enabled=true # spring.application.strategy.control.enabled=true
# 开启和关闭Ribbon默认的ZoneAvoidanceRule负载均衡策略。一旦关闭,则使用RoundRobin简单轮询负载均衡策略。缺失则默认为true # 开启和关闭Ribbon默认的ZoneAvoidanceRule负载均衡策略。一旦关闭,则使用RoundRobin简单轮询负载均衡策略。缺失则默认为true
# spring.application.strategy.zone.avoidance.rule.enabled=true # spring.application.strategy.zone.avoidance.rule.enabled=true
# 路由策略过滤器的执行顺序(Order)。缺失则默认为8888 # 路由策略过滤器的执行顺序(Order)。缺失则默认为9000
# spring.application.strategy.gateway.filter.order=8888 # spring.application.strategy.gateway.route.filter.order=9000
\ No newline at end of file \ No newline at end of file
...@@ -16,7 +16,7 @@ import org.springframework.cloud.netflix.zuul.EnableZuulProxy; ...@@ -16,7 +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; import com.nepxion.discovery.plugin.strategy.zuul.filter.ZuulStrategyRouteFilter;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
...@@ -27,12 +27,12 @@ public class DiscoveryApplicationZuul { ...@@ -27,12 +27,12 @@ public class DiscoveryApplicationZuul {
} }
@Bean @Bean
public MyDiscoveryEnabledStrategy myDiscoveryEnabledStrategy() { public ZuulStrategyRouteFilter zuulStrategyRouteFilter() {
return new MyDiscoveryEnabledStrategy(); return new ZuulStrategyRouteFilter();
} }
@Bean @Bean
public MyZuulFilter myZuulFilter() { public MyDiscoveryEnabledStrategy myDiscoveryEnabledStrategy() {
return new MyZuulFilter(); return new MyDiscoveryEnabledStrategy();
} }
} }
\ No newline at end of file
...@@ -73,6 +73,8 @@ spring.boot.admin.client.url=http://localhost:5555 ...@@ -73,6 +73,8 @@ spring.boot.admin.client.url=http://localhost:5555
# spring.application.strategy.control.enabled=true # spring.application.strategy.control.enabled=true
# 开启和关闭Ribbon默认的ZoneAvoidanceRule负载均衡策略。一旦关闭,则使用RoundRobin简单轮询负载均衡策略。缺失则默认为true # 开启和关闭Ribbon默认的ZoneAvoidanceRule负载均衡策略。一旦关闭,则使用RoundRobin简单轮询负载均衡策略。缺失则默认为true
# spring.application.strategy.zone.avoidance.rule.enabled=true # spring.application.strategy.zone.avoidance.rule.enabled=true
# 路由策略过滤器的执行顺序(Order)。缺失则默认为0
# spring.application.strategy.zuul.route.filter.order=0
# 开启Zuul网关上实现Hystrix线程隔离模式做服务隔离时,必须把spring.application.strategy.hystrix.threadlocal.supported设置为true,同时要引入discovery-plugin-strategy-starter-hystrix包,否则线程切换时会发生ThreadLocal上下文对象丢失 # 开启Zuul网关上实现Hystrix线程隔离模式做服务隔离时,必须把spring.application.strategy.hystrix.threadlocal.supported设置为true,同时要引入discovery-plugin-strategy-starter-hystrix包,否则线程切换时会发生ThreadLocal上下文对象丢失
# zuul.ribbon-isolation-strategy=thread # zuul.ribbon-isolation-strategy=thread
# spring.application.strategy.hystrix.threadlocal.supported=true # spring.application.strategy.hystrix.threadlocal.supported=true
\ 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