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
ee774edd
Commit
ee774edd
authored
May 26, 2019
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加配置中心配置路由规则映射在网关过滤器的映射
parent
f367319f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
1 deletions
+48
-1
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/impl/MyGatewayFilter.java
+24
-1
discovery-springcloud-example-zuul/src/main/java/com/nepxion/discovery/plugin/example/zuul/impl/MyZuulFilter.java
+24
-0
No files found.
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/impl/MyGatewayFilter.java
View file @
ee774edd
...
...
@@ -33,10 +33,14 @@ public class MyGatewayFilter implements GlobalFilter, Ordered {
String
routeVersion
=
getRouteVersionFromConfig
();
// String routeVersion = getRouteVersionFromCustomer();
String
routeRegion
=
getRouteRegionFromConfig
();
// String routeRegion = getRouteRegionFromCustomer();
System
.
out
.
println
(
"Route Version="
+
routeVersion
);
System
.
out
.
println
(
"Route Region="
+
routeRegion
);
// 通过过滤器设置路由Header头部信息,来取代界面(Postman)上的设置,并全链路传递到服务端
ServerHttpRequest
newRequest
=
exchange
.
getRequest
().
mutate
().
header
(
DiscoveryConstant
.
N_D_VERSION
,
routeVersion
).
build
();
ServerHttpRequest
newRequest
=
exchange
.
getRequest
().
mutate
().
header
(
DiscoveryConstant
.
N_D_VERSION
,
routeVersion
).
header
(
DiscoveryConstant
.
N_D_REGION
,
routeRegion
).
build
();
ServerWebExchange
newExchange
=
exchange
.
mutate
().
request
(
newRequest
).
build
();
return
chain
.
filter
(
newExchange
);
...
...
@@ -61,8 +65,26 @@ public class MyGatewayFilter implements GlobalFilter, Ordered {
return
null
;
}
// 从远程配置中心或者本地配置文件获取区域路由配置。如果是远程配置中心,则值会动态改变
protected
String
getRouteRegionFromConfig
()
{
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
if
(
ruleEntity
!=
null
)
{
StrategyEntity
strategyEntity
=
ruleEntity
.
getStrategyEntity
();
if
(
strategyEntity
!=
null
)
{
return
strategyEntity
.
getRegionValue
();
}
}
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\"}"
;
}
// 自定义区域路由配置
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
discovery-springcloud-example-zuul/src/main/java/com/nepxion/discovery/plugin/example/zuul/impl/MyZuulFilter.java
View file @
ee774edd
...
...
@@ -42,10 +42,15 @@ public class MyZuulFilter extends ZuulFilter {
String
routeVersion
=
getRouteVersionFromConfig
();
// String routeVersion = getRouteVersionFromCustomer();
String
routeRegion
=
getRouteRegionFromConfig
();
// String routeRegion = getRouteRegionFromCustomer();
System
.
out
.
println
(
"Route Version="
+
routeVersion
);
System
.
out
.
println
(
"Route Region="
+
routeRegion
);
// 通过过滤器设置路由Header头部信息,来取代界面(Postman)上的设置,并全链路传递到服务端
ZuulStrategyFilterResolver
.
setHeader
(
DiscoveryConstant
.
N_D_VERSION
,
routeVersion
);
ZuulStrategyFilterResolver
.
setHeader
(
DiscoveryConstant
.
N_D_REGION
,
routeRegion
);
return
null
;
}
...
...
@@ -63,8 +68,26 @@ public class MyZuulFilter extends ZuulFilter {
return
null
;
}
// 从远程配置中心或者本地配置文件获取区域路由配置。如果是远程配置中心,则值会动态改变
protected
String
getRouteRegionFromConfig
()
{
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
if
(
ruleEntity
!=
null
)
{
StrategyEntity
strategyEntity
=
ruleEntity
.
getStrategyEntity
();
if
(
strategyEntity
!=
null
)
{
return
strategyEntity
.
getRegionValue
();
}
}
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\"}"
;
}
// 自定义区域路由配置
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
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