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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM