监听类
package com.bdd.iot.config.nacos;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.bdd.iot.config.my.MyDefinedConfig;
import com.bdd.iot.service.DingTalkService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author
* @date 2022-08-19 10:26
*/
@Log4j2
@Component
public class ServiceNacosListner {
@Autowired
private MyDefinedConfig myDefinedConfig;
private static final String DING_DING_TOKEN = "钉钉token";
private static final String DING_DING_SECRET = "钉钉secret";
public static List<String> serviceNames = new ArrayList<>();
static {
serviceNames.add("bms");
serviceNames.add("gateway");
}
private static Map<String, Integer> cache = new ConcurrentHashMap<>();
@PostConstruct
public void init() throws Exception {
Properties properties = System.getProperties();
properties.setProperty("serverAddr", myDefinedConfig.getDiscoveryUrl());
properties.setProperty("namespace", "public");
properties.setProperty(PropertyKeyConst.USERNAME, myDefinedConfig.getNacosUsername());
properties.setProperty(PropertyKeyConst.PASSWORD, myDefinedConfig.getNacosPassword());
NamingService naming = NamingFactory.createNamingService(properties);
log.info("当前监听nacos地址:{}",myDefinedConfig.getDiscoveryUrl());
for(String service : serviceNames) {
naming.subscribe(service, event -> {
log.info("触发监听");
List<Instance> instances = ((BddNamingEvent)event).getInstances();
String serviceName = ((BddNamingEvent)event).getServiceName();
cache.computeIfAbsent(serviceName, k -> instances.size());
if(cache.get(serviceName) <instances.size()){
cache.put(serviceName, instances.size());
}
if(instances.size() <cache.get(serviceName)) {
log.info("============服务下线"+ serviceName);
cache.put(serviceName, instances.size());
try {
sendDingDingMessage(2,(BddNamingEvent)event);
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}else {
log.info("============服务上线"+ serviceName+",当前节点数量:"+instances.size());
try {
sendDingDingMessage(1,(BddNamingEvent)event);
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
});
}
}
public void sendDingDingMessage(Integer type,BddNamingEvent event) throws InterruptedException, IOException {
String[] serviceInformation = event.getServiceName().split("@");
DingTalkService.sendDingDing(
"服 务 名 称:"
+serviceInformation[2]
+"\n分 组 名 称:"
+serviceInformation[0]
+"\nI P:"
+event.getIp()
+"\n端 口:"
+event.getPort()
+"\n状 态:"+ (type == 1 ? "上线[对勾]" :"下线[打叉]")
+"\n当前节点数:"+event.getInstances().size()
+"\nnacos 地址:"+ (myDefinedConfig.getActive().equals("prod") ? "127.0.0.1:8848" : myDefinedConfig.getDiscoveryUrl()),
DING_DING_TOKEN,
DING_DING_SECRET);
if(type != 1 && serviceInformation[2].equals("bdd-gateway") && "test".equalsIgnoreCase(myDefinedConfig.getActive())){
Thread.sleep(5000);
Runtime.getRuntime().exec("sh /root/boot.sh bdd-gateway 89");
}
}
}
#BddNamingEvent类
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* W