Commit c45e7f5b by Nepxion

重构类结构

parent 60d60ed0
...@@ -29,8 +29,9 @@ public class AdminAutoConfiguration { ...@@ -29,8 +29,9 @@ public class AdminAutoConfiguration {
@Autowired(required = false) @Autowired(required = false)
private Registration registration; private Registration registration;
@SuppressWarnings("rawtypes")
@Bean @Bean
public AdminEndpoint adminEndpoint(ServiceRegistry<?> serviceRegistry) { public AdminEndpoint adminEndpoint(ServiceRegistry serviceRegistry) {
AdminEndpoint adminEndpoint = new AdminEndpoint(serviceRegistry); AdminEndpoint adminEndpoint = new AdminEndpoint(serviceRegistry);
adminEndpoint.setRegistration(registration); adminEndpoint.setRegistration(registration);
......
...@@ -15,39 +15,43 @@ import org.slf4j.Logger; ...@@ -15,39 +15,43 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.mvc.AbstractMvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.nepxion.discovery.plugin.framework.cache.PluginCache; import com.nepxion.discovery.plugin.framework.cache.PluginCache;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant; import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
@ManagedResource(description = "Admin endpoint") @ManagedResource(description = "Admin Endpoint")
public class AdminEndpoint implements MvcEndpoint, ApplicationContextAware, EnvironmentAware { @SuppressWarnings("unchecked")
public class AdminEndpoint extends AbstractMvcEndpoint implements ApplicationContextAware {
private static final Logger LOG = LoggerFactory.getLogger(AdminEndpoint.class); private static final Logger LOG = LoggerFactory.getLogger(AdminEndpoint.class);
private ConfigurableApplicationContext context; private ConfigurableApplicationContext applicationContext;
private Environment environment; @SuppressWarnings("rawtypes")
private ServiceRegistry<?> serviceRegistry; private ServiceRegistry serviceRegistry;
private Registration registration; private Registration registration;
@Autowired @Autowired
private PluginCache pluginCache; private PluginCache pluginCache;
public AdminEndpoint(ServiceRegistry<?> serviceRegistry) { @SuppressWarnings("rawtypes")
public AdminEndpoint(ServiceRegistry serviceRegistry) {
super("/admin", true, true);
this.serviceRegistry = serviceRegistry; this.serviceRegistry = serviceRegistry;
} }
...@@ -55,44 +59,94 @@ public class AdminEndpoint implements MvcEndpoint, ApplicationContextAware, Envi ...@@ -55,44 +59,94 @@ public class AdminEndpoint implements MvcEndpoint, ApplicationContextAware, Envi
this.registration = registration; this.registration = registration;
} }
@RequestMapping(path = "filter", method = RequestMethod.GET) @RequestMapping(path = "blacklist", method = RequestMethod.GET)
@ResponseBody
@ManagedOperation @ManagedOperation
public Object filter(@RequestParam("serviceId") String serviceId, @RequestParam("ip") String ip) { public Object blacklist(@RequestParam("serviceId") String serviceId, @RequestParam("ip") String ip) {
Boolean discoveryControlEnabled = environment.getProperty(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, Boolean.class, Boolean.TRUE); Boolean discoveryControlEnabled = getEnvironment().getProperty(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, Boolean.class, Boolean.TRUE);
if (!discoveryControlEnabled) { if (!discoveryControlEnabled) {
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);
} }
if (registration == null) {
throw new PluginException("No registration found");
}
pluginCache.put(serviceId, ip); pluginCache.put(serviceId, ip);
LOG.info("Add blacklist for serviceId={}, ip={} successfully", serviceId, ip);
return "success"; return "success";
} }
@Override @RequestMapping(path = "clear", method = RequestMethod.GET)
public String getPath() { @ResponseBody
return "/discovery"; @ManagedOperation
} public Object clear(@RequestParam("serviceId") String serviceId) {
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);
}
@SuppressWarnings("rawtypes") if (registration == null) {
@Override throw new PluginException("No registration found");
public Class<? extends Endpoint> getEndpointType() { }
return null;
pluginCache.clear(serviceId);
LOG.info("Clear blacklist for serviceId={} successfully", serviceId);
return "success";
} }
@Override @RequestMapping(path = "status", method = RequestMethod.POST)
public boolean isSensitive() { @ResponseBody
return true; @ManagedOperation
public Object status(@RequestBody String status) {
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");
}
serviceRegistry.setStatus(registration, status);
LOG.info("Set status for serviceId={} status={} successfully", registration.getServiceId(), status);
return "success";
} }
@Override @RequestMapping(path = "deregister", method = RequestMethod.POST)
public void setEnvironment(Environment environment) { @ResponseBody
this.environment = environment; @ManagedOperation
public Object deregister() {
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");
}
serviceRegistry.deregister(registration);
LOG.info("Deregister for serviceId={} successfully", registration.getServiceId());
return "success";
} }
@Override @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (applicationContext instanceof ConfigurableApplicationContext) { if (applicationContext instanceof ConfigurableApplicationContext) {
this.context = (ConfigurableApplicationContext) applicationContext; this.applicationContext = (ConfigurableApplicationContext) applicationContext;
} }
} }
public ConfigurableApplicationContext getApplicationContext() {
return applicationContext;
}
} }
\ No newline at end of file
...@@ -50,7 +50,7 @@ public class PluginCache { ...@@ -50,7 +50,7 @@ public class PluginCache {
} }
} }
public boolean remove(String key) { public boolean clear(String key) {
loadingCache.invalidate(key); loadingCache.invalidate(key);
return Boolean.TRUE; return Boolean.TRUE;
......
...@@ -23,7 +23,7 @@ public class DiscoveryController { ...@@ -23,7 +23,7 @@ public class DiscoveryController {
@Autowired @Autowired
private DiscoveryClient discoveryClient; private DiscoveryClient discoveryClient;
@RequestMapping(value = "/instances", method = RequestMethod.GET) @RequestMapping(path = "/instances", method = RequestMethod.GET)
public List<ServiceInstance> instances() { public List<ServiceInstance> instances() {
return discoveryClient.getInstances("discovery-springcloud-example-b"); return discoveryClient.getInstances("discovery-springcloud-example-b");
} }
......
...@@ -13,4 +13,5 @@ spring.application.discovery.control.enabled=true ...@@ -13,4 +13,5 @@ spring.application.discovery.control.enabled=true
# 开启和关闭远程配置中心规则配置文件读取。一旦关闭,默认读取本地规则配置文件(例如:rule.xml)。缺失则默认为true # 开启和关闭远程配置中心规则配置文件读取。一旦关闭,默认读取本地规则配置文件(例如:rule.xml)。缺失则默认为true
spring.application.discovery.remote.config.enabled=true spring.application.discovery.remote.config.enabled=true
management.port=5432
management.security.enabled=false management.security.enabled=false
\ 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