使用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/

相關文章
相關標籤/搜索