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為了更加明確的看出連接的效果,並沒有用默認的而是自己新建了一個,修改了一下打印信息。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM