zoukankan      html  css  js  c++  java
  • java简单使用netty

    一 整合

    由于本人的学过的技术太多太乱了,于是决定一个一个的整合到一个springboot项目里面。

    附上自己的github项目地址 https://github.com/247292980/spring-boot

    附上汇总博文地址 https://www.cnblogs.com/ydymz/p/9391653.html

    以整合功能

    spring-boot,FusionChart,thymeleaf,vue,ShardingJdbc,mybatis-generator,微信分享授权,drools,spring-security,spring-jpa,webjars,Aspect,drools-drt,rabbitmq,zookeeper,mongodb,mysql存储过程,前端的延迟加载

    这次就来整合下netty

    之前都是用java源码来做socket连接,现在试一下netty。

    三 代码

    客户端

    public class NettyClientDemo {
        public static String host = "127.0.0.1";
        public static int port = 1234;
    
        public static void main(String[] args) throws Exception {
            EventLoopGroup group = new NioEventLoopGroup();
            try {
                //创建 Bootstrap
                Bootstrap b = new Bootstrap();
                //指定 EventLoopGroup 以处理客户端事件;需要适用于 NIO 的实现
                b.group(group)
                        //适用于 NIO 传输的Channel 类型
                        .channel(NioSocketChannel.class)
                        //设置服务器的InetSocketAddress
                        .remoteAddress(new InetSocketAddress(host, port))
                        //在创建Channel时,向 ChannelPipeline中添加一个 EchoClientHandler实例
                        .handler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            public void initChannel(SocketChannel ch) throws Exception {
                                ch.pipeline().addLast(new EchoClientHandler());
                            }
                        });
                //连接到远程节点,阻塞等待直到连接完成
                ChannelFuture f = b.connect().sync();
                //阻塞,直到Channel 关闭
                f.channel().closeFuture().sync();
            } finally {
                //关闭线程池并且释放所有的资源
                group.shutdownGracefully().sync();
            }
        }
    }

    服务器

    public class NettyServerDemo {
        public static final int port = 1234;
    
        public static void main(String[] args) throws Exception {
            final EchoServerHandler serverHandler = new EchoServerHandler();
            //(1) 创建EventLoopGroup
            EventLoopGroup group = new NioEventLoopGroup();
            try {
                //(2) 创建ServerBootstrap
                ServerBootstrap b = new ServerBootstrap();
                b.group(group)
                        //(3) 指定所使用的 NIO 传输 Channel
                        .channel(NioServerSocketChannel.class)
                        //(4) 使用指定的端口设置套接字地址
                        .localAddress(new InetSocketAddress(port))
                        //(5) 添加一个EchoServerHandler到于Channel的 ChannelPipeline
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            public void initChannel(SocketChannel ch) throws Exception {
                                //EchoServerHandler 被标注为@Shareable,所以我们可以总是使用同样的实例
                                //这里对于所有的客户端连接来说,都会使用同一个 EchoServerHandler,因为其被标注为@Sharable,
                                //这将在后面的章节中讲到。
                                ch.pipeline().addLast(serverHandler);
                            }
                        });
                //(6) 异步地绑定服务器;调用 sync()方法阻塞等待直到绑定完成
                ChannelFuture f = b.bind().sync();
                System.out.println(NettyServerDemo.class.getName() +
                        " started and listening for connections on " + f.channel().localAddress());
                //(7) 获取 Channel 的CloseFuture,并且阻塞当前线程直到它完成
                f.channel().closeFuture().sync();
            } finally {
                //(8) 关闭 EventLoopGroup,释放所有的资源
                group.shutdownGracefully().sync();
            }
        }
    }

    pom.xml

       <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.2</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-all</artifactId>
                <version>5.0.0.Alpha1</version>
            </dependency>
        </dependencies>

    四  总结

    pom.xml拉上来是因为百度的教程大多没有,而netty似乎向下兼容没有做的很好。

    代码理解有点网络编程基础的基本都知道是什么意思。

    EchoServerHandler 和 EchoClientHandler为了更加明确的看出连接的效果,并没有用默认的而是自己新建了一个,修改了一下打印信息。

  • 相关阅读:
    如何优化多个关键字
    如何优化中小型企业网站
    SEO内容为王之如何创造伪原创
    中央电化教育馆教学资源库介绍
    教育网络游戏《学乐吧》介绍
    教育技术学专业主干课程系列教材(共八本)
    百度,google对网站首页内页权重分配的区别
    网站内链对网站排名有那些作用和影响?
    网站好排名,页面内链少不了
    公务员考试与事业单位考试
  • 原文地址:https://www.cnblogs.com/ydymz/p/9849879.html
Copyright © 2011-2022 走看看