Commit 22824902 by Nepxion

增加REST推送方式

parent 7090b666
......@@ -9,8 +9,11 @@ package com.nepxion.discovery.plugin.admincenter.endpoint;
* @version 1.0
*/
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
......@@ -33,6 +36,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import com.nepxion.discovery.plugin.framework.cache.PluginCache;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
@ManagedResource(description = "Admin Endpoint")
......@@ -40,6 +44,8 @@ import com.nepxion.discovery.plugin.framework.exception.PluginException;
public class AdminEndpoint extends AbstractMvcEndpoint implements ApplicationContextAware {
private static final Logger LOG = LoggerFactory.getLogger(AdminEndpoint.class);
private static final String ENCODING_UTF_8 = "UTF-8";
private ConfigurableApplicationContext applicationContext;
@SuppressWarnings("rawtypes")
private ServiceRegistry serviceRegistry;
......@@ -48,6 +54,9 @@ public class AdminEndpoint extends AbstractMvcEndpoint implements ApplicationCon
@Autowired
private PluginCache pluginCache;
@Autowired
private PluginPublisher pluginPublisher;
@SuppressWarnings("rawtypes")
public AdminEndpoint(ServiceRegistry serviceRegistry) {
super("/admin", true, true);
......@@ -59,6 +68,31 @@ public class AdminEndpoint extends AbstractMvcEndpoint implements ApplicationCon
this.registration = registration;
}
@RequestMapping(path = "config", method = RequestMethod.POST)
@ResponseBody
@ManagedOperation
public Object config(@RequestBody String config) {
Boolean discoveryControlEnabled = getEnvironment().getProperty(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, Boolean.class, Boolean.TRUE);
if (!discoveryControlEnabled) {
return new ResponseEntity<>(Collections.singletonMap("Message", "Discovery control is disabled"), HttpStatus.NOT_FOUND);
}
if (registration == null) {
throw new PluginException("No registration found");
}
try {
InputStream inputStream = IOUtils.toInputStream(config, ENCODING_UTF_8);
pluginPublisher.publish(inputStream);
} catch (IOException e) {
throw new PluginException("To input stream failed", e);
}
LOG.info("receive config successfully");
return "success";
}
@RequestMapping(path = "blacklist", method = RequestMethod.GET)
@ResponseBody
@ManagedOperation
......
......@@ -21,11 +21,6 @@
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>eventbus-aop-starter</artifactId>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</dependency>
......
......@@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Value;
import com.google.common.eventbus.Subscribe;
import com.nepxion.discovery.plugin.configcenter.constant.ConfigConstant;
import com.nepxion.discovery.plugin.configcenter.loader.ConfigLoader;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
import com.nepxion.eventbus.annotation.EventBus;
import com.nepxion.eventbus.core.Event;
......@@ -32,6 +33,9 @@ import com.nepxion.eventbus.core.Event;
public class ConfigSubscriber {
private static final Logger LOG = LoggerFactory.getLogger(ConfigSubscriber.class);
@Value("${" + PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED + ":true}")
private Boolean discoveryControlEnabled;
@Value("${" + ConfigConstant.SPRING_APPLICATION_DISCOVERY_REMOTE_CONFIG_ENABLED + ":true}")
private Boolean remoteConfigEnabled;
......@@ -56,15 +60,26 @@ public class ConfigSubscriber {
@Subscribe
public void subscribe(Event event) {
if (!discoveryControlEnabled) {
LOG.info("********** Discovery control is disabled, reject to accept remote push **********");
return;
}
if (!remoteConfigEnabled) {
LOG.info("********** Remote config is disabled, reject to accept remote push **********");
return;
}
Object object = event.getSource();
if (object instanceof InputStream) {
LOG.info("********** Remote config change has been retrieved **********");
InputStream inputStream = (InputStream) event.getSource();
InputStream inputStream = (InputStream) object;
parse(inputStream);
}
}
private void parse(InputStream inputStream) {
if (inputStream == null) {
......
......@@ -13,7 +13,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.nepxion.discovery.plugin.configcenter.ConfigParser;
import com.nepxion.discovery.plugin.configcenter.ConfigPublisher;
import com.nepxion.discovery.plugin.configcenter.ConfigSubscriber;
@Configuration
......@@ -27,9 +26,4 @@ public class ConfigAutoConfiguration {
public ConfigSubscriber configSubscriber() {
return new ConfigSubscriber();
}
@Bean
public ConfigPublisher configPublisher() {
return new ConfigPublisher();
}
}
\ No newline at end of file
......@@ -34,5 +34,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>eventbus-aop-starter</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -16,6 +16,7 @@ import org.springframework.context.annotation.Configuration;
import com.nepxion.discovery.plugin.framework.cache.PluginCache;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher;
import com.nepxion.discovery.plugin.framework.strategy.DiscoveryControlStrategy;
import com.nepxion.discovery.plugin.framework.strategy.RegisterControlStrategy;
......@@ -32,6 +33,11 @@ public class PluginAutoConfiguration {
}
@Bean
public PluginPublisher pluginPublisher() {
return new PluginPublisher();
}
@Bean
public ReentrantReadWriteLock reentrantReadWriteLock() {
return new ReentrantReadWriteLock();
}
......
package com.nepxion.discovery.plugin.configcenter;
package com.nepxion.discovery.plugin.framework.event;
/**
* <p>Title: Nepxion Discovery</p>
......@@ -9,18 +9,16 @@ package com.nepxion.discovery.plugin.configcenter;
* @version 1.0
*/
import java.io.InputStream;
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.eventbus.core.Event;
import com.nepxion.eventbus.core.EventControllerFactory;
public class ConfigPublisher {
public class PluginPublisher {
@Autowired
private EventControllerFactory eventControllerFactory;
public void publish(InputStream inputStream) {
eventControllerFactory.getAsyncController().post(new Event(inputStream));
public void publish(Object object) {
eventControllerFactory.getAsyncController().post(new Event(object));
}
}
\ No newline at end of file
......@@ -21,12 +21,12 @@ import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.plugin.configcenter.ConfigPublisher;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher;
// 模拟从远程配置中心接受配置更新
public class DiscoveryConfigSubscriber {
@Autowired
private ConfigPublisher configPublisher;
private PluginPublisher pluginPublisher;
@PostConstruct
public void initialize() {
......@@ -40,7 +40,7 @@ public class DiscoveryConfigSubscriber {
int index = threadLocalRandom.nextInt(5) + 1;
System.out.println("-------------------- rule" + index + ".xml is loaded --------------------");
InputStream inputStream = getInputStream("src/main/resources/rule" + index + ".xml");
configPublisher.publish(inputStream);
pluginPublisher.publish(inputStream);
}
}, 10000L, 15000L);
}
......
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