使用Vertx构建微服务

Vertx

Vert.x is a tool-kit for building reactive applications on the JVM.(Vertx是运行在JVM上用来构建reactive application的工具集)html

Vertx Design

  • 响应式的(Responsive):一个响应式系统须要在合理的时间内处理请求。
  • 弹性的(Resilient):一个响应式系统必须在遇到异常(崩溃,超时, 500 错误等等)的时候保持响应的能力,因此它必需要为异常处理 而设计。
  • 可伸缩的(Elastic):一个响应式系统必须在不一样的负载状况下都要保持响应能力,因此它必须能伸能缩,而且能够利用最少的资源来处理负载。
  • 消息驱动(Message driven):一个响应式系统的各个组件之间经过 异步消息传递 来进行交互。
  • 支持多种语言:只要能运行在JVM上的语言,基本都支持。
  • 简单的并发模型:就像写单线程代码同样简单,多线程并发由Vertx控制。
  • 支持Event Bus:在同一个vertx集群,各个verticle 实例间能够经过event bus通讯。同时也支持跨进程的TCP Event Bus (tcp-eventbus-bridge)
  • Vertx与Netty的关系:Vertx使用Netty处理全部的IO。

Vertx 术语

Verticlejava

Vertx部署和运行的代码。Verticles可使用多种语言实现。node

Vert.x Instancereact

Vert.x instance运行在JVM里,Verticle运行在Vert.x instance里。多个Verticles在一个Vert.x instance里异步执行。多个Vert.x instances能够经过Event Bus组成集群。git

Concurrencygithub

  • Standard Verticle:始终在同一个Event Loop线程上执行,同一个Verticle 中的逻辑能够避免资源竞争和死锁。
  • Worker Verticle:在worker threads上执行,Vertx保证最多同时只有一个worker thread在执行逻辑,避免竞争和死锁。可是在不一样的时刻,可能由不一样的线程在执行。
  • Multi-threaded worker verticle:和Worker Verticle相似,可是不保证线程安全,在同一时刻,可能由多个线程在执行。

Event-based Programming Model安全

使用“事件驱动”的模式去编写代码,采用异步回调handler的方式去处理事件,不能被阻塞!多线程

Event Loops并发

Vert.x的核心线程池,默认每一个Verticle运行在本身的Event Loop线程上,不能被阻塞!app

Message Passing

不一样的Verticle能够经过Event Bus通讯,集群模式下不一样主机上的Verticle也能够经过Event Bus通讯,来实现distributed applications。

Shared data

不一样的Verticle之间能够经过 Shared Data 共享数据。

Vert.x Architecture

Verticle 是执行单元,在同一个Vertx实例中能够同时执行多个Verticle。Verticle在event-loop线程上执行,多个Vert.x instances能够在多个host上执行,各个Verticles 经过event bus通讯。

 

Vert.x Thread Pool

Vert.x 有三种线程池

Acceptor: 用来接收socket链接. 只要有一个服务port须要监听,就会有一个accept线程。

acceptorEventLoopGroup = new NioEventLoopGroup(1, acceptorEventLoopThreadFactory);
acceptorEventLoopGroup.setIoRatio(100);

Event Loops: 不断地轮询获取事件,并将获取到的事件分发到对应的事件处理器中进行处理,永远不要阻塞Event Loop线程

eventLoopThreadFactory = new VertxThreadFactory("vert.x-eventloop-thread-", checker, false, options.getMaxEventLoopExecuteTime());
eventLoopGroup = new NioEventLoopGroup(options.getEventLoopPoolSize(), eventLoopThreadFactory);
eventLoopGroup.setIoRatio(NETTY_IO_RATIO);

Worker Threads: Worker线程池,它其实就是一种Fixed Thread Pool:

ExecutorService workerExec = Executors.newFixedThreadPool(options.getWorkerPoolSize(),
    new VertxThreadFactory("vert.x-worker-thread-", checker, true, options.getMaxWorkerExecuteTime()));
PoolMetrics workerPoolMetrics = isMetricsEnabled() ? metrics.createMetrics(workerExec, "worker", "vert.x-worker-thread", options.getWorkerPoolSize()) : null;
workerPool = new WorkerPool(workerExec, workerPoolMetrics);

Why is Hazelcast Used?

Vert.x 使用 Hazelcast 做为一个In-Memory Data Grid (IMDG). Hazelcast 是一个内嵌的组件。

若是使用集群模式,Vert.x 默认使用Hazelcast来管理在各个不一样Host上的Instance通讯。Hazelcast 也是一种分布式的存储,Hazelcast 是Vert.x Event Bus 在集群模式下的实现基础,也是Vertx分布式共享内存的Shared Map的基础。

微服务

什么是微服务?

  1. split the application into a set of decoupled components providing defined services 把一个应用程序分红各个独立的解耦的组件提供服务。(defined means with a known interface or API)

  2. allow the components communicate with whatever protocol the choose, often REST, but not necessarily 组件之间使用协议通讯,一般是REST。

  3. allow the components use whatever languages and technologies they want 组件能够用不一样的语言和技术实现。

  4. allow each component be developed, released and deployed independently 组件可独立的开发、发布和部署。

  5. allow the deployments be automated in their own pipeline 组件在本身的环境下自动部署。

  6. allow the orchestration of the whole application be reduced to the barest minimum 让整个程序的依赖尽可能最少。

 一个微服务实例

The Micro-Trader Application

The application uses several types of services:

  • HTTP endpoint (i.e. REST API) - this service is located using an HTTP URL.

  • Service proxies - these are asynchronous services exposed on the event bus using an RPC interaction mechanism, the service is located using an (event bus) address.

  • Message sources - these are components publishing messages on the event bus, the service is located using an (event bus) address.

源码:https://github.com/cescoffier/vertx-microservices-workshop

 

 

Reference:

http://vertx.io/docs/guide-for-java-devs/

http://vertx.io/docs/vertx-core/java/

https://www.cubrid.org/blog/inside-vertx-comparison-with-nodejs/
https://www.cubrid.org/blog/understanding-vertx-architecture-part-2

https://medium.com/@levon_t/java-vert-x-starter-guide-part-1-30cb050d68aa
https://medium.com/@levon_t/java-vert-x-starter-guide-part-2-worker-verticles-c49866df44ab

http://www.sczyh30.com/vertx-blueprint-microservice/cn/index.html

《Vert.x - From zero to (micro)-hero》http://escoffier.me/vertx-hol/

相关文章
相关标签/搜索