java
@Component
@Endpoint(id = "nacos-deregister-instance")
public class NacosDeRegisterInstanceEndpoint {
public static final Logger logger = LoggerFactory.getLogger(NacosDeRegisterInstanceEndpoint.class);
// @Resource
// private SpringClientFactory springClientFactory;
@Resource
private RefreshScope refreshScope;
@Resource
private NacosDiscoveryProperties nacosDiscoveryProperties;
public NacosDeRegisterInstanceEndpoint(NacosDiscoveryProperties nacosDiscoveryProperties) {
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
}
@ReadOperation
public String deregisterInstance() {
String serviceName = nacosDiscoveryProperties.getService();
String groupName = nacosDiscoveryProperties.getGroup();
String clusterName = nacosDiscoveryProperties.getClusterName();
String ip = nacosDiscoveryProperties.getIp();
int port = nacosDiscoveryProperties.getPort();
logger.info("deregister from nacos, serviceName:{}, groupName:{}, clusterName:{}, ip:{}, port:{}", serviceName, groupName, clusterName, ip, port);
try {
nacosDiscoveryProperties.namingServiceInstance().deregisterInstance(serviceName, groupName, ip, port, clusterName);
refreshScope.refresh(serviceName);
} catch (NacosException e) {
logger.error("deregister from nacos error", e);
return "error";
}
return "success";
}
@ConditionalOnClass(Endpoint.class)
@ConditionalOnBean(type = "com.alibaba.cloud.nacos.NacosDiscoveryProperties")
public class NacosDeRegisterInstanceEndpointAutoConfiguration {
@Resource
private NacosDiscoveryProperties nacosDiscoveryProperties;
@ConditionalOnMissingBean
@Bean
public NacosDeRegisterInstanceEndpoint nacosDeRegisterEndpoint() {
return new NacosDeRegisterInstanceEndpoint(nacosDiscoveryProperties);
}
}
}
服务下线
java
management:
endpoints:
web:
exposure:
include: health,shutdown,service-registry,nacos-deregister-instance
endpoint:
health:
show-details: always
shutdown:
enabled: true
nacos-deregister-instance:
enabled: true
java
management:
endpoints:
web:
exposure:
include: health,shutdown,service-registry
path-mapping:
service-registry: deregister
endpoint:
health:
show-details: always
shutdown:
enabled: true
nacos-deregister-instance:
enabled: true