|
@@ -0,0 +1,201 @@
|
|
|
|
|
+package cn.reghao.tnb.admin.service;
|
|
|
|
|
+
|
|
|
|
|
+import cn.reghao.tnb.admin.model.vo.SpringCloudInstance;
|
|
|
|
|
+import cn.reghao.tnb.admin.model.vo.SpringCloudNacos;
|
|
|
|
|
+import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
|
|
|
|
+import com.alibaba.nacos.api.NacosFactory;
|
|
|
|
|
+import com.alibaba.nacos.api.PropertyKeyConst;
|
|
|
|
|
+import com.alibaba.nacos.api.config.ConfigService;
|
|
|
|
|
+import com.alibaba.nacos.api.config.listener.Listener;
|
|
|
|
|
+import com.alibaba.nacos.api.exception.NacosException;
|
|
|
|
|
+import com.alibaba.nacos.api.naming.NamingFactory;
|
|
|
|
|
+import com.alibaba.nacos.api.naming.NamingService;
|
|
|
|
|
+import com.alibaba.nacos.api.naming.listener.NamingEvent;
|
|
|
|
|
+import com.alibaba.nacos.api.naming.pojo.Instance;
|
|
|
|
|
+import com.alibaba.nacos.api.naming.pojo.ListView;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Properties;
|
|
|
|
|
+import java.util.concurrent.Executor;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author reghao
|
|
|
|
|
+ * @date 2026-01-21 14:12:16
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+public class NacosManager {
|
|
|
|
|
+ private final NacosDiscoveryProperties nacosDiscoveryProperties;
|
|
|
|
|
+ private NamingService namingService;
|
|
|
|
|
+ private ConfigService configService;
|
|
|
|
|
+
|
|
|
|
|
+ public NacosManager(NacosDiscoveryProperties nacosDiscoveryProperties) {
|
|
|
|
|
+ this.nacosDiscoveryProperties = nacosDiscoveryProperties;
|
|
|
|
|
+ initNacosService();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void initNacosService() {
|
|
|
|
|
+ String serverAddr = nacosDiscoveryProperties.getServerAddr();
|
|
|
|
|
+ String username = nacosDiscoveryProperties.getUsername();
|
|
|
|
|
+ String password = nacosDiscoveryProperties.getPassword();
|
|
|
|
|
+
|
|
|
|
|
+ Properties properties = new Properties();
|
|
|
|
|
+ properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
|
|
|
|
|
+ properties.put(PropertyKeyConst.USERNAME, username);
|
|
|
|
|
+ properties.put(PropertyKeyConst.PASSWORD, password);
|
|
|
|
|
+ try {
|
|
|
|
|
+ this.namingService = NamingFactory.createNamingService(properties);
|
|
|
|
|
+ this.configService = NacosFactory.createConfigService(properties);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error(e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<SpringCloudNacos> getServiceList() {
|
|
|
|
|
+ int pageNumber = 1;
|
|
|
|
|
+ int pageSize = 100;
|
|
|
|
|
+ List<SpringCloudNacos> allServices = new ArrayList<>();
|
|
|
|
|
+ try {
|
|
|
|
|
+ String groupName = "DEFAULT_GROUP";
|
|
|
|
|
+ ListView<String> listView = namingService.getServicesOfServer(pageNumber, pageSize, groupName);
|
|
|
|
|
+ List<String> serviceList = listView.getData();
|
|
|
|
|
+ if (serviceList != null && !serviceList.isEmpty()) {
|
|
|
|
|
+ for (String serviceName : serviceList) {
|
|
|
|
|
+ List<Instance> healthyList = namingService.selectInstances(serviceName, true);
|
|
|
|
|
+ List<Instance> unhealthyList = namingService.selectInstances(serviceName, false);
|
|
|
|
|
+ allServices.add(new SpringCloudNacos(serviceName, healthyList.size(), unhealthyList.size()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error(e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return allServices;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<SpringCloudInstance> getServiceInstances(String serviceName) {
|
|
|
|
|
+ List<SpringCloudInstance> allInstances = new ArrayList<>();
|
|
|
|
|
+ try {
|
|
|
|
|
+ List<Instance> instances = namingService.selectInstances(serviceName, true);
|
|
|
|
|
+ for (Instance instance : instances) {
|
|
|
|
|
+ String instanceAddr = String.format("%s:%s", instance.getIp(), instance.getPort());
|
|
|
|
|
+ //System.out.println("健康节点: " + ins.getIp() + ":" + ins.getPort());
|
|
|
|
|
+ allInstances.add(new SpringCloudInstance(instanceAddr));
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error(e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return allInstances;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void stat() {
|
|
|
|
|
+ String serverAddr = nacosDiscoveryProperties.getServerAddr();
|
|
|
|
|
+ String username = nacosDiscoveryProperties.getUsername();
|
|
|
|
|
+ String password = nacosDiscoveryProperties.getPassword();
|
|
|
|
|
+
|
|
|
|
|
+ String dataId = "search-dev";
|
|
|
|
|
+ String group = "DEFAULT_GROUP";
|
|
|
|
|
+ Properties properties = new Properties();
|
|
|
|
|
+ properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ ConfigService configService = NacosFactory.createConfigService(properties);
|
|
|
|
|
+ String content = configService.getConfig(dataId, group, 5000);
|
|
|
|
|
+ System.out.println("############################################################################");
|
|
|
|
|
+ System.out.println(content);
|
|
|
|
|
+ System.out.println("############################################################################");
|
|
|
|
|
+ configService.addListener(dataId, group, new Listener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void receiveConfigInfo(String configInfo) {
|
|
|
|
|
+ System.out.println("receive:" + configInfo);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Executor getExecutor() {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ /*boolean isPublishOk = configService.publishConfig(dataId, group, "content");
|
|
|
|
|
+ System.out.println(isPublishOk);*/
|
|
|
|
|
+
|
|
|
|
|
+ //Thread.sleep(3000);
|
|
|
|
|
+ //content = configService.getConfig(dataId, group, 5000);
|
|
|
|
|
+ System.out.println("############################################################################");
|
|
|
|
|
+ //System.out.println(content);
|
|
|
|
|
+ System.out.println("############################################################################");
|
|
|
|
|
+
|
|
|
|
|
+ /*boolean isRemoveOk = configService.removeConfig(dataId, group);
|
|
|
|
|
+ System.out.println(isRemoveOk);
|
|
|
|
|
+ Thread.sleep(3000);*/
|
|
|
|
|
+
|
|
|
|
|
+ //content = configService.getConfig(dataId, group, 5000);
|
|
|
|
|
+ System.out.println("############################################################################");
|
|
|
|
|
+ //System.out.println(content);
|
|
|
|
|
+ System.out.println("############################################################################");
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static void nacos() throws InterruptedException, NacosException {
|
|
|
|
|
+ String serverAddr = "192.168.0.209";
|
|
|
|
|
+ String username = "nacos";
|
|
|
|
|
+ String password = "Test_123456";
|
|
|
|
|
+ String group = "DEFAULT_GROUP";
|
|
|
|
|
+ Properties properties = new Properties();
|
|
|
|
|
+ properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
|
|
|
|
|
+ properties.put(PropertyKeyConst.USERNAME, username);
|
|
|
|
|
+ properties.put(PropertyKeyConst.PASSWORD, password);
|
|
|
|
|
+ NamingService namingService = NamingFactory.createNamingService(properties);
|
|
|
|
|
+
|
|
|
|
|
+ String serviceName = "user-service";
|
|
|
|
|
+ namingService.subscribe(serviceName, event -> {
|
|
|
|
|
+ if (event instanceof NamingEvent namingEvent) {
|
|
|
|
|
+ System.out.println("服务名称: " + namingEvent.getServiceName());
|
|
|
|
|
+ // 获取当前最新的所有实例列表
|
|
|
|
|
+ List<Instance> currentInstances = namingEvent.getInstances();
|
|
|
|
|
+ System.out.println("当前最新实例数: " + currentInstances.size());
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 获取所有健康的实例(自动过滤掉不健康的节点)
|
|
|
|
|
+ /*List<Instance> instances = namingService.selectInstances("user-service", true);
|
|
|
|
|
+ for (Instance ins : instances) {
|
|
|
|
|
+ System.out.println("健康节点: " + ins.getIp() + ":" + ins.getPort());
|
|
|
|
|
+ }*/
|
|
|
|
|
+
|
|
|
|
|
+ int pageNo = 1;
|
|
|
|
|
+ int pageSize = 100; // 每页拉取 100 条
|
|
|
|
|
+ List<String> allServices = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ while (true) {
|
|
|
|
|
+ // 获取指定分组(如 DEFAULT_GROUP)下的服务列表
|
|
|
|
|
+ ListView<String> listView = namingService.getServicesOfServer(pageNo, pageSize, "DEFAULT_GROUP");
|
|
|
|
|
+ List<String> serviceList = listView.getData();
|
|
|
|
|
+ if (serviceList != null && !serviceList.isEmpty()) {
|
|
|
|
|
+ allServices.addAll(serviceList);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果获取到的数量达到总数,或者当前页返回为空,说明已经拿到了全部数据
|
|
|
|
|
+ if (allServices.size() >= listView.getCount() || serviceList == null || serviceList.isEmpty()) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ pageNo++; // 翻页
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 打印结果
|
|
|
|
|
+ System.out.println("该 Namespace 下总服务数: " + allServices.size());
|
|
|
|
|
+ System.out.println("服务列表详情: " + allServices);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void main(String[] args) throws InterruptedException, NacosException {
|
|
|
|
|
+ nacos();
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println("goto long sleep");
|
|
|
|
|
+ Thread.sleep(300_000);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|