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
cca8b7e8
Commit
cca8b7e8
authored
Jun 02, 2019
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化自定义策略的接口
parent
57956175
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
37 additions
and
49 deletions
+37
-49
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/AbstractDiscoveryEnabledAdapter.java
+12
-10
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/DiscoveryEnabledAdapter.java
+2
-3
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/DiscoveryEnabledStrategy.java
+2
-3
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/rule/DiscoveryEnabledBasePredicate.java
+1
-5
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/rule/DiscoveryEnabledZoneAvoidancePredicate.java
+1
-5
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/impl/MyDiscoveryEnabledStrategy.java
+5
-7
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/impl/MyDiscoveryEnabledStrategy.java
+9
-9
discovery-springcloud-example-zuul/src/main/java/com/nepxion/discovery/plugin/example/zuul/impl/MyDiscoveryEnabledStrategy.java
+5
-7
No files found.
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/AbstractDiscoveryEnabledAdapter.java
View file @
cca8b7e8
...
@@ -31,27 +31,27 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
...
@@ -31,27 +31,27 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
protected
PluginAdapter
pluginAdapter
;
protected
PluginAdapter
pluginAdapter
;
@Override
@Override
public
boolean
apply
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
public
boolean
apply
(
Server
server
)
{
boolean
enabled
=
applyVersion
(
server
,
metadata
);
boolean
enabled
=
applyVersion
(
server
);
if
(!
enabled
)
{
if
(!
enabled
)
{
return
false
;
return
false
;
}
}
enabled
=
applyRegion
(
server
,
metadata
);
enabled
=
applyRegion
(
server
);
if
(!
enabled
)
{
if
(!
enabled
)
{
return
false
;
return
false
;
}
}
enabled
=
applyAddress
(
server
,
metadata
);
enabled
=
applyAddress
(
server
);
if
(!
enabled
)
{
if
(!
enabled
)
{
return
false
;
return
false
;
}
}
return
applyStrategy
(
server
,
metadata
);
return
applyStrategy
(
server
);
}
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
private
boolean
applyVersion
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
private
boolean
applyVersion
(
Server
server
)
{
String
versionValue
=
getVersionValue
(
server
);
String
versionValue
=
getVersionValue
(
server
);
if
(
StringUtils
.
isEmpty
(
versionValue
))
{
if
(
StringUtils
.
isEmpty
(
versionValue
))
{
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
...
@@ -67,6 +67,7 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
...
@@ -67,6 +67,7 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
return
true
;
return
true
;
}
}
Map
<
String
,
String
>
metadata
=
pluginAdapter
.
getServerMetadata
(
server
);
String
version
=
metadata
.
get
(
DiscoveryConstant
.
VERSION
);
String
version
=
metadata
.
get
(
DiscoveryConstant
.
VERSION
);
if
(
StringUtils
.
isEmpty
(
version
))
{
if
(
StringUtils
.
isEmpty
(
version
))
{
return
false
;
return
false
;
...
@@ -94,7 +95,7 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
...
@@ -94,7 +95,7 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
}
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
private
boolean
applyRegion
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
private
boolean
applyRegion
(
Server
server
)
{
String
regionValue
=
getRegionValue
(
server
);
String
regionValue
=
getRegionValue
(
server
);
if
(
StringUtils
.
isEmpty
(
regionValue
))
{
if
(
StringUtils
.
isEmpty
(
regionValue
))
{
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
...
@@ -110,6 +111,7 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
...
@@ -110,6 +111,7 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
return
true
;
return
true
;
}
}
Map
<
String
,
String
>
metadata
=
pluginAdapter
.
getServerMetadata
(
server
);
String
region
=
metadata
.
get
(
DiscoveryConstant
.
REGION
);
String
region
=
metadata
.
get
(
DiscoveryConstant
.
REGION
);
if
(
StringUtils
.
isEmpty
(
region
))
{
if
(
StringUtils
.
isEmpty
(
region
))
{
return
false
;
return
false
;
...
@@ -137,7 +139,7 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
...
@@ -137,7 +139,7 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
}
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
private
boolean
applyAddress
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
private
boolean
applyAddress
(
Server
server
)
{
String
addressValue
=
getAddressValue
(
server
);
String
addressValue
=
getAddressValue
(
server
);
if
(
StringUtils
.
isEmpty
(
addressValue
))
{
if
(
StringUtils
.
isEmpty
(
addressValue
))
{
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
RuleEntity
ruleEntity
=
pluginAdapter
.
getRule
();
...
@@ -168,12 +170,12 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
...
@@ -168,12 +170,12 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
return
false
;
return
false
;
}
}
private
boolean
applyStrategy
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
private
boolean
applyStrategy
(
Server
server
)
{
if
(
discoveryEnabledStrategy
==
null
)
{
if
(
discoveryEnabledStrategy
==
null
)
{
return
true
;
return
true
;
}
}
return
discoveryEnabledStrategy
.
apply
(
server
,
metadata
);
return
discoveryEnabledStrategy
.
apply
(
server
);
}
}
protected
abstract
String
getVersionValue
(
Server
server
);
protected
abstract
String
getVersionValue
(
Server
server
);
...
...
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/DiscoveryEnabledAdapter.java
View file @
cca8b7e8
...
@@ -9,10 +9,8 @@ package com.nepxion.discovery.plugin.strategy.adapter;
...
@@ -9,10 +9,8 @@ package com.nepxion.discovery.plugin.strategy.adapter;
* @version 1.0
* @version 1.0
*/
*/
import
java.util.Map
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.Server
;
public
interface
DiscoveryEnabledAdapter
{
public
interface
DiscoveryEnabledAdapter
{
boolean
apply
(
Server
server
,
Map
<
String
,
String
>
metadata
);
boolean
apply
(
Server
server
);
}
}
\ No newline at end of file
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/DiscoveryEnabledStrategy.java
View file @
cca8b7e8
...
@@ -9,10 +9,8 @@ package com.nepxion.discovery.plugin.strategy.adapter;
...
@@ -9,10 +9,8 @@ package com.nepxion.discovery.plugin.strategy.adapter;
* @version 1.0
* @version 1.0
*/
*/
import
java.util.Map
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.Server
;
public
interface
DiscoveryEnabledStrategy
{
public
interface
DiscoveryEnabledStrategy
{
boolean
apply
(
Server
server
,
Map
<
String
,
String
>
metadata
);
boolean
apply
(
Server
server
);
}
}
\ No newline at end of file
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/rule/DiscoveryEnabledBasePredicate.java
View file @
cca8b7e8
...
@@ -9,8 +9,6 @@ package com.nepxion.discovery.plugin.strategy.rule;
...
@@ -9,8 +9,6 @@ package com.nepxion.discovery.plugin.strategy.rule;
* @version 1.0
* @version 1.0
*/
*/
import
java.util.Map
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledAdapter
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledAdapter
;
import
com.netflix.loadbalancer.AbstractServerPredicate
;
import
com.netflix.loadbalancer.AbstractServerPredicate
;
...
@@ -31,9 +29,7 @@ public class DiscoveryEnabledBasePredicate extends AbstractServerPredicate {
...
@@ -31,9 +29,7 @@ public class DiscoveryEnabledBasePredicate extends AbstractServerPredicate {
return
true
;
return
true
;
}
}
Map
<
String
,
String
>
metadata
=
pluginAdapter
.
getServerMetadata
(
server
);
return
discoveryEnabledAdapter
.
apply
(
server
);
return
discoveryEnabledAdapter
.
apply
(
server
,
metadata
);
}
}
public
void
setPluginAdapter
(
PluginAdapter
pluginAdapter
)
{
public
void
setPluginAdapter
(
PluginAdapter
pluginAdapter
)
{
...
...
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/rule/DiscoveryEnabledZoneAvoidancePredicate.java
View file @
cca8b7e8
...
@@ -9,8 +9,6 @@ package com.nepxion.discovery.plugin.strategy.rule;
...
@@ -9,8 +9,6 @@ package com.nepxion.discovery.plugin.strategy.rule;
* @version 1.0
* @version 1.0
*/
*/
import
java.util.Map
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledAdapter
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledAdapter
;
import
com.netflix.client.config.IClientConfig
;
import
com.netflix.client.config.IClientConfig
;
...
@@ -47,9 +45,7 @@ public class DiscoveryEnabledZoneAvoidancePredicate extends ZoneAvoidancePredica
...
@@ -47,9 +45,7 @@ public class DiscoveryEnabledZoneAvoidancePredicate extends ZoneAvoidancePredica
return
true
;
return
true
;
}
}
Map
<
String
,
String
>
metadata
=
pluginAdapter
.
getServerMetadata
(
server
);
return
discoveryEnabledAdapter
.
apply
(
server
);
return
discoveryEnabledAdapter
.
apply
(
server
,
metadata
);
}
}
public
void
setPluginAdapter
(
PluginAdapter
pluginAdapter
)
{
public
void
setPluginAdapter
(
PluginAdapter
pluginAdapter
)
{
...
...
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/impl/MyDiscoveryEnabledStrategy.java
View file @
cca8b7e8
...
@@ -9,8 +9,6 @@ package com.nepxion.discovery.plugin.example.gateway.impl;
...
@@ -9,8 +9,6 @@ package com.nepxion.discovery.plugin.example.gateway.impl;
* @version 1.0
* @version 1.0
*/
*/
import
java.util.Map
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
...
@@ -33,18 +31,18 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
...
@@ -33,18 +31,18 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
private
PluginAdapter
pluginAdapter
;
private
PluginAdapter
pluginAdapter
;
@Override
@Override
public
boolean
apply
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
public
boolean
apply
(
Server
server
)
{
// 对Rest调用传来的Header参数(例如:mobile)做策略
// 对Rest调用传来的Header参数(例如:mobile)做策略
return
applyFromHeader
(
server
,
metadata
);
return
applyFromHeader
(
server
);
}
}
// 根据Rest调用传来的Header参数(例如:mobile),选取执行调用请求的服务实例
// 根据Rest调用传来的Header参数(例如:mobile),选取执行调用请求的服务实例
private
boolean
applyFromHeader
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
private
boolean
applyFromHeader
(
Server
server
)
{
String
mobile
=
gatewayStrategyContextHolder
.
getHeader
(
"mobile"
);
String
mobile
=
gatewayStrategyContextHolder
.
getHeader
(
"mobile"
);
String
version
=
metadata
.
get
(
DiscoveryConstant
.
VERSION
);
String
serviceId
=
pluginAdapter
.
getServerServiceId
(
server
);
String
serviceId
=
pluginAdapter
.
getServerServiceId
(
server
);
String
version
=
pluginAdapter
.
getServerMetadata
(
server
).
get
(
DiscoveryConstant
.
VERSION
);
LOG
.
info
(
"Gateway端负载均衡用户定制触发:mobile={}, serviceId={},
metadata={}"
,
mobile
,
serviceId
,
metadata
);
LOG
.
info
(
"Gateway端负载均衡用户定制触发:mobile={}, serviceId={},
version={}"
,
mobile
,
serviceId
,
version
);
if
(
StringUtils
.
isNotEmpty
(
mobile
))
{
if
(
StringUtils
.
isNotEmpty
(
mobile
))
{
// 手机号以移动138开头,路由到1.0版本的服务上
// 手机号以移动138开头,路由到1.0版本的服务上
...
...
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/impl/MyDiscoveryEnabledStrategy.java
View file @
cca8b7e8
...
@@ -34,23 +34,24 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
...
@@ -34,23 +34,24 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
private
PluginAdapter
pluginAdapter
;
private
PluginAdapter
pluginAdapter
;
@Override
@Override
public
boolean
apply
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
public
boolean
apply
(
Server
server
)
{
// 对Rest调用传来的Header参数(例如:token)做策略
// 对Rest调用传来的Header参数(例如:token)做策略
boolean
enabled
=
applyFromHeader
(
server
,
metadata
);
boolean
enabled
=
applyFromHeader
(
server
);
if
(!
enabled
)
{
if
(!
enabled
)
{
return
false
;
return
false
;
}
}
// 对RPC调用传来的方法参数做策略
// 对RPC调用传来的方法参数做策略
return
applyFromMethod
(
server
,
metadata
);
return
applyFromMethod
(
server
);
}
}
// 根据Rest调用传来的Header参数(例如:token),选取执行调用请求的服务实例
// 根据Rest调用传来的Header参数(例如:token),选取执行调用请求的服务实例
private
boolean
applyFromHeader
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
private
boolean
applyFromHeader
(
Server
server
)
{
String
token
=
serviceStrategyContextHolder
.
getHeader
(
"token"
);
String
token
=
serviceStrategyContextHolder
.
getHeader
(
"token"
);
String
serviceId
=
pluginAdapter
.
getServerServiceId
(
server
);
String
serviceId
=
pluginAdapter
.
getServerServiceId
(
server
);
String
version
=
pluginAdapter
.
getServerMetadata
(
server
).
get
(
DiscoveryConstant
.
VERSION
);
LOG
.
info
(
"Service端负载均衡用户定制触发:token={}, serviceId={},
metadata={}"
,
token
,
serviceId
,
metadata
);
LOG
.
info
(
"Service端负载均衡用户定制触发:token={}, serviceId={},
version={}"
,
token
,
serviceId
,
version
);
String
filterServiceId
=
"discovery-springcloud-example-c"
;
String
filterServiceId
=
"discovery-springcloud-example-c"
;
String
filterToken
=
"123"
;
String
filterToken
=
"123"
;
...
@@ -65,13 +66,12 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
...
@@ -65,13 +66,12 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
// 根据RPC调用传来的方法参数(例如接口名、方法名、参数名或参数值等),选取执行调用请求的服务实例
// 根据RPC调用传来的方法参数(例如接口名、方法名、参数名或参数值等),选取执行调用请求的服务实例
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
private
boolean
applyFromMethod
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
private
boolean
applyFromMethod
(
Server
server
)
{
Map
<
String
,
Object
>
attributes
=
serviceStrategyContextHolder
.
getRpcAttributes
();
Map
<
String
,
Object
>
attributes
=
serviceStrategyContextHolder
.
getRpcAttributes
();
String
serviceId
=
pluginAdapter
.
getServerServiceId
(
server
);
String
serviceId
=
pluginAdapter
.
getServerServiceId
(
server
);
String
version
=
metadata
.
get
(
DiscoveryConstant
.
VERSION
);
String
version
=
pluginAdapter
.
getServerMetadata
(
server
)
.
get
(
DiscoveryConstant
.
VERSION
);
LOG
.
info
(
"Serivice端负载均衡用户定制触发:attributes={}, serviceId={},
metadata={}"
,
attributes
,
serviceId
,
metadata
);
LOG
.
info
(
"Serivice端负载均衡用户定制触发:attributes={}, serviceId={},
version={}"
,
attributes
,
serviceId
,
version
);
String
filterServiceId
=
"discovery-springcloud-example-b"
;
String
filterServiceId
=
"discovery-springcloud-example-b"
;
String
filterVersion
=
"1.0"
;
String
filterVersion
=
"1.0"
;
...
...
discovery-springcloud-example-zuul/src/main/java/com/nepxion/discovery/plugin/example/zuul/impl/MyDiscoveryEnabledStrategy.java
View file @
cca8b7e8
...
@@ -9,8 +9,6 @@ package com.nepxion.discovery.plugin.example.zuul.impl;
...
@@ -9,8 +9,6 @@ package com.nepxion.discovery.plugin.example.zuul.impl;
* @version 1.0
* @version 1.0
*/
*/
import
java.util.Map
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
...
@@ -33,18 +31,18 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
...
@@ -33,18 +31,18 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
private
PluginAdapter
pluginAdapter
;
private
PluginAdapter
pluginAdapter
;
@Override
@Override
public
boolean
apply
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
public
boolean
apply
(
Server
server
)
{
// 对Rest调用传来的Header参数(例如:mobile)做策略
// 对Rest调用传来的Header参数(例如:mobile)做策略
return
applyFromHeader
(
server
,
metadata
);
return
applyFromHeader
(
server
);
}
}
// 根据Rest调用传来的Header参数(例如:mobile),选取执行调用请求的服务实例
// 根据Rest调用传来的Header参数(例如:mobile),选取执行调用请求的服务实例
private
boolean
applyFromHeader
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
private
boolean
applyFromHeader
(
Server
server
)
{
String
mobile
=
zuulStrategyContextHolder
.
getHeader
(
"mobile"
);
String
mobile
=
zuulStrategyContextHolder
.
getHeader
(
"mobile"
);
String
version
=
metadata
.
get
(
DiscoveryConstant
.
VERSION
);
String
serviceId
=
pluginAdapter
.
getServerServiceId
(
server
);
String
serviceId
=
pluginAdapter
.
getServerServiceId
(
server
);
String
version
=
pluginAdapter
.
getServerMetadata
(
server
).
get
(
DiscoveryConstant
.
VERSION
);
LOG
.
info
(
"Zuul端负载均衡用户定制触发:mobile={}, serviceId={},
metadata={}"
,
mobile
,
serviceId
,
metadata
);
LOG
.
info
(
"Zuul端负载均衡用户定制触发:mobile={}, serviceId={},
version={}"
,
mobile
,
serviceId
,
version
);
if
(
StringUtils
.
isNotEmpty
(
mobile
))
{
if
(
StringUtils
.
isNotEmpty
(
mobile
))
{
// 手机号以移动138开头,路由到1.0版本的服务上
// 手机号以移动138开头,路由到1.0版本的服务上
...
...
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