Vertx.vertx()初始框图和模块
Vertx.vertx()实例
一、构造方法
1. VertxImpl构造方法 选择 transports protocol , default select 模型
if (options.getPreferNativeTransport()) {
Transport nativeTransport = Transport.nativeTransport();
if (nativeTransport != null && nativeTransport.isAvailable()) {
transport = nativeTransport;// 使用netty-transport-native epoll-ET 或 Kqueue
} else {
transport = Transport.JDK;// java.nio (linux下 epoll-LT)
}
} else {
transport = Transport.JDK;// java.nio (linux下 epoll-LT)
}
native transports protocol
| transports | TCP | UDP | SCTP | UDT |
|---|---|---|---|---|
| jdk-NIO | X | X | X | X |
| netty-native-epoll | X | X | — | — |
| OIO | X | X | X | X |
PS : select 模型是线性扫描所有 FD , 时间复杂度O(n), 轮训机制.
epoll 模型基于callback机制 , k 活跃数 时间复杂度O(k).
试用场景: 大量FD 基本都是活跃状态,select 比 epoll 优秀,没有epollctl的回調开销,
反之有大量idle , dead状态 , epoll更优秀
2. BlockedThreadChecker线程,检查所有线程阻塞, 观察者模式
//默认Timer每 1 seconds检查一次 , 默认warn时间 5 seconds
checker = new BlockedThreadChecker(options.getBlockedThreadCheckInterval()
, options.getWarningExceptionTime()); //守护线程,监听所有阻塞线程,遍历扫描(container) WeakHashMap 弱引用,
//ContextImpl.runCloseHooks 执行结束置为null,使GC线程自动回收资源, 防止内存泄漏
timer = new Timer("vertx-blocked-thread-checker", true);
timer.schedule(...);
3. eventloop线程池
eventloop线程本质是对netty eventloop的封装.
eventLoopGroup = transport.eventLoopGroup(options.getEventLoopPoolSize()
,eventLoopThreadFactory, NETTY_IO_RATIO);
// 默认 2 * number of cores ,linux下依赖 /proc/self/status 做计算 acceptorEventLoopGroup = transport.eventLoopGroup(
, acceptorEventLoopThreadFactory, );
//负责客户端连接,线程1,单进程服务线程为1即可,否则造成资源浪费
note: eventloop线程checker默认最大阻塞时间 2 seconds,处理时间越长,eventloop事件堆积 就越多,影响服务正常响应,切不可阻塞,阻塞任务可换成worker线程池运行
4. worker线程池
worker线程池是J.U.C包下的FixedThreadPool,默认default 20
ExecutorService workerExec=Executors.newFixedThreadPool(
options.getWorkerPoolSize(),new VertxThreadFactory("vert.x-worker-thread-"
,checker, true, options.getMaxWorkerExecuteTime()));
note: worker主要用来处理耗时任务, 阻塞同步的调用而设计,checker默认超时时间 60 seconds, 执行行为两种, 串行(采用数据结构队列,FIFO) ; 并行 .
5.internal 线程池
internal线程池是J.U.C包下的FixedThreadPool,默认default 20
ExecutorService internalBlockingExec = Executors.newFixedThreadPool(
options.getInternalBlockingPoolSize()
,new VertxThreadFactory("vert.x-internal-blocking-"
,checker, true, options.getMaxWorkerExecuteTime()));
note: 内部使用的线程池,主要用来将阻塞代码异步化
5.初始化加载模块
//创建文件解析器,会创建一个 cache 目录,默认当前执行目录建立隐藏文件夹.vertx,
//使用 kill -15 <pid> 使进程触发ShutdownHook清理 cache目录文件和目录
fileResolver = new FileResolver(this, options.isFileResolverCachingEnabled()); /**
* 创建地址解析器,会读取/etc/resolv.conf DNS配置文件,
* resolv.conf手册地址:http://www.man7.org/linux/man-pages/man5/resolv.conf.5.html
*/
addressResolver = new AddressResolver(this, options.getAddressResolverOptions()); //创建部署Verticle管理器
deploymentManager = new DeploymentManager(this); //启用集群cluster,创建集群管理器
clusterManager = getClusterManager(options); //集群状态下创建HA管理器(高可用)
haManager = new HAManager(this, deploymentManager, clusterManager, options.getQuorumSize(), options.getHAGroup(), haEnabled); //创建并且启动事件总线,local和cluster模式实现不同
createAndStartEventBus(options, resultHandler); //创建sharedData实例,共享数据,local和cluster模式实现不同
sharedData = new SharedDataImpl(this, clusterManager); //创建抽象层的文件系统模块,AsyncFileImpl 依赖 java nio AsynchronousFileChannel
// private final FileSystem fileSystem = getFileSystem();
//不同操作系统call调用有兼容问题,最好开发环境和发布环境一致
Utils.isWindows() ? new WindowsFileSystem(this) : new FileSystemImpl(this) //初始化度量指标
metrics = initialiseMetrics(options);
5. static 块
static {
// 关闭netty的资源泄露器检查,有性能的一定开销
ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.DISABLED);
// 默认使用 Jdk deflater/inflater
System.setProperty("io.netty.noJdkZlibDecoder", "false");
}
二、其它模块
//创建DNSclient
DnsResolverProvider provider = new
DnsResolverProvider(this,addressResolverOptions); //创建异步的httpClient,协议包括 HTTP_1_0 , HTTP_1_1 , HTTP_2 和 HTTPS
new HttpClientImpl(this, options); //创建httpServer, 默认协议协商版本 HTTP/2 , HTTP/1.1 , 支持 https
//(SNI:多证书单服务需要集成KeyCertOptions自己实现)
new HttpServerImpl(this, serverOptions); //创建tcp服务端,支持 tls
new NetServerImpl(this, options); //创建tcp客户端,支持 tls
new NetClientImpl(this, options); //创建udp服务
DatagramSocketImpl.create(this, options); //额外 CLI(command-line interface) , cli 模块
框图
REQUIRED MODEL
FREE MODEL
整体框图
Vertx.vertx()初始框图和模块的更多相关文章
- vertx实例的fileSystem文件系统模块
初始化 //根据OS区分实现 System.getProperty("os.name").toLowerCase(); Utils.isWindows() ? new Window ...
- vertx模块HAManager高可用
HAManager public HAManager(VertxInternal vertx, DeploymentManager deploymentManager, ClusterManager ...
- vertx模块DeploymentManager部署管理器
DeploymentManager public DeploymentManager(VertxInternal vertx) { this.vertx = vertx; loadVerticleFa ...
- vertx.io 与nodejs 一个简单的性能比较
vertx.io 与node 都是可以进行js运行的一个引擎,但是vertx 支持的语言相对于node 多,可以查看官网.今天下网上查询相关的信息 时来了解到vertx.io 性能比node 好,于是 ...
- vertx核心类之VertxImpl
在Vert.x中,Vertx接口是最为重要的一个接口,vertx-core的基础功能都在此接口中提供.这篇文章中我们就来分析一下Vertx接口体系的内部实现以及创建流程.本文对应Vert.x的版本为 ...
- vertx简单客户端创建
import java.util.HashMap;import java.util.Map; import com.yunva.vertx.test.vertproject.util.JsonUtil ...
- vertx简单服务创建
import java.util.HashMap;import java.util.Map; import org.slf4j.Logger;import org.slf4j.LoggerFactor ...
- Kotlin & Vertx 构建web服务
感想 Kotlin 是一门好语言,值得大家了解一下. Vertx 是一个好框架,也值得大家了解一下. Kotlin 写过js,也写过一点点go,主力一直是java.用了kotlin,貌似找到了常用语言 ...
- Kotlin Vertx
Kotlin & Vertx Kotlin 是一门好语言,值得大家了解一下. Vertx 是一个好框架,也值得大家了解一下. Kotlin 写过js,也写过一点点go,主力一直是java.用了 ...
随机推荐
- kettle表更新/插入更新
更新: 1.1更新表: 目标表: 插入更新: 2.1匹配表: 目标表: 插入/更新转换 目标表
- 【数学建模】MatLab 数据读写方法汇总
1.读入 txt 文件数据. load xxx.txt A=load(‘xxx.txt’) A=dlmread(‘xxx.txt’) A=importdata(‘xxx.txt’) 例:将身高体重的 ...
- AssetBundle打包
为热更新打基础(xlua\tolua) 素材.源码链接:http://www.sikiedu.com/course/74/task/1812/show 一.AssetBundle的定义和作用 1,As ...
- [BZOJ 2480] [SPOJ 3105] Mod
Description 已知数 \(a,p,b\),求满足 \(a^x\equiv b\pmod p\) 的最小自然数 \(x\). Input 每个测试文件中最多包含 \(100\) 组测试数据. ...
- [BZOJ 3992] [SDOI 2015] 序列统计
Description 传送门 Solution [一] 设 \(f[i][j]\) 表示前 \(i\) 个数的乘积在模 \(p\) 意义下等于 \(j\) 的方案数,有 \[ f[i][j]=\su ...
- bzoj 1208: [HNOI2004]宠物收养所 (Treap)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1208 题面: 1208: [HNOI2004]宠物收养所 Time Limit: 10 ...
- docker_weave
安装 curl -L git.io/weave -o /usr/local/bin/weave chmod a+x /usr/local/bin/weave 启动 weave weave launch ...
- sqlalchemy和pymysql通过ssh连接远程mysql服务器
首先需要一个模块sshtunnel,如果没有直接pip install sshtunnel 其实连个连接方式非常像: pymysql连接方式: import pymysql from sshtunne ...
- Math & Number Theory
数学 数论: 莫比乌斯反演 矩阵游戏 小学数学,欧拉定理 组合: 线性代数: 高斯消元 其他: 一些题目
- 学习python第二天
编程语言分为哪几种? 1. 机器语言:是通过直接编写二进制指令对计算机下达指令的编程方式 -- 0000,0000,00000000001:加载 暂存区A 存储器地址1 2. 汇编语言:是通过编写二进 ...