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
63ba70d2
Commit
63ba70d2
authored
May 29, 2019
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改网关端灰度路由示例
parent
842004ae
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
12 deletions
+28
-12
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/impl/MyDiscoveryEnabledStrategy.java
+14
-6
discovery-springcloud-example-zuul/src/main/java/com/nepxion/discovery/plugin/example/zuul/impl/MyDiscoveryEnabledStrategy.java
+14
-6
No files found.
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/impl/MyDiscoveryEnabledStrategy.java
View file @
63ba70d2
...
@@ -16,6 +16,7 @@ import org.slf4j.Logger;
...
@@ -16,6 +16,7 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledStrategy
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledStrategy
;
import
com.nepxion.discovery.plugin.strategy.gateway.context.GatewayStrategyContextHolder
;
import
com.nepxion.discovery.plugin.strategy.gateway.context.GatewayStrategyContextHolder
;
...
@@ -39,17 +40,24 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
...
@@ -39,17 +40,24 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
// 根据Rest调用传来的Header参数(例如Token),选取执行调用请求的服务实例
// 根据Rest调用传来的Header参数(例如Token),选取执行调用请求的服务实例
private
boolean
applyFromHeader
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
private
boolean
applyFromHeader
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
String
token
=
gatewayStrategyContextHolder
.
getHeader
(
"token"
);
String
mobile
=
gatewayStrategyContextHolder
.
getHeader
(
"mobile"
);
String
version
=
metadata
.
get
(
DiscoveryConstant
.
VERSION
);
String
serviceId
=
pluginAdapter
.
getServerServiceId
(
server
);
String
serviceId
=
pluginAdapter
.
getServerServiceId
(
server
);
LOG
.
info
(
"Gateway端负载均衡用户定制触发:token={}, serviceId={}, metadata={}"
,
token
,
serviceId
,
metadata
);
LOG
.
info
(
"Gateway端负载均衡用户定制触发:mobile={}, serviceId={}, metadata={}"
,
mobile
,
serviceId
,
metadata
);
String
filterToken
=
"abc"
;
if
(
StringUtils
.
isNotEmpty
(
token
)
&&
token
.
contains
(
filterToken
))
{
LOG
.
info
(
"过滤条件:当Token含有'{}'的时候,不能被Ribbon负载均衡到"
,
filterToken
);
if
(
StringUtils
.
isNotEmpty
(
mobile
))
{
// 手机号以移动138开头,路由到1.0版本的服务上
if
(
mobile
.
startsWith
(
"138"
)
&&
StringUtils
.
equals
(
version
,
"1.0"
))
{
return
true
;
// 手机号以联通133开头,路由到2.0版本的服务上
}
else
if
(
mobile
.
startsWith
(
"133"
)
&&
StringUtils
.
equals
(
version
,
"1.1"
))
{
return
true
;
}
else
{
// 其它情况,直接拒绝请求
return
false
;
return
false
;
}
}
}
return
true
;
return
true
;
}
}
...
...
discovery-springcloud-example-zuul/src/main/java/com/nepxion/discovery/plugin/example/zuul/impl/MyDiscoveryEnabledStrategy.java
View file @
63ba70d2
...
@@ -16,6 +16,7 @@ import org.slf4j.Logger;
...
@@ -16,6 +16,7 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledStrategy
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledStrategy
;
import
com.nepxion.discovery.plugin.strategy.zuul.context.ZuulStrategyContextHolder
;
import
com.nepxion.discovery.plugin.strategy.zuul.context.ZuulStrategyContextHolder
;
...
@@ -39,17 +40,24 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
...
@@ -39,17 +40,24 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
// 根据Rest调用传来的Header参数(例如Token),选取执行调用请求的服务实例
// 根据Rest调用传来的Header参数(例如Token),选取执行调用请求的服务实例
private
boolean
applyFromHeader
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
private
boolean
applyFromHeader
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
String
token
=
zuulStrategyContextHolder
.
getHeader
(
"token"
);
String
mobile
=
zuulStrategyContextHolder
.
getHeader
(
"mobile"
);
String
version
=
metadata
.
get
(
DiscoveryConstant
.
VERSION
);
String
serviceId
=
pluginAdapter
.
getServerServiceId
(
server
);
String
serviceId
=
pluginAdapter
.
getServerServiceId
(
server
);
LOG
.
info
(
"Zuul端负载均衡用户定制触发:token={}, serviceId={}, metadata={}"
,
token
,
serviceId
,
metadata
);
LOG
.
info
(
"Zuul端负载均衡用户定制触发:mobile={}, serviceId={}, metadata={}"
,
mobile
,
serviceId
,
metadata
);
String
filterToken
=
"abc"
;
if
(
StringUtils
.
isNotEmpty
(
token
)
&&
token
.
contains
(
filterToken
))
{
LOG
.
info
(
"过滤条件:当Token含有'{}'的时候,不能被Ribbon负载均衡到"
,
filterToken
);
if
(
StringUtils
.
isNotEmpty
(
mobile
))
{
// 手机号以移动138开头,路由到1.0版本的服务上
if
(
mobile
.
startsWith
(
"138"
)
&&
StringUtils
.
equals
(
version
,
"1.0"
))
{
return
true
;
// 手机号以联通133开头,路由到2.0版本的服务上
}
else
if
(
mobile
.
startsWith
(
"133"
)
&&
StringUtils
.
equals
(
version
,
"1.1"
))
{
return
true
;
}
else
{
// 其它情况,直接拒绝请求
return
false
;
return
false
;
}
}
}
return
true
;
return
true
;
}
}
...
...
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