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
776b34ee
Commit
776b34ee
authored
May 21, 2019
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
支持在网关过滤器中从远程配置中心获取路由信息配置,并全链路传递到服务端
parent
694c4528
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
6 deletions
+74
-6
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/DiscoveryApplicationGateway.java
+3
-2
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/impl/MyGatewayFilter.java
+33
-1
discovery-springcloud-example-zuul/src/main/java/com/nepxion/discovery/plugin/example/zuul/DiscoveryApplicationZuul.java
+4
-2
discovery-springcloud-example-zuul/src/main/java/com/nepxion/discovery/plugin/example/zuul/impl/MyZuulFilter.java
+34
-1
No files found.
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/DiscoveryApplicationGateway.java
View file @
776b34ee
...
@@ -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) {
...
...
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/impl/MyGatewayFilter.java
View file @
776b34ee
...
@@ -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
discovery-springcloud-example-zuul/src/main/java/com/nepxion/discovery/plugin/example/zuul/DiscoveryApplicationZuul.java
View file @
776b34ee
...
@@ -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
discovery-springcloud-example-zuul/src/main/java/com/nepxion/discovery/plugin/example/zuul/impl/MyZuulFilter.java
View file @
776b34ee
...
@@ -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
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