简单心跳检测
简单心跳检测
- 说明
- 服务器
- 服务器代码
- 服务器处理类
- 服务器定时器
- 客户端
- 客户端定时器
- 客户端处理类
- 客户端测试代码1
- 客户端测试代码2
说明
主要逻辑是服务器监听一个端口,然后客户端每隔一段时间发送报文过来,表明要将该服务注册到服务列表(主要是防止该服务重启了或者注册服务器重启了,然后服务列表不存在该服务)
而服务端也会定时的监控服务列表,发送报文查看该服务是否还存活
服务器
服务器代码
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;public class Resity {public static Map<String,String> map=new HashMap<>();public static void main(String[] args) {try (ServerSocket serverSocket = new ServerSocket(70);){//创建定时器Timer timer = new Timer();timer.schedule(new TimeTask(), 1000, 30000);//1秒后执行,并且每隔30秒后执行一次while(true){Socket socket = serverSocket.accept();System.out.println("注册中心有请求进来了.....");new Thread(new ResityHand(socket)).start();}} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}public static synchronized void putService(String key,String value){map.put(key, value);}public static synchronized void removeService(String key){map.remove(key);}}
服务器处理类
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;public class ResityHand implements Runnable{private Socket socket;public ResityHand(Socket socket){this.socket = socket;}@Overridepublic void run() {try(InputStream inputStream = socket.getInputStream();) {byte[] bs = new byte[1024];inputStream.read(bs,0,2);StringBuffer strBuffer = new StringBuffer();int len;String str = new String(bs,0,2).trim();System.out.println(str);if(str.equals("00")){System.out.println("注册处理。。。。。。。");while( (len = inputStream.read(bs)) != -1){strBuffer.append(new String(bs,0,len));}String info = strBuffer.toString();String[] split = info.split("\\|",2);if( !Resity.map.containsKey(split[0])){Resity.map.put(split[0], split[1]);}}else{System.out.println("非注册处理。。。。。。。");}} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}finally{try {socket.close();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}}}
服务器定时器
import java.net.Socket;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TimerTask;public class TimeTask extends TimerTask{@Overridepublic void run() {StringBuffer stBuffer = new StringBuffer();stBuffer.append("可用服务:\n");//获取服务器列表,对每个服务进行心跳检测Iterator<Entry<String, String>> iterator = Resity.map.entrySet().iterator();while(iterator.hasNext()){Entry<String, String> next = iterator.next();String key = next.getKey();String value = next.getValue();String[] split = value.split("\\|");try (Socket socket = new Socket(split[0], Integer.valueOf(split[1]));){socket.sendUrgentData(0);stBuffer.append(split[0]).append(":").append(split[1]).append("\n");} catch(Exception e){Resity.removeService(key);System.out.println("移除了:\t"+split[0]+":"+split[1]);}}if(Resity.map.size() > 0){System.out.println(stBuffer.toString());}}}
客户端
客户端定时器
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.TimerTask;public class ServiceTimeTask extends TimerTask{private String info;private String ipAddress;private int port;public ServiceTimeTask(String ipAddress,int port,String info){this.ipAddress = ipAddress;this.port = port;this.info = info;}@Overridepublic void run() {try {Socket socketResity = new Socket(ipAddress, port);OutputStream outputStream = socketResity.getOutputStream();outputStream.write(info.getBytes());outputStream.flush();socketResity.close();} catch (UnknownHostException e) {// TODO 自动生成的 catch 块e.printStackTrace();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}}
客户端处理类
import java.io.IOException;
import java.net.Socket;public class SocketHand implements Runnable{private Socket socket;public SocketHand(Socket socket){this.socket = socket;}@Overridepublic void run() {// TODO 自动生成的方法存根try {Thread.sleep(2000);socket.close();} catch (InterruptedException e) {// TODO 自动生成的 catch 块e.printStackTrace();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}}
客户端测试代码1
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Timer;public class Service_1 {public static void main(String[] args) {try (ServerSocket serverSocket = new ServerSocket(91);){String message = "00service01|127.0.0.1|91";String ipAddress = "127.0.0.1";int port = 70;new Timer().schedule(new ServiceTimeTask(ipAddress, port, message), 1000, 30000); while(true){Socket socket = serverSocket.accept();new Thread(new SocketHand(socket)).start();}} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}}
客户端测试代码2
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Timer;public class Service_2 {public static void main(String[] args) {try (ServerSocket serverSocket = new ServerSocket(92);){String message = "00service02|127.0.0.1|92";String ipAddress = "127.0.0.1";int port = 70;new Timer().schedule(new ServiceTimeTask(ipAddress, port, message), 1000, 30000);;while(true){Socket socket = serverSocket.accept();new Thread(new SocketHand(socket)).start();}} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
