Mais conteúdo relacionado Semelhante a 浅说Vertx (20) 浅说Vertx2. 我是谁
入行时间
10+年
社区身份
GroovyQ(www.groovyq.net)站长
西安社区组织者(OpenParty、QClub西安)
个人兴趣
广泛……,目前专注于vertx
联系方式
Twitter/微博:@foxgem
邮箱:jianhgreat@hotmail.com
3. 什么是Vert.x?
文艺范儿的官方定义
Vert.x is a
lightweight, high
performance
application platform
for the JVM that's
designed for
modern
mobile, web, and
enterprise
applications.
胡氏简化版
Node.js on JVM
4. 典型的应用场景
vertx缺省支持
Socket Server
Http Server
WebSocket
SockJS
典型应用
解析实时数据帧的通信
服务器
Web服务器
向前端屏蔽后端数据源
位置的数据服务器
分布式消息处理器
……
8. Vert.x的若干关键词
事件总线简单示例
发送端
接收端
eb = vertx.eventBus
…
eb.publish('out.client', repository.findAllClients())
eb.registerHandler('out.client', function(message) {
that.total(message.total);
that.clients.removeAll();
for (var i = 0; i < message.total; i++) {
that.clients.push(message.clients[i]);
};
});
12. Vert.x应用的开发
1. 建立工程
Vertx应用即普通的Java工程
apply plugin: 'groovy'
vertxHome = '...'
repositories {
flatDir dirs: "${vertxHome}/lib"
}
dependencies {
groovy localGroovy()
compile gradleApi()
compile fileTree(dir: "${vertxHome}/lib", includes:
['*.jar'])
runtime fileTree(dir: "${vertxHome}/lib", includes:
['*.jar'])
runtime files("${vertxHome}/conf")
}
14. Vert.x应用的开发
3. 编译工程
4. 运行
小技巧
可使用-instances参数指定同时运行的Verticle
的实例数,最大化利用CPU核数
Vertx提供了gradle工程的应用模板
https://github.com/vert-x/vertx-gradle-
template
gradle build
vertx run Server.groovy -cp ./build/libs/*.jar
15. 开发经验
同步 -> 异步
分而治之,充分利用Verticle
事件总线很方便,设计事件体系是关键
尤其是事件地址,专项专用
利用动态部署实现功能的动态增减
永远不要阻塞事件循环,IO操作转移到Worker
Verticle
例子1:
普通Verticle -> 事件总线 -> Worker Verticle -> DB Server
例子2
利用Worker Verticle实现连接池,在上例中部署多个worker
verticle实例即可,每个Worker中保持与DB的连接