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
31738bd2
Commit
31738bd2
authored
Jun 24, 2018
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构策略算法
parent
1af9e1e4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
23 deletions
+66
-23
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/strategy/DiscoveryControlStrategy.java
+59
-16
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/strategy/RegisterControlStrategy.java
+7
-7
No files found.
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/strategy/DiscoveryControlStrategy.java
View file @
31738bd2
...
...
@@ -25,12 +25,14 @@ import com.nepxion.discovery.plugin.framework.cache.PluginCache;
import
com.nepxion.discovery.plugin.framework.constant.PluginConstant
;
import
com.nepxion.discovery.plugin.framework.entity.DiscoveryEntity
;
import
com.nepxion.discovery.plugin.framework.entity.DiscoveryServiceEntity
;
import
com.nepxion.discovery.plugin.framework.entity.FilterEntity
;
import
com.nepxion.discovery.plugin.framework.entity.FilterType
;
import
com.nepxion.discovery.plugin.framework.entity.RuleEntity
;
import
com.nepxion.discovery.plugin.framework.entity.VersionEntity
;
public
class
DiscoveryControlStrategy
{
@Autowired
private
RuleEntity
plugin
Entity
;
private
RuleEntity
rule
Entity
;
@Autowired
private
PluginCache
pluginCache
;
...
...
@@ -51,24 +53,53 @@ public class DiscoveryControlStrategy {
}
private
void
applyIpAddressFilter
(
String
providerServiceId
,
List
<
ServiceInstance
>
instances
)
{
String
filterIpAddress
=
pluginCache
.
get
(
providerServiceId
);
if
(
StringUtils
.
isNotEmpty
(
filterIpAddress
))
{
Iterator
<
ServiceInstance
>
iterator
=
instances
.
iterator
();
while
(
iterator
.
hasNext
())
{
ServiceInstance
serviceInstance
=
iterator
.
next
();
String
host
=
serviceInstance
.
getHost
();
boolean
valid
=
validateBlacklist
(
filterIpAddress
,
host
);
if
(
valid
)
{
iterator
.
remove
();
}
DiscoveryEntity
discoveryEntity
=
ruleEntity
.
getDiscoveryEntity
();
if
(
discoveryEntity
==
null
)
{
return
;
}
FilterEntity
filterEntity
=
discoveryEntity
.
getFilterEntity
();
if
(
filterEntity
==
null
)
{
return
;
}
FilterType
filterType
=
filterEntity
.
getFilterType
();
List
<
String
>
globalFilterValueList
=
filterEntity
.
getFilterValueList
();
Map
<
String
,
List
<
String
>>
filterMap
=
filterEntity
.
getFilterMap
();
List
<
String
>
filterValueList
=
filterMap
.
get
(
providerServiceId
);
List
<
String
>
allFilterValueList
=
new
ArrayList
<
String
>();
if
(
CollectionUtils
.
isNotEmpty
(
globalFilterValueList
))
{
allFilterValueList
.
addAll
(
globalFilterValueList
);
}
if
(
CollectionUtils
.
isNotEmpty
(
filterValueList
))
{
allFilterValueList
.
addAll
(
filterValueList
);
}
Iterator
<
ServiceInstance
>
iterator
=
instances
.
iterator
();
while
(
iterator
.
hasNext
())
{
ServiceInstance
serviceInstance
=
iterator
.
next
();
String
host
=
serviceInstance
.
getHost
();
switch
(
filterType
)
{
case
BLACKLIST:
if
(
validateBlacklist
(
allFilterValueList
,
host
))
{
iterator
.
remove
();
}
break
;
case
WHITELIST:
if
(
validateWhitelist
(
allFilterValueList
,
host
))
{
iterator
.
remove
();
}
break
;
}
}
}
private
boolean
validateBlacklist
(
String
filterIpAddress
,
String
ipAddress
)
{
String
[]
filterArray
=
StringUtils
.
split
(
ipAddress
,
PluginConstant
.
SEPARATE
);
for
(
String
filter
:
filterArray
)
{
if
(
ipAddress
.
startsWith
(
filter
))
{
private
boolean
validateBlacklist
(
List
<
String
>
allFilterValueList
,
String
ipAddress
)
{
for
(
String
filterValue
:
allFilterValueList
)
{
if
(
ipAddress
.
startsWith
(
filterValue
))
{
return
true
;
}
}
...
...
@@ -76,13 +107,25 @@ public class DiscoveryControlStrategy {
return
false
;
}
private
boolean
validateWhitelist
(
List
<
String
>
allFilterValueList
,
String
ipAddress
)
{
boolean
matched
=
true
;
for
(
String
filterValue
:
allFilterValueList
)
{
if
(
ipAddress
.
startsWith
(
filterValue
))
{
matched
=
false
;
break
;
}
}
return
matched
;
}
private
void
applyVersionFilter
(
String
consumerServiceId
,
String
consumerServiceVersion
,
String
providerServiceId
,
List
<
ServiceInstance
>
instances
)
{
// 如果消费端未配置版本号,那么它可以调用提供端所有服务,需要符合规范,极力避免该情况发生
if
(
StringUtils
.
isEmpty
(
consumerServiceVersion
))
{
return
;
}
DiscoveryEntity
discoveryEntity
=
plugin
Entity
.
getDiscoveryEntity
();
DiscoveryEntity
discoveryEntity
=
rule
Entity
.
getDiscoveryEntity
();
if
(
discoveryEntity
==
null
)
{
return
;
}
...
...
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/strategy/RegisterControlStrategy.java
View file @
31738bd2
...
...
@@ -83,8 +83,8 @@ public class RegisterControlStrategy {
private
void
validateBlacklist
(
List
<
String
>
allFilterValueList
,
String
ipAddress
)
{
LOG
.
info
(
"********** IP address blacklist={}, current ip address={} **********"
,
allFilterValueList
,
ipAddress
);
for
(
String
filter
:
allFilterValueList
)
{
if
(
ipAddress
.
startsWith
(
filter
))
{
for
(
String
filter
Value
:
allFilterValueList
)
{
if
(
ipAddress
.
startsWith
(
filter
Value
))
{
throw
new
PluginException
(
ipAddress
+
" isn't allowed to register to Eureka server, because it is in blacklist"
);
}
}
...
...
@@ -93,15 +93,15 @@ public class RegisterControlStrategy {
private
void
validateWhitelist
(
List
<
String
>
allFilterValueList
,
String
ipAddress
)
{
LOG
.
info
(
"********** IP address whitelist={}, current ip address={} **********"
,
allFilterValueList
,
ipAddress
);
boolean
valid
=
fals
e
;
for
(
String
filter
:
allFilterValueList
)
{
if
(
ipAddress
.
startsWith
(
filter
))
{
valid
=
tru
e
;
boolean
matched
=
tru
e
;
for
(
String
filter
Value
:
allFilterValueList
)
{
if
(
ipAddress
.
startsWith
(
filter
Value
))
{
matched
=
fals
e
;
break
;
}
}
if
(
!
vali
d
)
{
if
(
matche
d
)
{
throw
new
PluginException
(
ipAddress
+
" isn't allowed to register to Eureka server, because it isn't in whitelist"
);
}
}
...
...
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