1、导入依赖
<**dependency**> <**groupId**>io.netty</**groupId**> <**artifactId**>netty-all</**artifactId**> <**version**>4.1.56.Final</**version**> </**dependency**>
<**dependency**> <**groupId**>org.projectlombok</**groupId**> <**artifactId**>lombok</**artifactId**> <**version**>1.18.4</**version**> </**dependency**>
<**dependency**> <**groupId**>org.springframework.boot</**groupId**> <**artifactId**>spring-boot-starter</**artifactId**> </**dependency**>
2、编写Netty服务端处理器
package com.lgdz.netty.server;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import lombok.extern.slf4j.Slf4j;
@Slf4j public class NettyServerHandler extends ChannelInboundHandlerAdapter { _/** _ * _客户端连接会触发 _ _*/ _ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { log.info("Channel active......"); } _/** _ * _客户端发消息会触发 _ _*/ _ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { log.info("服务器收到消息: {}", msg.toString()); ctx.write("ok"); ctx.flush(); } _/** _ * _发生异常触发 _ _*/ _ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } }
3、编写Netty服务端初始化器
package com.lgdz.netty.server;
import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import io.netty.util.CharsetUtil;
_/**
_ * @author _Gjing
_ * _* netty__服务初始化器
_ **/ public class ServerChannelInitializer extends ChannelInitializer
4、编写Netty服务启动
package com.lgdz.netty.server;
import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component;
import java.net.InetSocketAddress;
_/** _ * @author _Gjing _ * * _服务启动监听器 _ **/ @Component @Slf4j public class NettyServer { public void start(InetSocketAddress socketAddress) { //new _一个主线程组 _ EventLoopGroup bossGroup = new NioEventLoopGroup(1); //new _一个工作线程组 _ EventLoopGroup workGroup = new NioEventLoopGroup(200); ServerBootstrap bootstrap = new ServerBootstrap() .group(bossGroup, workGroup) .channel(NioServerSocketChannel.class) .childHandler(new ServerChannelInitializer()) .localAddress(socketAddress) _//_设置队列大小 _ .option(ChannelOption.SO_BACKLOG, 1024) // 两小时内没有数据的通信时,TCP__会自动发送一个活动探测数据报文 _ .childOption(ChannelOption.SO_KEEPALIVE, true); _//绑定端口,__开始接收进来的连接 _ try { ChannelFuture future = bootstrap.bind(socketAddress).sync(); log.info("服务器启动开始监听端口: {}", socketAddress.getPort()); future.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { _//__关闭主线程组 _ bossGroup.shutdownGracefully(); _//__关闭工作线程组 _ workGroup.shutdownGracefully(); } } }
5、启动类
package com.lgdz;
import com.lgdz.netty.server.NettyServer; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import java.net.InetSocketAddress;
/** * @ClassName: App * _@Description:_启动类 * @author: dxd * @date: 2020-9-14 14:17:24 * @Copyright: _力戈电子科技有限公司 _ */ @SpringBootApplication @MapperScan("com.lgdz.mapper") public class App extends SpringBootServletInitializer{
public static void main(String[] args) { SpringApplication.run(App.class, args); //_启动__Netty__服务端 _ NettyServer nettyServer = new NettyServer(); nettyServer.start(new InetSocketAddress("192.168.10.175", 10066)); }//end_ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(App.class); }//end_ }
6.待解决问题
- 粘包半包处理
- TCP硬件注册包处理 (跟硬件协议相关)
- 心跳处理。集成redis处理设备在线与否 (redis类博客有完整教程)
- 利用TCP连接session,向硬件发送命令
7.该博客整合Netty产生的问题截图
- 粘包现象。
- 硬件注册包频繁。