• springboot+netty


    1、服务端

    1)、pom.xml

      <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>5.0.0.Alpha2</version>
      </dependency>

    2)、DiscardServerHandler 

    @Component
    @ChannelHandler.Sharable
    public class DiscardServerHandler extends ChannelInboundHandlerAdapter {

       /*****Autowired 注入为空Null解决如下

           // 参考:https://www.cnblogs.com/orzlin/p/9235918.html

      private static DiscardServerHandler discardServerHandler;
      @Autowired
      private BaseService baseService;

      @PostConstruct
      public void init(){
        discardServerHandler=this;
      }

      @Override
      public void channelRead(ChannelHandlerContext ctx,Object msg){
        try{
          ByteBuf in=(ByteBuf)msg;
          System.out.println("收到你的客户端内容是");
          System.out.println(in.toString(CharsetUtil.UTF_8));
          discardServerHandler.baseService.test();
        }finally {
          ReferenceCountUtil.release(msg);
        }
      }

      @Override
      public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause){
        cause.printStackTrace();
        ctx.close();
      }
    }

    3)、ChildChannelHandler 

    @Component
    public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {

        @Autowired
        private DiscardServerHandler discardServerHandler;

        @Override
        protected void initChannel(SocketChannel socketChannel) throws Exception {
          //ByteBuf byteBuf=Unpooled.copiedBuffer("$".getBytes());
          //socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,byteBuf));
          socketChannel.pipeline().addLast(new DiscardServerHandler());
        }
    }

    4)、DiscardServer 


    @Component
    public class DiscardServer {
      @Resource
      private ChildChannelHandler childChannelHandler;
      public void run(int port) throws Exception{
        EventLoopGroup bossGroup=new NioEventLoopGroup();
        EventLoopGroup workerGroup=new NioEventLoopGroup();
        System.out.println("准备运行端口:"+port);
      try {
        ServerBootstrap bootstrap=new ServerBootstrap();
        bootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class)
        .option(ChannelOption.SO_BACKLOG,128).
        childHandler(childChannelHandler);
        ChannelFuture f=bootstrap.bind(port).sync();
        f.channel().closeFuture().sync();
      }finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
      }
      }
    }

    5)、BaseServiceImpl   BaseService 


    @Service
    public class BaseServiceImpl implements BaseService {

      @Override
      public void test() {
        System.out.println("调用service服务");
      }
    }


    public interface BaseService {
      /**
      * 测试接口
      */
      void test();
    }

    6)、App 
    @SpringBootApplication
    public class App implements CommandLineRunner {

      @Resource
      private DiscardServer discardServer;

      public static void main(String[] args)
      {
        SpringApplication.run(App.class,args);
      }


      @Override
      public void run(String... args) throws Exception {
        discardServer.run(3018);
      }
    }

    2、客户端

    public class AppClient {
    public static void main(String[] args) {


      try {
        String content = "我客户端向服务端发送的信息【年低准备过春节了 年低准备过春节了】";
        String sendMsg = "$ from client port 2020 " + content + " $";
        Socket socket = new Socket("localhost", 3018);
        OutputStream outputStream = socket.getOutputStream();
        PrintWriter printWriter = new PrintWriter(outputStream);
        printWriter.write(sendMsg);
        printWriter.flush();
        socket.shutdownOutput();
        socket.close();

      } catch (Exception e) {
        e.printStackTrace();
      }
    }


    }

    参考https://www.cnblogs.com/guoyuchuan/p/9581283.html

  • 相关阅读:
    HanLP vs LTP 分词功能测试
    HanLP中文分词Lucene插件
    pyhanlp:hanlp的python接口
    Hanlp自然语言处理工具之词法分析器
    基于结构化感知机的词性标注与命名实体识别框架
    分词工具Hanlp基于感知机的中文分词框架
    Android环境下hanlp汉字转拼音功能的使用介绍
    Javascript JSON语法基础
    mui ajax方法详解
    HBuilder的webview操作
  • 原文地址:https://www.cnblogs.com/smallfa/p/15656696.html
Copyright © 2020-2023  润新知