spring boot 项目启动之前 做接口端口检测
前言
这里主要是用代码的方式实现在服务启动之前,服务先尝试连接一下需要被连接的服务,如果连接失败的话,服务本身启动失败,并且打印启动失败日志
一、springboot启动类中添加websocket连接
1.在启动类中添加方法
public class BacksystemApplication {
private static final Logger logger= LoggerFactory.getLogger(BacksystemApplication.class);
public static void main(String[] args) {
Socket socket=new Socket();
SocketAddress socketAddress=new InetSocketAddress("192.172.0.99",9001);
try {
socket.connect(socketAddress);
SpringApplication.run(BacksystemApplication.class, args);
logger.info("XXXX系统启动成功。");
}catch (IOException e){
logger.error("服务启动失败,连接XXXX服务失败,请检查负服务是否开启!");
}finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
很简单也很容易就解决了上述需求。主要是 我们思路是我们在启动