Commit 4c9cd5a5 by Nepxion

封装事件总线

parent a947a694
...@@ -37,7 +37,7 @@ import com.nepxion.discovery.plugin.framework.cache.RuleCache; ...@@ -37,7 +37,7 @@ import com.nepxion.discovery.plugin.framework.cache.RuleCache;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant; import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware; import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
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.PluginEventWapper;
import com.nepxion.discovery.plugin.framework.event.RuleChangedEvent; import com.nepxion.discovery.plugin.framework.event.RuleChangedEvent;
@RestController @RestController
...@@ -51,7 +51,7 @@ public class ConfigEndpoint implements MvcEndpoint { ...@@ -51,7 +51,7 @@ public class ConfigEndpoint implements MvcEndpoint {
private PluginContextAware pluginContextAware; private PluginContextAware pluginContextAware;
@Autowired @Autowired
private PluginPublisher pluginPublisher; private PluginEventWapper pluginEventWapper;
@Autowired @Autowired
private RuleCache ruleCache; private RuleCache ruleCache;
...@@ -68,7 +68,7 @@ public class ConfigEndpoint implements MvcEndpoint { ...@@ -68,7 +68,7 @@ public class ConfigEndpoint implements MvcEndpoint {
try { try {
InputStream inputStream = IOUtils.toInputStream(config, PluginConstant.ENCODING_UTF_8); InputStream inputStream = IOUtils.toInputStream(config, PluginConstant.ENCODING_UTF_8);
pluginPublisher.asyncPublish(new RuleChangedEvent(inputStream)); pluginEventWapper.fireRuleChanged(new RuleChangedEvent(inputStream), true);
} catch (IOException e) { } catch (IOException e) {
LOG.error("Publish config failed", e); LOG.error("Publish config failed", e);
......
...@@ -33,7 +33,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -33,7 +33,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter; import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware; import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher; import com.nepxion.discovery.plugin.framework.event.PluginEventWapper;
import com.nepxion.discovery.plugin.framework.event.VersionChangedEvent; import com.nepxion.discovery.plugin.framework.event.VersionChangedEvent;
@RestController @RestController
...@@ -47,7 +47,7 @@ public class VersionEndpoint implements MvcEndpoint { ...@@ -47,7 +47,7 @@ public class VersionEndpoint implements MvcEndpoint {
private PluginAdapter pluginAdapter; private PluginAdapter pluginAdapter;
@Autowired @Autowired
private PluginPublisher pluginPublisher; private PluginEventWapper pluginEventWapper;
@RequestMapping(path = "/version/send", method = RequestMethod.POST) @RequestMapping(path = "/version/send", method = RequestMethod.POST)
@ApiOperation(value = "设置服务的动态版本", notes = "", response = ResponseEntity.class, httpMethod = "POST") @ApiOperation(value = "设置服务的动态版本", notes = "", response = ResponseEntity.class, httpMethod = "POST")
...@@ -61,7 +61,7 @@ public class VersionEndpoint implements MvcEndpoint { ...@@ -61,7 +61,7 @@ public class VersionEndpoint implements MvcEndpoint {
pluginAdapter.setDynamicVersion(version); pluginAdapter.setDynamicVersion(version);
pluginPublisher.asyncPublish(new VersionChangedEvent()); pluginEventWapper.fireVersionChanged(new VersionChangedEvent(), true);
return ResponseEntity.ok().body("OK"); return ResponseEntity.ok().body("OK");
} }
...@@ -78,7 +78,7 @@ public class VersionEndpoint implements MvcEndpoint { ...@@ -78,7 +78,7 @@ public class VersionEndpoint implements MvcEndpoint {
pluginAdapter.clearDynamicVersion(); pluginAdapter.clearDynamicVersion();
pluginPublisher.asyncPublish(new VersionChangedEvent()); pluginEventWapper.fireVersionChanged(new VersionChangedEvent(), true);
return ResponseEntity.ok().body("OK"); return ResponseEntity.ok().body("OK");
} }
......
...@@ -12,14 +12,14 @@ package com.nepxion.discovery.plugin.configcenter; ...@@ -12,14 +12,14 @@ package com.nepxion.discovery.plugin.configcenter;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.plugin.configcenter.loader.AbstractConfigLoader; import com.nepxion.discovery.plugin.configcenter.loader.AbstractConfigLoader;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher; import com.nepxion.discovery.plugin.framework.event.PluginEventWapper;
import com.nepxion.discovery.plugin.framework.event.RuleChangedEvent; import com.nepxion.discovery.plugin.framework.event.RuleChangedEvent;
public abstract class ConfigAdapter extends AbstractConfigLoader { public abstract class ConfigAdapter extends AbstractConfigLoader {
@Autowired @Autowired
private PluginPublisher pluginPublisher; private PluginEventWapper pluginEventWapper;
public void publish(RuleChangedEvent ruleChangedEvent) { public void fireRuleChanged(RuleChangedEvent ruleChangedEvent, boolean async) {
pluginPublisher.asyncPublish(ruleChangedEvent); pluginEventWapper.fireRuleChanged(ruleChangedEvent, async);
} }
} }
\ No newline at end of file
...@@ -16,6 +16,7 @@ import com.nepxion.discovery.plugin.framework.cache.PluginCache; ...@@ -16,6 +16,7 @@ import com.nepxion.discovery.plugin.framework.cache.PluginCache;
import com.nepxion.discovery.plugin.framework.cache.RuleCache; import com.nepxion.discovery.plugin.framework.cache.RuleCache;
import com.nepxion.discovery.plugin.framework.context.PluginContainerInitializedHandler; import com.nepxion.discovery.plugin.framework.context.PluginContainerInitializedHandler;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware; import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.event.PluginEventWapper;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher; import com.nepxion.discovery.plugin.framework.event.PluginPublisher;
import com.nepxion.discovery.plugin.framework.event.PluginSubscriber; import com.nepxion.discovery.plugin.framework.event.PluginSubscriber;
import com.nepxion.discovery.plugin.framework.listener.discovery.DiscoveryListenerExecutor; import com.nepxion.discovery.plugin.framework.listener.discovery.DiscoveryListenerExecutor;
...@@ -51,6 +52,11 @@ public class PluginAutoConfiguration { ...@@ -51,6 +52,11 @@ public class PluginAutoConfiguration {
} }
@Bean @Bean
public PluginEventWapper pluginEventWapper() {
return new PluginEventWapper();
}
@Bean
public PluginCache pluginCache() { public PluginCache pluginCache() {
return new PluginCache(); return new PluginCache();
} }
......
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 org.springframework.beans.factory.annotation.Autowired;
public class PluginEventWapper {
@Autowired
private PluginPublisher pluginPublisher;
@Autowired
private PluginSubscriber pluginSubscriber;
public void fireRuleChanged(RuleChangedEvent ruleChangedEvent, boolean async) {
if (async) {
pluginPublisher.asyncPublish(ruleChangedEvent);
} else {
pluginSubscriber.onRuleChanged(ruleChangedEvent);
}
}
public void fireVersionChanged(VersionChangedEvent versionChangedEvent, boolean async) {
if (async) {
pluginPublisher.asyncPublish(versionChangedEvent);
} else {
pluginSubscriber.onVersionChanged(versionChangedEvent);
}
}
}
\ No newline at end of file
...@@ -36,7 +36,7 @@ public class PluginSubscriber { ...@@ -36,7 +36,7 @@ public class PluginSubscriber {
private LoadBalanceListenerExecutor loadBalanceListenerExecutor; private LoadBalanceListenerExecutor loadBalanceListenerExecutor;
@Subscribe @Subscribe
public void subscribeRuleChanged(RuleChangedEvent ruleChangedEvent) { public void onRuleChanged(RuleChangedEvent ruleChangedEvent) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled(); Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
Boolean remoteConfigEnabled = pluginContextAware.isRemoteConfigEnabled(); Boolean remoteConfigEnabled = pluginContextAware.isRemoteConfigEnabled();
...@@ -57,11 +57,11 @@ public class PluginSubscriber { ...@@ -57,11 +57,11 @@ public class PluginSubscriber {
InputStream inputStream = ruleChangedEvent.getInputStream(); InputStream inputStream = ruleChangedEvent.getInputStream();
pluninConfigParser.parse(inputStream); pluninConfigParser.parse(inputStream);
subscribeVersionChanged(null); onVersionChanged(null);
} }
@Subscribe @Subscribe
public void subscribeVersionChanged(VersionChangedEvent versionChangedEvent) { public void onVersionChanged(VersionChangedEvent versionChangedEvent) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled(); Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!discoveryControlEnabled) { if (!discoveryControlEnabled) {
LOG.info("********** Discovery control is disabled, ignore to subscribe **********"); LOG.info("********** Discovery control is disabled, ignore to subscribe **********");
......
...@@ -48,7 +48,7 @@ public class DiscoveryConfigAdapter extends ConfigAdapter { ...@@ -48,7 +48,7 @@ public class DiscoveryConfigAdapter extends ConfigAdapter {
public void publish() { public void publish() {
try { try {
InputStream inputStream = FileUtils.openInputStream(new File("src/main/resources/rule.xml")); InputStream inputStream = FileUtils.openInputStream(new File("src/main/resources/rule.xml"));
publish(new RuleChangedEvent(inputStream)); fireRuleChanged(new RuleChangedEvent(inputStream), true);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -16,7 +16,7 @@ import com.nepxion.eventbus.annotation.EventBus; ...@@ -16,7 +16,7 @@ import com.nepxion.eventbus.annotation.EventBus;
@EventBus @EventBus
public class MySubscriber { public class MySubscriber {
@Subscribe @Subscribe
public void subscribeRegisterFailure(RegisterFailureEvent registerFailureEvent) { public void onRegisterFailure(RegisterFailureEvent registerFailureEvent) {
System.out.println("========== 注册失败:eventType=" + registerFailureEvent.getEventType() + ", eventDescription=" + registerFailureEvent.getEventDescription() + ", serviceId=" + registerFailureEvent.getServiceId() + ", ipAddress=" + registerFailureEvent.getIpAddress() + ", port=" + registerFailureEvent.getPort()); System.out.println("========== 注册失败:eventType=" + registerFailureEvent.getEventType() + ", eventDescription=" + registerFailureEvent.getEventDescription() + ", serviceId=" + registerFailureEvent.getServiceId() + ", ipAddress=" + registerFailureEvent.getIpAddress() + ", port=" + registerFailureEvent.getPort());
} }
} }
\ No newline at end of file
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