Netty practical scene - heartbeat detection (java)

The main application scenarios of netty are heartbeat detection and network communication. The following mainly introduces the content of heartbeat detection, which is mainly divided into two parts: server and client.

1. The server receives and parses the client information to determine whether the client is still alive

EventLoopGroup pGroup = new NioEventLoopGroup();
EventLoopGroup cGroup = new NioEventLoopGroup();

ServerBootstrap b = new ServerBootstrap();
b.group(pGroup, cGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
//设置日志
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
//Jboss Marshalling解码器MarshallingDecoder和MarshallingEncoder
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
      sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
      sc.pipeline().addLast(new ServerHeartBeatHandler());
   }
});

ChannelFuture cf = b.bind(8888).sync();

cf.channel().closeFuture().sync();
pGroup.shutdownGracefully();
cGroup.shutdownGracefully();

 

2. Client, send the server running information, memory, CPU, etc.

EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
//Jboss Marshalling解码器MarshallingDecoder和MarshallingEncoder
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
      sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
      sc.pipeline().addLast(new ClienHeartBeattHandler());
   }
});

ChannelFuture cf = b.connect("127.0.0.1", 8888).sync();

cf.channel().closeFuture().sync();
group.shutdownGracefully();

 

3. Check the heartbeat connection to determine whether a machine is or needs to obtain the running status of the computer at that time.

Obtain the sigar.jar package. According to the configuration of the local computer, select the environment information of sigar, such as my computer, window 64-bit: sigar-amd64-winnt.dll

The code for obtaining cup information is as follows:

Sigar sigar = new Sigar () ;
CpuInfo infos [] = sigar. getCpuInfoList () ;
CpuPerc cpuList [] = null ;

System . out . println ( "cpu total parameter status:" + sigar. getCpu ()) ;
 System . out . println ( "cpu total percentage status: " + sigar. getCpuPerc ()) ;

cpuList = sigar. getCpuPercList () ;
 for ( int i = 0 ; i < infos. length ; i++ ) { // CpuInfo for single or multiple CPUs
 info = infos [ i ] ;
     System . out . println ( "th" + ( i + 1 ) + "block CPU info" ) ;
     System.out.println ( "CPU total MHz : " + info.getMhz ( ) ) ;// The total amount of CPU MHz
 System . out . println ( "CPU manufacturer: " + info. getVendor ()) ; // Get the CPU vendor, eg: Intel
 System . out . println ( "CPU category: " + info .getModel ()) ; // Get the type of CPU, such as: Celeron
 System . out . println ( "CPU cache size: " + info. getCacheSize ()) ; // Buffer memory size
 printCpuPerc ( cpuList [ i ]) ;
 }

 

 

4. Summary:

Use netty for heartbeat detection, mainly based on the network communication function of netty, the client sends the service operation information (memory, CPU) to the server, the server receives the client message, obtains the running status of the server, and checks according to the obtained server parameters. Whether the client is available.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326058898&siteId=291194637