使用Vertx构建微服务

Vertx

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

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 术语

Verticle

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

Vert.x Instance

Vert.x instance运行在JVM里,Verticle运行在Vert.x instance里。多个Verticles在一个Vert.x instance里异步执行。多个Vert.x instances可以通过Event Bus组成集群。

Concurrency

  • 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线程上,不能被阻塞!

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/

 

posted @ 2017-10-19 17:18  阿凡卢  阅读(21864)  评论(0编辑  收藏  举报