Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
discovery
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陈文顺
discovery
Commits
38677a95
Commit
38677a95
authored
May 26, 2019
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加配置中心配置路由规则映射在网关过滤器的映射
parent
ee774edd
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
112 additions
and
65 deletions
+112
-65
discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/constant/GatewayStrategyConstant.java
+3
-2
discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/filter/GatewayStrategyFilter.java
+2
-1
discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/filter/GatewayStrategyRouteFilter.java
+40
-26
discovery-plugin-strategy-starter-zuul/src/main/java/com/nepxion/discovery/plugin/strategy/zuul/constant/ZuulStrategyConstant.java
+16
-0
discovery-plugin-strategy-starter-zuul/src/main/java/com/nepxion/discovery/plugin/strategy/zuul/filter/ZuulStrategyRouteFilter.java
+34
-23
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/DiscoveryApplicationGateway.java
+5
-5
discovery-springcloud-example-gateway/src/main/resources/bootstrap.properties
+3
-3
discovery-springcloud-example-zuul/src/main/java/com/nepxion/discovery/plugin/example/zuul/DiscoveryApplicationZuul.java
+6
-5
discovery-springcloud-example-zuul/src/main/resources/bootstrap.properties
+3
-0
No files found.
discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/constant/GatewayStrategyConstant.java
View file @
38677a95
...
...
@@ -10,6 +10,6 @@ package com.nepxion.discovery.plugin.strategy.gateway.constant;
*/
public
class
GatewayStrategyConstant
{
public
static
final
String
SPRING_APPLICATION_STRATEGY_GATEWAY_
FILTER_ORDER
=
"spring.application.strategy.gateway
.filter.order"
;
public
static
final
int
SPRING_APPLICATION_STRATEGY_GATEWAY_
FILTER_ORDER_VALUE
=
8888
;
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_
ROUTE_FILTER_ORDER_VALUE
=
9000
;
}
\ No newline at end of file
discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/filter/GatewayStrategyFilter.java
View file @
38677a95
...
...
@@ -40,6 +40,6 @@ public class GatewayStrategyFilter implements GlobalFilter, Ordered {
@Override
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
discovery-
springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/impl/MyGateway
Filter.java
→
discovery-
plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/filter/GatewayStrategyRoute
Filter.java
View file @
38677a95
package
com
.
nepxion
.
discovery
.
plugin
.
example
.
gateway
.
impl
;
package
com
.
nepxion
.
discovery
.
plugin
.
strategy
.
gateway
.
filter
;
/**
* <p>Title: Nepxion Discovery</p>
...
...
@@ -11,10 +11,12 @@ package com.nepxion.discovery.plugin.example.gateway.impl;
import
reactor.core.publisher.Mono
;
import
org.apache.commons.lang3.StringUtils
;
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
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.http.server.reactive.ServerHttpRequest
;
import
org.springframework.web.server.ServerWebExchange
;
...
...
@@ -24,36 +26,44 @@ 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
{
public
class
GatewayStrategyRouteFilter
implements
GlobalFilter
,
Ordered
{
@Autowired
private
ConfigurableEnvironment
environment
;
@Autowired
private
PluginAdapter
pluginAdapter
;
@Override
public
Mono
<
Void
>
filter
(
ServerWebExchange
exchange
,
GatewayFilterChain
chain
)
{
String
routeVersion
=
getRouteVersionFromConfig
();
// String routeVersion = getRouteVersionFromCustomer();
String
routeRegion
=
getRouteRegionFromConfig
();
// String routeRegion = getRouteRegionFromCustomer();
public
int
getOrder
()
{
return
environment
.
getProperty
(
GatewayStrategyConstant
.
SPRING_APPLICATION_STRATEGY_GATEWAY_ROUTE_FILTER_ORDER
,
Integer
.
class
,
GatewayStrategyConstant
.
SPRING_APPLICATION_STRATEGY_GATEWAY_ROUTE_FILTER_ORDER_VALUE
);
}
System
.
out
.
println
(
"Route Version="
+
routeVersion
);
System
.
out
.
println
(
"Route Region="
+
routeRegion
);
@Override
public
Mono
<
Void
>
filter
(
ServerWebExchange
exchange
,
GatewayFilterChain
chain
)
{
String
routeVersion
=
getRouteVersion
();
String
routeRegion
=
getRouteRegion
();
String
routeAddress
=
getRouteAddress
();
// 通过过滤器设置路由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
();
return
chain
.
filter
(
newExchange
);
}
@Override
public
int
getOrder
()
{
// Order必须小于8888
return
GatewayStrategyConstant
.
SPRING_APPLICATION_STRATEGY_GATEWAY_FILTER_ORDER_VALUE
-
1
;
}
// 从远程配置中心或者本地配置文件获取版本路由配置。如果是远程配置中心,则值会动态改变
protected
String
getRouteVersion
FromConfig
()
{
protected
String
getRouteVersion
()
{
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
if
(
ruleEntity
!=
null
)
{
StrategyEntity
strategyEntity
=
ruleEntity
.
getStrategyEntity
();
...
...
@@ -66,7 +76,7 @@ public class MyGatewayFilter implements GlobalFilter, Ordered {
}
// 从远程配置中心或者本地配置文件获取区域路由配置。如果是远程配置中心,则值会动态改变
protected
String
getRouteRegion
FromConfig
()
{
protected
String
getRouteRegion
()
{
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
if
(
ruleEntity
!=
null
)
{
StrategyEntity
strategyEntity
=
ruleEntity
.
getStrategyEntity
();
...
...
@@ -78,13 +88,16 @@ public class MyGatewayFilter implements GlobalFilter, Ordered {
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\"}"
;
}
// 从远程配置中心或者本地配置文件获取IP地址和端口路由配置。如果是远程配置中心,则值会动态改变
protected
String
getRouteAddress
()
{
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
if
(
ruleEntity
!=
null
)
{
StrategyEntity
strategyEntity
=
ruleEntity
.
getStrategyEntity
();
if
(
strategyEntity
!=
null
)
{
return
strategyEntity
.
getAddressValue
();
}
}
// 自定义区域路由配置
protected
String
getRouteRegionFromCustomer
()
{
return
"{\"discovery-springcloud-example-a\":\"qa;dev\", \"discovery-springcloud-example-b\":\"dev\", \"discovery-springcloud-example-c\":\"qa\"}"
;
return
null
;
}
}
\ No newline at end of file
discovery-plugin-strategy-starter-zuul/src/main/java/com/nepxion/discovery/plugin/strategy/zuul/constant/ZuulStrategyConstant.java
0 → 100644
View file @
38677a95
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
discovery-
springcloud-example-zuul/src/main/java/com/nepxion/discovery/plugin/example/zuul/impl/MyZuul
Filter.java
→
discovery-
plugin-strategy-starter-zuul/src/main/java/com/nepxion/discovery/plugin/strategy/zuul/filter/ZuulStrategyRoute
Filter.java
View file @
38677a95
package
com
.
nepxion
.
discovery
.
plugin
.
example
.
zuul
.
impl
;
package
com
.
nepxion
.
discovery
.
plugin
.
strategy
.
zuul
.
filter
;
/**
* <p>Title: Nepxion Discovery</p>
...
...
@@ -9,16 +9,21 @@ package com.nepxion.discovery.plugin.example.zuul.impl;
* @version 1.0
*/
import
org.apache.commons.lang3.StringUtils
;
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.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.
constant.ZuulStrategyConstant
;
import
com.netflix.zuul.ZuulFilter
;
public
class
MyZuulFilter
extends
ZuulFilter
{
public
class
ZuulStrategyRouteFilter
extends
ZuulFilter
{
@Autowired
private
ConfigurableEnvironment
environment
;
@Autowired
private
PluginAdapter
pluginAdapter
;
...
...
@@ -29,7 +34,7 @@ public class MyZuulFilter extends ZuulFilter {
@Override
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
...
...
@@ -39,24 +44,26 @@ public class MyZuulFilter extends ZuulFilter {
@Override
public
Object
run
()
{
String
routeVersion
=
getRouteVersionFromConfig
();
// String routeVersion = getRouteVersionFromCustomer();
String
routeRegion
=
getRouteRegionFromConfig
();
// String routeRegion = getRouteRegionFromCustomer();
System
.
out
.
println
(
"Route Version="
+
routeVersion
);
System
.
out
.
println
(
"Route Region="
+
routeRegion
);
String
routeVersion
=
getRouteVersion
();
String
routeRegion
=
getRouteRegion
();
String
routeAddress
=
getRouteAddress
();
// 通过过滤器设置路由Header头部信息,来取代界面(Postman)上的设置,并全链路传递到服务端
ZuulStrategyFilterResolver
.
setHeader
(
DiscoveryConstant
.
N_D_VERSION
,
routeVersion
);
ZuulStrategyFilterResolver
.
setHeader
(
DiscoveryConstant
.
N_D_REGION
,
routeRegion
);
if
(
StringUtils
.
isNotEmpty
(
routeVersion
))
{
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
;
}
// 从远程配置中心或者本地配置文件获取版本路由配置。如果是远程配置中心,则值会动态改变
protected
String
getRouteVersion
FromConfig
()
{
protected
String
getRouteVersion
()
{
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
if
(
ruleEntity
!=
null
)
{
StrategyEntity
strategyEntity
=
ruleEntity
.
getStrategyEntity
();
...
...
@@ -69,7 +76,7 @@ public class MyZuulFilter extends ZuulFilter {
}
// 从远程配置中心或者本地配置文件获取区域路由配置。如果是远程配置中心,则值会动态改变
protected
String
getRouteRegion
FromConfig
()
{
protected
String
getRouteRegion
()
{
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
if
(
ruleEntity
!=
null
)
{
StrategyEntity
strategyEntity
=
ruleEntity
.
getStrategyEntity
();
...
...
@@ -81,13 +88,16 @@ public class MyZuulFilter extends ZuulFilter {
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\"}"
;
}
// 从远程配置中心或者本地配置文件获取IP地址和端口路由配置。如果是远程配置中心,则值会动态改变
protected
String
getRouteAddress
()
{
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
if
(
ruleEntity
!=
null
)
{
StrategyEntity
strategyEntity
=
ruleEntity
.
getStrategyEntity
();
if
(
strategyEntity
!=
null
)
{
return
strategyEntity
.
getAddressValue
();
}
}
// 自定义区域路由配置
protected
String
getRouteRegionFromCustomer
()
{
return
"{\"discovery-springcloud-example-a\":\"qa;dev\", \"discovery-springcloud-example-b\":\"dev\", \"discovery-springcloud-example-c\":\"qa\"}"
;
return
null
;
}
}
\ No newline at end of file
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/DiscoveryApplicationGateway.java
View file @
38677a95
...
...
@@ -15,7 +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.MyGateway
Filter
;
import
com.nepxion.discovery.plugin.
strategy.gateway.filter.GatewayStrategyRoute
Filter
;
@SpringBootApplication
@EnableDiscoveryClient
...
...
@@ -25,13 +25,13 @@ public class DiscoveryApplicationGateway {
}
@Bean
public
MyDiscoveryEnabledStrategy
myDiscoveryEnabledStrategy
()
{
return
new
MyDiscoveryEnabledStrategy
();
public
GatewayStrategyRouteFilter
gatewayStrategyRouteFilter
()
{
return
new
GatewayStrategyRouteFilter
();
}
@Bean
public
My
GatewayFilter
myGatewayFilter
()
{
return
new
My
GatewayFilter
();
public
My
DiscoveryEnabledStrategy
myDiscoveryEnabledStrategy
()
{
return
new
My
DiscoveryEnabledStrategy
();
}
/*@Bean
...
...
discovery-springcloud-example-gateway/src/main/resources/bootstrap.properties
View file @
38677a95
...
...
@@ -73,5 +73,5 @@ spring.boot.admin.client.url=http://localhost:5555
# spring.application.strategy.control.enabled=true
# 开启和关闭Ribbon默认的ZoneAvoidanceRule负载均衡策略。一旦关闭,则使用RoundRobin简单轮询负载均衡策略。缺失则默认为true
# spring.application.strategy.zone.avoidance.rule.enabled=true
# 路由策略过滤器的执行顺序(Order)。缺失则默认为8888
#
spring.application.strategy.gateway.filter.order
=
8888
\ No newline at end of file
# 路由策略过滤器的执行顺序(Order)。缺失则默认为9000
#
spring.application.strategy.gateway.route.filter.order
=
9000
\ No newline at end of file
discovery-springcloud-example-zuul/src/main/java/com/nepxion/discovery/plugin/example/zuul/DiscoveryApplicationZuul.java
View file @
38677a95
...
...
@@ -16,7 +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.MyZuul
Filter
;
import
com.nepxion.discovery.plugin.
strategy.zuul.filter.ZuulStrategyRoute
Filter
;
@SpringBootApplication
@EnableDiscoveryClient
...
...
@@ -27,12 +27,12 @@ public class DiscoveryApplicationZuul {
}
@Bean
public
MyDiscoveryEnabledStrategy
myDiscoveryEnabledStrategy
()
{
return
new
MyDiscoveryEnabledStrategy
();
public
ZuulStrategyRouteFilter
zuulStrategyRouteFilter
()
{
return
new
ZuulStrategyRouteFilter
();
}
@Bean
public
My
ZuulFilter
myZuulFilter
()
{
return
new
My
ZuulFilter
();
public
My
DiscoveryEnabledStrategy
myDiscoveryEnabledStrategy
()
{
return
new
My
DiscoveryEnabledStrategy
();
}
}
\ No newline at end of file
discovery-springcloud-example-zuul/src/main/resources/bootstrap.properties
View file @
38677a95
...
...
@@ -73,6 +73,8 @@ spring.boot.admin.client.url=http://localhost:5555
# spring.application.strategy.control.enabled=true
# 开启和关闭Ribbon默认的ZoneAvoidanceRule负载均衡策略。一旦关闭,则使用RoundRobin简单轮询负载均衡策略。缺失则默认为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.ribbon-isolation-strategy=thread
#
spring.application.strategy.hystrix.threadlocal.supported
=
true
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment