✨ 定时订阅程序启动后注册的服务
This commit is contained in:
parent
107c3d9ac0
commit
3bfa4ad41b
|
@ -2,7 +2,9 @@ package cn.com.xuxiaowei.nacos.sentinel;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
|
@EnableScheduling
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class NacosSentinelApplication {
|
public class NacosSentinelApplication {
|
||||||
|
|
||||||
|
|
|
@ -17,12 +17,11 @@ import org.apache.commons.lang3.RandomStringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
import org.springframework.context.event.EventListener;
|
import org.springframework.context.event.EventListener;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.Properties;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Nacos 注册中心 监听程序
|
* Nacos 注册中心 监听程序
|
||||||
|
@ -34,6 +33,8 @@ import java.util.UUID;
|
||||||
@Component
|
@Component
|
||||||
public class NacosDiscoveryListener {
|
public class NacosDiscoveryListener {
|
||||||
|
|
||||||
|
private final Set<String> subscribes = ConcurrentHashMap.newKeySet();
|
||||||
|
|
||||||
private NacosSentinelDiscoveryProperties nacosSentinelDiscoveryProperties;
|
private NacosSentinelDiscoveryProperties nacosSentinelDiscoveryProperties;
|
||||||
|
|
||||||
private NacosDiscoveryRepository nacosDiscoveryRepository;
|
private NacosDiscoveryRepository nacosDiscoveryRepository;
|
||||||
|
@ -66,7 +67,16 @@ public class NacosDiscoveryListener {
|
||||||
namingService = NamingFactory.createNamingService(properties);
|
namingService = NamingFactory.createNamingService(properties);
|
||||||
}
|
}
|
||||||
catch (NacosException e) {
|
catch (NacosException e) {
|
||||||
throw new RuntimeException(e);
|
log.error(String.format("【%s】连接 Nacos 异常:", logId), e);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
if (namingService == null) {
|
||||||
|
log.error("【{}】NamingService 为 null", logId);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
String serverStatus = namingService.getServerStatus();
|
||||||
|
log.info("【{}】NamingService 状态:{},Nacos 连接状态:{}", logId, serverStatus, serverStatus);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,7 +122,40 @@ public class NacosDiscoveryListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (String serviceName : serviceNames) {
|
for (String serviceName : serviceNames) {
|
||||||
|
subscribe(serviceName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(fixedRate = 5000)
|
||||||
|
public void myScheduledMethod() throws NacosException {
|
||||||
|
|
||||||
|
createNamingService();
|
||||||
|
|
||||||
|
int pageNo = nacosSentinelDiscoveryProperties.getPageNo();
|
||||||
|
int pageSize = nacosSentinelDiscoveryProperties.getPageSize();
|
||||||
|
String groupName = nacosSentinelDiscoveryProperties.getGroupName();
|
||||||
|
|
||||||
|
ListView<String> servicesOfServer = namingService.getServicesOfServer(pageNo, pageSize, groupName);
|
||||||
|
List<String> serviceNames = servicesOfServer.getData();
|
||||||
|
|
||||||
|
int maxLength = 0;
|
||||||
|
for (String serviceName : serviceNames) {
|
||||||
|
maxLength = Math.max(maxLength, serviceName.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String serviceName : serviceNames) {
|
||||||
|
if (!subscribes.contains(serviceName)) {
|
||||||
|
subscribe(serviceName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void subscribe(String serviceName) throws NacosException {
|
||||||
namingService.subscribe(serviceName, event -> {
|
namingService.subscribe(serviceName, event -> {
|
||||||
|
|
||||||
|
subscribes.add(serviceName);
|
||||||
|
|
||||||
NamingEvent namingEvent = (NamingEvent) event;
|
NamingEvent namingEvent = (NamingEvent) event;
|
||||||
List<Instance> instances = namingEvent.getInstances();
|
List<Instance> instances = namingEvent.getInstances();
|
||||||
|
|
||||||
|
@ -130,8 +173,8 @@ public class NacosDiscoveryListener {
|
||||||
int port = instance.getPort();
|
int port = instance.getPort();
|
||||||
String clusterName = instance.getClusterName();
|
String clusterName = instance.getClusterName();
|
||||||
log.info("【{}】Nacos 服务名称: {},IP: {},端口: {},群组名称: {},集群名称: {}", subscribeLogId, serviceName,
|
log.info("【{}】Nacos 服务名称: {},IP: {},端口: {},群组名称: {},集群名称: {}", subscribeLogId, serviceName,
|
||||||
StringUtils.formatLength(ip, 15), StringUtils.formatLength(port, 5),
|
StringUtils.formatLength(ip, 15), StringUtils.formatLength(port, 5), namingEvent.getGroupName(),
|
||||||
namingEvent.getGroupName(), clusterName);
|
clusterName);
|
||||||
|
|
||||||
boolean contains = false;
|
boolean contains = false;
|
||||||
for (Discovery discovery : discoveries) {
|
for (Discovery discovery : discoveries) {
|
||||||
|
@ -199,7 +242,6 @@ public class NacosDiscoveryListener {
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void healthy(String logId, int maxLength, String serviceName, NamingService namingService, boolean healthy)
|
private void healthy(String logId, int maxLength, String serviceName, NamingService namingService, boolean healthy)
|
||||||
throws NacosException {
|
throws NacosException {
|
||||||
|
|
Loading…
Reference in New Issue