Commit 201477bb by Nepxion

优化版本设置策略

parent 4c9cd5a5
...@@ -59,9 +59,7 @@ public class VersionEndpoint implements MvcEndpoint { ...@@ -59,9 +59,7 @@ public class VersionEndpoint implements MvcEndpoint {
return new ResponseEntity<>(Collections.singletonMap("Message", "Discovery control is disabled"), HttpStatus.NOT_FOUND); return new ResponseEntity<>(Collections.singletonMap("Message", "Discovery control is disabled"), HttpStatus.NOT_FOUND);
} }
pluginAdapter.setDynamicVersion(version); pluginEventWapper.fireVersionChanged(new VersionChangedEvent(VersionChangedEvent.EventType.VERSION_UPDATE, version), true);
pluginEventWapper.fireVersionChanged(new VersionChangedEvent(), true);
return ResponseEntity.ok().body("OK"); return ResponseEntity.ok().body("OK");
} }
...@@ -76,9 +74,7 @@ public class VersionEndpoint implements MvcEndpoint { ...@@ -76,9 +74,7 @@ public class VersionEndpoint implements MvcEndpoint {
return new ResponseEntity<>(Collections.singletonMap("Message", "Discovery control is disabled"), HttpStatus.NOT_FOUND); return new ResponseEntity<>(Collections.singletonMap("Message", "Discovery control is disabled"), HttpStatus.NOT_FOUND);
} }
pluginAdapter.clearDynamicVersion(); pluginEventWapper.fireVersionChanged(new VersionChangedEvent(VersionChangedEvent.EventType.VERSION_CLEAR), true);
pluginEventWapper.fireVersionChanged(new VersionChangedEvent(), true);
return ResponseEntity.ok().body("OK"); return ResponseEntity.ok().body("OK");
} }
......
...@@ -16,8 +16,10 @@ import org.slf4j.LoggerFactory; ...@@ -16,8 +16,10 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.framework.config.PluginConfigParser; import com.nepxion.discovery.plugin.framework.config.PluginConfigParser;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware; import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
import com.nepxion.discovery.plugin.framework.listener.loadbalance.LoadBalanceListenerExecutor; import com.nepxion.discovery.plugin.framework.listener.loadbalance.LoadBalanceListenerExecutor;
import com.nepxion.eventbus.annotation.EventBus; import com.nepxion.eventbus.annotation.EventBus;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer; import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
...@@ -30,6 +32,9 @@ public class PluginSubscriber { ...@@ -30,6 +32,9 @@ public class PluginSubscriber {
private PluginContextAware pluginContextAware; private PluginContextAware pluginContextAware;
@Autowired @Autowired
private PluginAdapter pluginAdapter;
@Autowired
private PluginConfigParser pluninConfigParser; private PluginConfigParser pluninConfigParser;
@Autowired @Autowired
...@@ -54,10 +59,14 @@ public class PluginSubscriber { ...@@ -54,10 +59,14 @@ public class PluginSubscriber {
LOG.info("********** Remote config change has been subscribed **********"); LOG.info("********** Remote config change has been subscribed **********");
if (ruleChangedEvent == null) {
throw new PluginException("RuleChangedEvent can't be null");
}
InputStream inputStream = ruleChangedEvent.getInputStream(); InputStream inputStream = ruleChangedEvent.getInputStream();
pluninConfigParser.parse(inputStream); pluninConfigParser.parse(inputStream);
onVersionChanged(null); refreshLoadBalancer();
} }
@Subscribe @Subscribe
...@@ -69,14 +78,39 @@ public class PluginSubscriber { ...@@ -69,14 +78,39 @@ public class PluginSubscriber {
return; return;
} }
LOG.info("********** Version change has been subscribed **********");
if (versionChangedEvent == null) {
throw new PluginException("VersionChangedEvent can't be null");
}
VersionChangedEvent.EventType eventType = versionChangedEvent.getEventType();
switch (eventType) {
case VERSION_UPDATE:
String version = versionChangedEvent.getVersion();
pluginAdapter.setDynamicVersion(version);
LOG.info("********** Version has been updated, new version is {} **********", version);
break;
case VERSION_CLEAR:
pluginAdapter.clearDynamicVersion();
LOG.info("********** Version has been cleared **********");
break;
}
refreshLoadBalancer();
}
private void refreshLoadBalancer() {
ZoneAwareLoadBalancer<?> loadBalancer = loadBalanceListenerExecutor.getLoadBalancer(); ZoneAwareLoadBalancer<?> loadBalancer = loadBalanceListenerExecutor.getLoadBalancer();
if (loadBalancer == null) { if (loadBalancer == null) {
return; return;
} }
LOG.info("********** Version change has been subscribed **********"); // 当规则或者版本更新后,强制刷新Ribbon缓存
// 当版本更新后,强制刷新Ribbon缓存
loadBalancer.updateListOfServers(); loadBalancer.updateListOfServers();
} }
} }
\ No newline at end of file
...@@ -11,10 +11,41 @@ package com.nepxion.discovery.plugin.framework.event; ...@@ -11,10 +11,41 @@ package com.nepxion.discovery.plugin.framework.event;
import java.io.Serializable; import java.io.Serializable;
import org.apache.commons.lang3.StringUtils;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
public class VersionChangedEvent implements Serializable { public class VersionChangedEvent implements Serializable {
private static final long serialVersionUID = 5079797986381461496L; private static final long serialVersionUID = 5079797986381461496L;
public VersionChangedEvent() { public enum EventType {
VERSION_UPDATE,
VERSION_CLEAR;
}
private EventType eventType;
private String version;
public VersionChangedEvent(EventType eventType) {
this(eventType, null);
}
public VersionChangedEvent(EventType eventType, String version) {
if (eventType == EventType.VERSION_UPDATE && StringUtils.isEmpty(version)) {
throw new PluginException("Version value can't be null or empty while updating");
}
this.eventType = eventType;
if (StringUtils.isNotEmpty(version)) {
this.version = version.trim();
}
}
public EventType getEventType() {
return eventType;
}
public String getVersion() {
return version;
} }
} }
\ 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