Netty(一)

简介: Netty(一)

一、异步事件驱动NIO框架Netty介绍

简介:介绍Netty来源,版本,目前在哪些主流公司和产品框架使用

1、Netty是由JBOSS提供的一个java开源看框架,是业界最流行的NIO框架,整合了多种协议

包括FTP,SMTP,HTTP等各种二进制文本协议)的实验经验,精心设计的框架,在多个大型商业项目中得到充分验证。

         1):API使用简单

         2):成熟,稳定

         3):社区活跃,有很多种NIO框架,如mina

         4)  :经过大规模的验证(互联网,大数据,网络游戏,电信行业

2、哪些主流框架产品在用?
      1): 搜索引擎框架(ElasticSerach,github里面的搜索是用es做的:底层是用Netty去做的通信的)。

         2):Hadoop:子项目Avro项目,底层是用Netty作为底层通信框架

         3):阿里巴巴的开源框架RPC框架  Dubbo
                 地址:http://dubbo.apache.org/zh-cn

二、使用JDK自带BIO编写一个Client-Server通信

1、BIO网络编程实战之编写BioServer服务端

    简介:使用jdk自带的Bio编写一个统一时间服务,这也是java的socket编程

代码如下:

  1. package com.weizhaoyang;

  2. import java.io.IOException;
  3. import java.net.ServerSocket;
  4. import java.net.Socket;

  5. public class BioServer {
  6.    public static final int PORT=8080;

  7.    public static void main(String[] args) throws IOException {
  8.        ServerSocket server=null;
  9.        try{
  10.            server=new ServerSocket(PORT);
  11.            System.out.println("the server is start  in port:"+PORT);
  12.            Socket socket=null;
  13.            while(true){
  14.                socket = server.accept();
  15.                new Thread(new TimeServerHandler(socket)).start();
  16.            }
  17.           }catch(Exception e){
  18.        }finally{
  19.            if(server!=null){
  20.                System.out.println("the time server close");
  21.                server.close();
  22.            }
  23.        }
  24.    }
  25. }
  26. package com.weizhaoyang;
  27. import java.io.BufferedReader;
  28. import java.io.InputStreamReader;
  29. import java.io.PrintWriter;
  30. import java.net.Socket;
  31. import java.util.Date;

  32. public class TimeServerHandler implements Runnable{
  33.    private Socket socket;
  34.    public TimeServerHandler(Socket socket){
  35.        this.socket=socket;
  36.    }
  37.    @Override
  38.    public void run() {
  39.        BufferedReader  in=null;
  40.        PrintWriter  out=null;
  41.        try{
  42.            in=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
  43.            out=new PrintWriter(this.socket.getOutputStream(),true);
  44.            String body=null;
  45.            while((body=in.readLine())!=null&&body.length()!=0){
  46.                System.out.println("the time server receive msg"+body);
  47.                out.println(new Date().toString());
  48.            }

  49.        }catch(Exception e){
  50.            e.printStackTrace();
  51.        }finally{
  52.            if(in!=null){
  53.                try{
  54.                    in.close();
  55.                }catch(Exception e){
  56.                    e.printStackTrace();
  57.                }
  58.            }
  59.            if(out!=null){
  60.                try{
  61.                    out.close();
  62.                }catch(Exception e){
  63.                    e.printStackTrace();
  64.                }
  65.            }
  66.            if(this.socket!=null){
  67.                try{
  68.                   this.socket.close();
  69.                }catch(Exception e){
  70.                    e.printStackTrace();
  71.                }
  72.            }
  73.        }
  74.    }
  75. }
  76. package com.weizhaoyang;

  77. import java.io.BufferedReader;
  78. import java.io.InputStreamReader;
  79. import java.io.PrintWriter;
  80. import java.net.Socket;

  81. public class BioClient {
  82.    private static final int PORT=8080;
  83.    private static final String HOST="127.0.0.1";
  84.    public static void main(String[] args) {
  85.        Socket socket=null;
  86.        BufferedReader in=null;
  87.        PrintWriter out=null;
  88.        try{
  89.           socket=new Socket(HOST,PORT);
  90.            in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
  91.            out=new PrintWriter(socket.getOutputStream(),true);
  92.            out.println("i am client");
  93.            String resp=in.readLine();
  94.            System.out.println("当前服务时间"+resp);
  95.        }catch(Exception e){
  96.            e.printStackTrace();
  97.        }finally{
  98.            if(in!=null){
  99.                try{
  100.                    in.close();
  101.                }catch(Exception e){
  102.                    e.printStackTrace();
  103.                }
  104.            }
  105.            if(out!=null){
  106.                try{
  107.                    out.close();
  108.                }catch(Exception e){
  109.                    e.printStackTrace();
  110.                }
  111.            }
  112.            if(socket!=null){
  113.                try{
  114.                    socket.close();
  115.                }catch(Exception e){
  116.                    e.printStackTrace();
  117.                }
  118.            }
  119.        }

  120.    }
  121. }

运行的结果如下:

三、BIO编写Client/Server通信优缺点分析

   简介:讲解BIO的优缺点,为啥不能高并发情况下性能弱

上面用BIO写了时间同步服务器,在很多项目的时候,做一些时间同步也是很有必要的,如果项目部署了很多的服务器,如果要同步每个服务的时间点,就需要这样的服务统一对应他们的时间。

1、服务端启动好,就在那里阻塞,accpet,等待客户端进行连接

2、一有连接进来,然后就会给它分配一个线程,处理对应的请求。

3、在TimeServerHandler处理对应的业务逻辑,编码和解码事件都会在这个类处理。

优点:模型简单

         编码简单

缺点:性能瓶颈,请求数和线程数属于  N:N关系

         高并发情况下,CPU切换线程上下文损耗大

案例:web服务器Tomcat7之前都使用BIO进行,7之后使用NIO,一个线程对应多个请求进行轮询的操作,或者一个线程池去操作。

改进:伪NIO,使用线程池处理业务逻辑,还会出现阻塞的。

相关文章
netty
netty
59 0
|
1月前
|
网络协议 前端开发 安全
Netty
Netty
54 1
|
1月前
|
JavaScript 网络安全
Netty在Firbase中的使用
这篇文章探讨了Netty在Firebase实时数据同步服务中的应用,包括长轮询、HTTP 1.1 keep-alive和流水线化、控制SSL处理器等,展示了Netty如何支持Firebase处理高并发的网络通信和多种协议。
35 1
|
3月前
|
消息中间件 编解码 Java
Netty介绍
Netty介绍
31 4
Netty介绍
|
4月前
|
存储 安全 Java
为什么Netty要造FastThreadLocal?
Netty 的 FastThreadLocal 是一种高效的线程局部变量,设计用于解决标准 ThreadLocal 的性能和内存泄漏问题。FastThreadLocal 通过使用数组而非哈希表存储数据,避免了哈希冲突带来的性能损耗,查询效率达到 O(1)。此外,FastThreadLocal 提供了 remove() 方法和 FastThreadLocalRunnable 类,以防止内存泄漏,确保在执行完成后自动清理对象。相比于 ThreadLocal,FastThreadLocal 具有更高的性能和安全性。
|
4月前
|
存储 Java API
Netty指南
Netty指南
56 2
|
4月前
|
设计模式 网络协议 Java
Netty | 一起来了解了解Netty吧
Netty | 一起来了解了解Netty吧
58 0
|
4月前
|
监控 前端开发 Java
Netty使用篇
Netty使用篇
|
10月前
|
安全 Java Linux
Netty4 使用总结
Netty4 使用总结
46 0
netty练习
netty练习
38 0