Commit 93beba08 by Nepxion

增加注册失败后事件发布/订阅功能

parent dd569587
...@@ -65,6 +65,17 @@ public class ConsulAdapter implements PluginAdapter { ...@@ -65,6 +65,17 @@ public class ConsulAdapter implements PluginAdapter {
} }
@Override @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() { public String getVersion() {
return version; return version;
} }
......
...@@ -33,6 +33,17 @@ public class EurekaAdapter implements PluginAdapter { ...@@ -33,6 +33,17 @@ public class EurekaAdapter implements PluginAdapter {
} }
@Override @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() { public String getVersion() {
return environment.getProperty(EurekaConstant.METADATA_VERSION); return environment.getProperty(EurekaConstant.METADATA_VERSION);
} }
......
...@@ -33,6 +33,17 @@ public class ZookeeperAdapter implements PluginAdapter { ...@@ -33,6 +33,17 @@ public class ZookeeperAdapter implements PluginAdapter {
} }
@Override @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() { public String getVersion() {
return environment.getProperty(ZookeeperConstant.METADATA_VERSION); return environment.getProperty(ZookeeperConstant.METADATA_VERSION);
} }
......
...@@ -14,5 +14,7 @@ import org.springframework.cloud.client.serviceregistry.Registration; ...@@ -14,5 +14,7 @@ import org.springframework.cloud.client.serviceregistry.Registration;
public interface PluginAdapter { public interface PluginAdapter {
String getIpAddress(Registration registration); String getIpAddress(Registration registration);
int getPort(Registration registration);
String getVersion(); String getVersion();
} }
\ No newline at end of file
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
...@@ -24,6 +24,8 @@ import com.nepxion.discovery.plugin.framework.entity.FilterEntity; ...@@ -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.FilterType;
import com.nepxion.discovery.plugin.framework.entity.RegisterEntity; import com.nepxion.discovery.plugin.framework.entity.RegisterEntity;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity; 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.exception.PluginException;
import com.nepxion.discovery.plugin.framework.listener.AbstractRegisterListener; import com.nepxion.discovery.plugin.framework.listener.AbstractRegisterListener;
...@@ -36,15 +38,19 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener { ...@@ -36,15 +38,19 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener {
@Autowired @Autowired
private PluginAdapter pluginAdapter; private PluginAdapter pluginAdapter;
@Autowired
private PluginPublisher pluginPublisher;
@Override @Override
public void onRegister(Registration registration) { public void onRegister(Registration registration) {
String serviceId = registration.getServiceId(); String serviceId = registration.getServiceId();
String ipAddress = pluginAdapter.getIpAddress(registration); 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(); RegisterEntity registerEntity = ruleEntity.getRegisterEntity();
if (registerEntity == null) { if (registerEntity == null) {
return; return;
...@@ -72,25 +78,25 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener { ...@@ -72,25 +78,25 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener {
switch (filterType) { switch (filterType) {
case BLACKLIST: case BLACKLIST:
validateBlacklist(allFilterValueList, ipAddress); validateBlacklist(allFilterValueList, serviceId, ipAddress, port);
break; break;
case WHITELIST: case WHITELIST:
validateWhitelist(allFilterValueList, ipAddress); validateWhitelist(allFilterValueList, serviceId, ipAddress, port);
break; 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); LOG.info("********** IP address blacklist={}, current ip address={} **********", allFilterValueList, ipAddress);
for (String filterValue : allFilterValueList) { for (String filterValue : allFilterValueList) {
if (ipAddress.startsWith(filterValue)) { 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); LOG.info("********** IP address whitelist={}, current ip address={} **********", allFilterValueList, ipAddress);
boolean matched = true; boolean matched = true;
...@@ -102,10 +108,16 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener { ...@@ -102,10 +108,16 @@ public class IpAddressFilterRegisterListener extends AbstractRegisterListener {
} }
if (matched) { 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 @Override
public void onDeregister(Registration registration) { public void onDeregister(Registration registration) {
......
...@@ -16,6 +16,7 @@ import org.springframework.context.annotation.Bean; ...@@ -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.MyDiscoveryListener;
import com.nepxion.discovery.plugin.example.extension.MyRegisterListener; import com.nepxion.discovery.plugin.example.extension.MyRegisterListener;
import com.nepxion.discovery.plugin.example.extension.MySubscriber;
import com.nepxion.discovery.plugin.example.impl.DiscoveryConfigAdapter; import com.nepxion.discovery.plugin.example.impl.DiscoveryConfigAdapter;
@SpringBootApplication @SpringBootApplication
...@@ -41,4 +42,9 @@ public class DiscoveryApplicationA1 { ...@@ -41,4 +42,9 @@ public class DiscoveryApplicationA1 {
public MyDiscoveryListener myDiscoveryListener() { public MyDiscoveryListener myDiscoveryListener() {
return new MyDiscoveryListener(); return new MyDiscoveryListener();
} }
@Bean
public MySubscriber mySubscriber() {
return new MySubscriber();
}
} }
\ No newline at end of file
...@@ -18,7 +18,7 @@ import com.nepxion.discovery.plugin.framework.listener.AbstractDiscoveryListener ...@@ -18,7 +18,7 @@ import com.nepxion.discovery.plugin.framework.listener.AbstractDiscoveryListener
public class MyDiscoveryListener extends AbstractDiscoveryListener { public class MyDiscoveryListener extends AbstractDiscoveryListener {
@Override @Override
public void onGetInstances(String serviceId, List<ServiceInstance> instances) { 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 @Override
......
...@@ -14,7 +14,6 @@ import org.springframework.cloud.client.serviceregistry.Registration; ...@@ -14,7 +14,6 @@ import org.springframework.cloud.client.serviceregistry.Registration;
import com.nepxion.discovery.plugin.framework.listener.AbstractRegisterListener; import com.nepxion.discovery.plugin.framework.listener.AbstractRegisterListener;
public class MyRegisterListener extends AbstractRegisterListener { public class MyRegisterListener extends AbstractRegisterListener {
@Override @Override
public void onRegister(Registration registration) { public void onRegister(Registration registration) {
System.out.println("========== register() 被触发:serviceId=" + registration.getServiceId()); System.out.println("========== register() 被触发:serviceId=" + registration.getServiceId());
...@@ -27,7 +26,7 @@ public class MyRegisterListener extends AbstractRegisterListener { ...@@ -27,7 +26,7 @@ public class MyRegisterListener extends AbstractRegisterListener {
@Override @Override
public void onSetStatus(Registration registration, String status) { 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 @Override
......
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
...@@ -19,6 +19,9 @@ management.health.consul.enabled=false ...@@ -19,6 +19,9 @@ management.health.consul.enabled=false
# Plugin config # Plugin config
# 开启和关闭服务注册层面的控制。一旦关闭,服务注册的黑/白名单过滤功能将失效。缺失则默认为true # 开启和关闭服务注册层面的控制。一旦关闭,服务注册的黑/白名单过滤功能将失效。缺失则默认为true
spring.application.register.control.enabled=true spring.application.register.control.enabled=true
# 开启和关闭禁止注册后发送异步事件通知。一旦关闭,禁止注册后,不会发送异步事件通知。缺失则默认为true
spring.application.register.failure.event.enabled=true
# 开启和关闭服务发现层面的控制。一旦关闭,服务多版本调用的控制功能将失效,动态屏蔽指定IP地址的服务实例被发现的功能将失效。缺失则默认为true # 开启和关闭服务发现层面的控制。一旦关闭,服务多版本调用的控制功能将失效,动态屏蔽指定IP地址的服务实例被发现的功能将失效。缺失则默认为true
spring.application.discovery.control.enabled=true spring.application.discovery.control.enabled=true
# 开启和关闭远程配置中心规则文件读取。一旦关闭,默认读取本地规则文件(例如:rule.xml)。缺失则默认为true # 开启和关闭远程配置中心规则文件读取。一旦关闭,默认读取本地规则文件(例如:rule.xml)。缺失则默认为true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment