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
93beba08
Commit
93beba08
authored
Jun 30, 2018
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加注册失败后事件发布/订阅功能
parent
dd569587
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
138 additions
and
11 deletions
+138
-11
discovery-plugin-framework-consul/src/main/java/com/nepxion/discovery/plugin/framework/adapter/ConsulAdapter.java
+11
-0
discovery-plugin-framework-eureka/src/main/java/com/nepxion/discovery/plugin/framework/adapter/EurekaAdapter.java
+11
-0
discovery-plugin-framework-zookeeper/src/main/java/com/nepxion/discovery/plugin/framework/adapter/ZookeeperAdapter.java
+11
-0
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/adapter/PluginAdapter.java
+3
-0
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/event/RegisterFaiureEvent.java
+47
-0
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/listener/impl/IpAddressFilterRegisterListener.java
+20
-8
discovery-springcloud-example/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryApplicationA1.java
+7
-0
discovery-springcloud-example/src/main/java/com/nepxion/discovery/plugin/example/extension/MyDiscoveryListener.java
+1
-1
discovery-springcloud-example/src/main/java/com/nepxion/discovery/plugin/example/extension/MyRegisterListener.java
+1
-2
discovery-springcloud-example/src/main/java/com/nepxion/discovery/plugin/example/extension/MySubscriber.java
+23
-0
discovery-springcloud-example/src/main/resources/bootstrap.properties
+3
-0
No files found.
discovery-plugin-framework-consul/src/main/java/com/nepxion/discovery/plugin/framework/adapter/ConsulAdapter.java
View file @
93beba08
...
...
@@ -65,6 +65,17 @@ public class ConsulAdapter implements PluginAdapter {
}
@Override
public
int
getPort
(
Registration
registration
)
{
if
(
registration
instanceof
ConsulRegistration
)
{
ConsulRegistration
consulRegistration
=
(
ConsulRegistration
)
registration
;
return
consulRegistration
.
getService
().
getPort
();
}
throw
new
PluginException
(
"Registration instance isn't the type of Consul"
);
}
@Override
public
String
getVersion
()
{
return
version
;
}
...
...
discovery-plugin-framework-eureka/src/main/java/com/nepxion/discovery/plugin/framework/adapter/EurekaAdapter.java
View file @
93beba08
...
...
@@ -33,6 +33,17 @@ public class EurekaAdapter implements PluginAdapter {
}
@Override
public
int
getPort
(
Registration
registration
)
{
if
(
registration
instanceof
EurekaRegistration
)
{
EurekaRegistration
eurekaRegistration
=
(
EurekaRegistration
)
registration
;
return
eurekaRegistration
.
getInstanceConfig
().
getNonSecurePort
();
}
throw
new
PluginException
(
"Registration instance isn't the type of Eureka"
);
}
@Override
public
String
getVersion
()
{
return
environment
.
getProperty
(
EurekaConstant
.
METADATA_VERSION
);
}
...
...
discovery-plugin-framework-zookeeper/src/main/java/com/nepxion/discovery/plugin/framework/adapter/ZookeeperAdapter.java
View file @
93beba08
...
...
@@ -33,6 +33,17 @@ public class ZookeeperAdapter implements PluginAdapter {
}
@Override
public
int
getPort
(
Registration
registration
)
{
if
(
registration
instanceof
ZookeeperRegistration
)
{
ZookeeperRegistration
zookeeperRegistration
=
(
ZookeeperRegistration
)
registration
;
return
zookeeperRegistration
.
getServiceInstance
().
getPort
();
}
throw
new
PluginException
(
"Registration instance isn't the type of Zookeeper"
);
}
@Override
public
String
getVersion
()
{
return
environment
.
getProperty
(
ZookeeperConstant
.
METADATA_VERSION
);
}
...
...
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/adapter/PluginAdapter.java
View file @
93beba08
...
...
@@ -14,5 +14,7 @@ import org.springframework.cloud.client.serviceregistry.Registration;
public
interface
PluginAdapter
{
String
getIpAddress
(
Registration
registration
);
int
getPort
(
Registration
registration
);
String
getVersion
();
}
\ No newline at end of file
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/event/RegisterFaiureEvent.java
0 → 100644
View file @
93beba08
package
com
.
nepxion
.
discovery
.
plugin
.
framework
.
event
;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import
java.io.Serializable
;
import
com.nepxion.discovery.plugin.framework.entity.FilterType
;
public
class
RegisterFaiureEvent
implements
Serializable
{
private
static
final
long
serialVersionUID
=
-
1343084923958294246L
;
private
FilterType
filterType
;
private
String
serviceId
;
private
String
ipAddress
;
private
int
port
;
public
RegisterFaiureEvent
(
FilterType
filterType
,
String
serviceId
,
String
ipAddress
,
int
port
)
{
this
.
filterType
=
filterType
;
this
.
serviceId
=
serviceId
;
this
.
ipAddress
=
ipAddress
;
this
.
port
=
port
;
}
public
String
getIpAddress
()
{
return
ipAddress
;
}
public
String
getServiceId
()
{
return
serviceId
;
}
public
int
getPort
()
{
return
port
;
}
public
FilterType
getFilterType
()
{
return
filterType
;
}
}
\ No newline at end of file
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/listener/impl/IpAddressFilterRegisterListener.java
View file @
93beba08
...
...
@@ -24,6 +24,8 @@ import com.nepxion.discovery.plugin.framework.entity.FilterEntity;
import
com.nepxion.discovery.plugin.framework.entity.FilterType
;
import
com.nepxion.discovery.plugin.framework.entity.RegisterEntity
;
import
com.nepxion.discovery.plugin.framework.entity.RuleEntity
;
import
com.nepxion.discovery.plugin.framework.event.PluginPublisher
;
import
com.nepxion.discovery.plugin.framework.event.RegisterFaiureEvent
;
import
com.nepxion.discovery.plugin.framework.exception.PluginException
;
import
com.nepxion.discovery.plugin.framework.listener.AbstractRegisterListener
;
...
...
@@ -36,15 +38,19 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener {
@Autowired
private
PluginAdapter
pluginAdapter
;
@Autowired
private
PluginPublisher
pluginPublisher
;
@Override
public
void
onRegister
(
Registration
registration
)
{
String
serviceId
=
registration
.
getServiceId
();
String
ipAddress
=
pluginAdapter
.
getIpAddress
(
registration
);
int
port
=
pluginAdapter
.
getPort
(
registration
);
applyIpAddressFilter
(
serviceId
,
ipAddress
);
applyIpAddressFilter
(
serviceId
,
ipAddress
,
port
);
}
private
void
applyIpAddressFilter
(
String
serviceId
,
String
ipAddress
)
{
private
void
applyIpAddressFilter
(
String
serviceId
,
String
ipAddress
,
int
port
)
{
RegisterEntity
registerEntity
=
ruleEntity
.
getRegisterEntity
();
if
(
registerEntity
==
null
)
{
return
;
...
...
@@ -72,25 +78,25 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener {
switch
(
filterType
)
{
case
BLACKLIST:
validateBlacklist
(
allFilterValueList
,
ipAddress
);
validateBlacklist
(
allFilterValueList
,
serviceId
,
ipAddress
,
port
);
break
;
case
WHITELIST:
validateWhitelist
(
allFilterValueList
,
ipAddress
);
validateWhitelist
(
allFilterValueList
,
serviceId
,
ipAddress
,
port
);
break
;
}
}
private
void
validateBlacklist
(
List
<
String
>
allFilterValueList
,
String
ipAddress
)
{
private
void
validateBlacklist
(
List
<
String
>
allFilterValueList
,
String
serviceId
,
String
ipAddress
,
int
port
)
{
LOG
.
info
(
"********** IP address blacklist={}, current ip address={} **********"
,
allFilterValueList
,
ipAddress
);
for
(
String
filterValue
:
allFilterValueList
)
{
if
(
ipAddress
.
startsWith
(
filterValue
))
{
throw
new
PluginException
(
ipAddress
+
" isn't allowed to register to Discovery server, because it is in blacklist"
);
onRegisterFaiure
(
FilterType
.
BLACKLIST
,
serviceId
,
ipAddress
,
port
);
}
}
}
private
void
validateWhitelist
(
List
<
String
>
allFilterValueList
,
String
ipAddress
)
{
private
void
validateWhitelist
(
List
<
String
>
allFilterValueList
,
String
serviceId
,
String
ipAddress
,
int
port
)
{
LOG
.
info
(
"********** IP address whitelist={}, current ip address={} **********"
,
allFilterValueList
,
ipAddress
);
boolean
matched
=
true
;
...
...
@@ -102,8 +108,14 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener {
}
if
(
matched
)
{
throw
new
PluginException
(
ipAddress
+
" isn't allowed to register to Discovery server, because it isn't in whitelist"
);
onRegisterFaiure
(
FilterType
.
WHITELIST
,
serviceId
,
ipAddress
,
port
);
}
}
private
void
onRegisterFaiure
(
FilterType
filterType
,
String
serviceId
,
String
ipAddress
,
int
port
)
{
pluginPublisher
.
asyncPublish
(
new
RegisterFaiureEvent
(
filterType
,
serviceId
,
ipAddress
,
port
));
throw
new
PluginException
(
ipAddress
+
" isn't allowed to register to Discovery server, because it is in blacklist"
);
}
@Override
...
...
discovery-springcloud-example/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryApplicationA1.java
View file @
93beba08
...
...
@@ -16,6 +16,7 @@ import org.springframework.context.annotation.Bean;
import
com.nepxion.discovery.plugin.example.extension.MyDiscoveryListener
;
import
com.nepxion.discovery.plugin.example.extension.MyRegisterListener
;
import
com.nepxion.discovery.plugin.example.extension.MySubscriber
;
import
com.nepxion.discovery.plugin.example.impl.DiscoveryConfigAdapter
;
@SpringBootApplication
...
...
@@ -41,4 +42,9 @@ public class DiscoveryApplicationA1 {
public
MyDiscoveryListener
myDiscoveryListener
()
{
return
new
MyDiscoveryListener
();
}
@Bean
public
MySubscriber
mySubscriber
()
{
return
new
MySubscriber
();
}
}
\ No newline at end of file
discovery-springcloud-example/src/main/java/com/nepxion/discovery/plugin/example/extension/MyDiscoveryListener.java
View file @
93beba08
...
...
@@ -18,7 +18,7 @@ import com.nepxion.discovery.plugin.framework.listener.AbstractDiscoveryListener
public
class
MyDiscoveryListener
extends
AbstractDiscoveryListener
{
@Override
public
void
onGetInstances
(
String
serviceId
,
List
<
ServiceInstance
>
instances
)
{
System
.
out
.
println
(
"========== getInstances() 被触发:serviceId="
+
serviceId
+
" instances="
+
instances
+
" =========="
);
System
.
out
.
println
(
"========== getInstances() 被触发:serviceId="
+
serviceId
+
"
,
instances="
+
instances
+
" =========="
);
}
@Override
...
...
discovery-springcloud-example/src/main/java/com/nepxion/discovery/plugin/example/extension/MyRegisterListener.java
View file @
93beba08
...
...
@@ -14,7 +14,6 @@ import org.springframework.cloud.client.serviceregistry.Registration;
import
com.nepxion.discovery.plugin.framework.listener.AbstractRegisterListener
;
public
class
MyRegisterListener
extends
AbstractRegisterListener
{
@Override
public
void
onRegister
(
Registration
registration
)
{
System
.
out
.
println
(
"========== register() 被触发:serviceId="
+
registration
.
getServiceId
());
...
...
@@ -27,7 +26,7 @@ public class MyRegisterListener extends AbstractRegisterListener {
@Override
public
void
onSetStatus
(
Registration
registration
,
String
status
)
{
System
.
out
.
println
(
"========== setStatus() 被触发:serviceId="
+
registration
.
getServiceId
()
+
" status="
+
status
);
System
.
out
.
println
(
"========== setStatus() 被触发:serviceId="
+
registration
.
getServiceId
()
+
"
,
status="
+
status
);
}
@Override
...
...
discovery-springcloud-example/src/main/java/com/nepxion/discovery/plugin/example/extension/MySubscriber.java
0 → 100644
View file @
93beba08
package
com
.
nepxion
.
discovery
.
plugin
.
example
.
extension
;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import
com.google.common.eventbus.Subscribe
;
import
com.nepxion.discovery.plugin.framework.event.RegisterFaiureEvent
;
import
com.nepxion.eventbus.annotation.EventBus
;
@EventBus
public
class
MySubscriber
{
@Subscribe
public
void
subscribeRegisterFaiure
(
RegisterFaiureEvent
registerFaiureEvent
)
{
System
.
out
.
println
(
"========== 注册失败:filterType="
+
registerFaiureEvent
.
getFilterType
()
+
", serviceId="
+
registerFaiureEvent
.
getServiceId
()
+
", ipAddress="
+
registerFaiureEvent
.
getIpAddress
()
+
", port="
+
registerFaiureEvent
.
getPort
());
}
}
\ No newline at end of file
discovery-springcloud-example/src/main/resources/bootstrap.properties
View file @
93beba08
...
...
@@ -19,6 +19,9 @@ management.health.consul.enabled=false
# Plugin config
# 开启和关闭服务注册层面的控制。一旦关闭,服务注册的黑/白名单过滤功能将失效。缺失则默认为true
spring.application.register.control.enabled
=
true
# 开启和关闭禁止注册后发送异步事件通知。一旦关闭,禁止注册后,不会发送异步事件通知。缺失则默认为true
spring.application.register.failure.event.enabled
=
true
# 开启和关闭服务发现层面的控制。一旦关闭,服务多版本调用的控制功能将失效,动态屏蔽指定IP地址的服务实例被发现的功能将失效。缺失则默认为true
spring.application.discovery.control.enabled
=
true
# 开启和关闭远程配置中心规则文件读取。一旦关闭,默认读取本地规则文件(例如:rule.xml)。缺失则默认为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