Vertx.vertx()实例

一、构造方法

1. VertxImpl构造方法 选择 transports protocol , default select 模型

  1. if (options.getPreferNativeTransport()) {
  2. Transport nativeTransport = Transport.nativeTransport();
  3. if (nativeTransport != null && nativeTransport.isAvailable()) {
  4. transport = nativeTransport;// 使用netty-transport-native epoll-ET 或 Kqueue
  5. } else {
  6. transport = Transport.JDK;// java.nio (linux下 epoll-LT)
  7. }
  8. } else {
  9. transport = Transport.JDK;// java.nio (linux下 epoll-LT)
  10. }

native transports protocol

transports TCP UDP SCTP UDT
jdk-NIO X X X X
netty-native-epoll X X
OIO X X X X
  1. PS : select 模型是线性扫描所有 FD , 时间复杂度O(n), 轮训机制.
  2. epoll 模型基于callback机制 , k 活跃数 时间复杂度O(k).
  3. 试用场景: 大量FD 基本都是活跃状态,select epoll 优秀,没有epollctl的回調开销,
  4. 反之有大量idle , dead状态 , epoll更优秀

2. BlockedThreadChecker线程,检查所有线程阻塞, 观察者模式

  1. //默认Timer每 1 seconds检查一次 , 默认warn时间 5 seconds
  2. checker = new BlockedThreadChecker(options.getBlockedThreadCheckInterval()
  3. , options.getWarningExceptionTime());
  4.  
  5. //守护线程,监听所有阻塞线程,遍历扫描(container) WeakHashMap 弱引用,
  6. //ContextImpl.runCloseHooks 执行结束置为null,使GC线程自动回收资源, 防止内存泄漏
  7. timer = new Timer("vertx-blocked-thread-checker", true);
  8. timer.schedule(...);
  1.  

3. eventloop线程池

  1. eventloop线程本质是对netty eventloop的封装.
  1. eventLoopGroup = transport.eventLoopGroup(options.getEventLoopPoolSize()
  2. ,eventLoopThreadFactory, NETTY_IO_RATIO);
  3. // 默认 2 * number of cores ,linux下依赖 /proc/self/status 做计算
  4.  
  5. acceptorEventLoopGroup = transport.eventLoopGroup(
  6. , acceptorEventLoopThreadFactory, );
  7. //负责客户端连接,线程1,单进程服务线程为1即可,否则造成资源浪费
  1. note: eventloop线程checker默认最大阻塞时间 2 seconds,处理时间越长,eventloop事件堆积 就越多,影响服务正常响应,切不可阻塞,阻塞任务可换成worker线程池运行

4. worker线程池

worker线程池是J.U.C包下的FixedThreadPool,默认default 20

  1. ExecutorService workerExec=Executors.newFixedThreadPool(
  2. options.getWorkerPoolSize(),new VertxThreadFactory("vert.x-worker-thread-"
  3. ,checker, true, options.getMaxWorkerExecuteTime()));

note: worker主要用来处理耗时任务, 阻塞同步的调用而设计,checker默认超时时间 60 seconds, 执行行为两种, 串行(采用数据结构队列,FIFO) ; 并行 .

5.internal 线程池

internal线程池是J.U.C包下的FixedThreadPool,默认default 20

  1. ExecutorService internalBlockingExec = Executors.newFixedThreadPool(
  2. options.getInternalBlockingPoolSize()
  3. ,new VertxThreadFactory("vert.x-internal-blocking-"
  4. ,checker, true, options.getMaxWorkerExecuteTime()));

note: 内部使用的线程池,主要用来将阻塞代码异步化

5.初始化加载模块

  1. //创建文件解析器,会创建一个 cache 目录,默认当前执行目录建立隐藏文件夹.vertx,
  2. //使用 kill -15 <pid> 使进程触发ShutdownHook清理 cache目录文件和目录
  3. fileResolver = new FileResolver(this, options.isFileResolverCachingEnabled());
  4.  
  5. /**
  6. * 创建地址解析器,会读取/etc/resolv.conf DNS配置文件,
  7. * resolv.conf手册地址:http://www.man7.org/linux/man-pages/man5/resolv.conf.5.html
  8. */
  9. addressResolver = new AddressResolver(this, options.getAddressResolverOptions());
  10.  
  11. //创建部署Verticle管理器
  12. deploymentManager = new DeploymentManager(this);
  13.  
  14. //启用集群cluster,创建集群管理器
  15. clusterManager = getClusterManager(options);
  16.  
  17. //集群状态下创建HA管理器(高可用)
  18. haManager = new HAManager(this, deploymentManager, clusterManager, options.getQuorumSize(), options.getHAGroup(), haEnabled);
  19.  
  20. //创建并且启动事件总线,local和cluster模式实现不同
  21. createAndStartEventBus(options, resultHandler);
  22.  
  23. //创建sharedData实例,共享数据,local和cluster模式实现不同
  24. sharedData = new SharedDataImpl(this, clusterManager);
  25.  
  26. //创建抽象层的文件系统模块,AsyncFileImpl 依赖 java nio AsynchronousFileChannel
  27. // private final FileSystem fileSystem = getFileSystem();
  28. //不同操作系统call调用有兼容问题,最好开发环境和发布环境一致
  29. Utils.isWindows() ? new WindowsFileSystem(this) : new FileSystemImpl(this)
  30.  
  31. //初始化度量指标
  32. metrics = initialiseMetrics(options);

5. static 块

  1. static {
  2. // 关闭netty的资源泄露器检查,有性能的一定开销
  3. ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.DISABLED);
  4. // 默认使用 Jdk deflater/inflater
  5. System.setProperty("io.netty.noJdkZlibDecoder", "false");
  6. }

二、其它模块

  1. //创建DNSclient
  2. DnsResolverProvider provider = new
  3. DnsResolverProvider(this,addressResolverOptions);
  4.  
  5. //创建异步的httpClient,协议包括 HTTP_1_0 , HTTP_1_1 , HTTP_2 和 HTTPS
  6. new HttpClientImpl(this, options);
  7.  
  8. //创建httpServer, 默认协议协商版本 HTTP/2 , HTTP/1.1 , 支持 https
  9. //(SNI:多证书单服务需要集成KeyCertOptions自己实现)
  10. new HttpServerImpl(this, serverOptions);
  11.  
  12. //创建tcp服务端,支持 tls
  13. new NetServerImpl(this, options);
  14.  
  15. //创建tcp客户端,支持 tls
  16. new NetClientImpl(this, options);
  17.  
  18. //创建udp服务
  19. DatagramSocketImpl.create(this, options);
  20.  
  21. //额外 CLI(command-line interface) , cli 模块

框图

REQUIRED MODEL

FREE MODEL

整体框图

Vertx.vertx()初始框图和模块的更多相关文章

  1. vertx实例的fileSystem文件系统模块

    初始化 //根据OS区分实现 System.getProperty("os.name").toLowerCase(); Utils.isWindows() ? new Window ...

  2. vertx模块HAManager高可用

    HAManager public HAManager(VertxInternal vertx, DeploymentManager deploymentManager, ClusterManager ...

  3. vertx模块DeploymentManager部署管理器

    DeploymentManager public DeploymentManager(VertxInternal vertx) { this.vertx = vertx; loadVerticleFa ...

  4. vertx.io 与nodejs 一个简单的性能比较

    vertx.io 与node 都是可以进行js运行的一个引擎,但是vertx 支持的语言相对于node 多,可以查看官网.今天下网上查询相关的信息 时来了解到vertx.io 性能比node 好,于是 ...

  5. vertx核心类之VertxImpl

    在Vert.x中,Vertx接口是最为重要的一个接口,vertx-core的基础功能都在此接口中提供.这篇文章中我们就来分析一下Vertx接口体系的内部实现以及创建流程.本文对应Vert.x的版本为  ...

  6. vertx简单客户端创建

    import java.util.HashMap;import java.util.Map; import com.yunva.vertx.test.vertproject.util.JsonUtil ...

  7. vertx简单服务创建

    import java.util.HashMap;import java.util.Map; import org.slf4j.Logger;import org.slf4j.LoggerFactor ...

  8. Kotlin & Vertx 构建web服务

    感想 Kotlin 是一门好语言,值得大家了解一下. Vertx 是一个好框架,也值得大家了解一下. Kotlin 写过js,也写过一点点go,主力一直是java.用了kotlin,貌似找到了常用语言 ...

  9. Kotlin Vertx

    Kotlin & Vertx Kotlin 是一门好语言,值得大家了解一下. Vertx 是一个好框架,也值得大家了解一下. Kotlin 写过js,也写过一点点go,主力一直是java.用了 ...

随机推荐

  1. poj-2195(最小费用流)

    题意:给你一个n*m的地图,H代表这个点有一个房子,m代表这个点是一个人,每次h走一步就花费一,问最小花费使得每个人能进入一个房间 代码:建立一个源点和汇点,每个人和源点相连,每个房子和汇点相连,每个 ...

  2. VueRouter和Vue生命周期(钩子函数)

    一.vue-router路由 1.介绍 vue-router是Vue的路由系统,用于定位资源的,在页面不刷新的情况下切换页面内容.类似于a标签,实际上在页面上展示出来的也是a标签,是锚点.router ...

  3. C. Multiplicity 简单数论+dp(dp[i][j]=dp[i-1][j-1]+dp[i-1][j] 前面序列要满足才能构成后面序列)+sort

    题意:给出n 个数 的序列 问 从n个数删去任意个数  删去的数后的序列b1 b2 b3 ......bk  k|bk 思路: 这种题目都有一个特性 就是取到bk 的时候 需要前面有个bk-1的序列前 ...

  4. Magento2自定义命令

    命令命名准则 命名指南概述 Magento 2引入了一个新的命令行界面(CLI),使组件开发人员能够插入模块提供的命令. Command name Command name 在命令中,它紧跟在命令的名 ...

  5. 【并发编程】一个最简单的Java程序有多少线程?

    一个最简单的Java程序有多少线程? 通过下面程序可以计算出当前程序的线程总数. import java.lang.management.ManagementFactory; import java. ...

  6. Python爬虫之二

    1)什么叫做URL url是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址.互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及 ...

  7. 越光后端开发——ygapi(2.新建Model)

    1.新建Model 1.users数据 1.在apps/users/models.py中: from datetime import datetime from django.db import mo ...

  8. redis的list取出数据方式速度测试

    redis测试: package business; import java.io.BufferedReader; import java.io.File; import java.io.FileIn ...

  9. Ubuntu16.04 安装g++6

    https://blog.csdn.net/qq_34877350/article/details/81182022 1.安装gcc-6: sudo apt-get update && ...

  10. 2018年秋季学期《c语言程序设计》助教总结

    <c语言程序设计>第七周助教总结 <c语言程序设计>第八周助教总结 <c语言程序设计>第九周助教总结 <c语言程序设计>第十周助教总结 <c语言程 ...