使用Vertx构建微服务

时间:2021-09-28 13:53:27

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通信。

使用Vertx构建微服务

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

使用Vertx构建微服务

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/

使用Vertx构建微服务的更多相关文章

  1. Chris Richardson微服务翻译:构建微服务之使用API网关

    Chris Richardson 微服务系列翻译全7篇链接: 微服务介绍 构建微服务之使用API网关(本文) 构建微服务之微服务架构的进程通讯 微服务架构中的服务发现 微服务之事件驱动的数据管理 微服 ...

  2. 微服务系列(二):使用 API 网关构建微服务

    编者的话|本文来自 Nginx 官方博客,是微服务系列文章的第二篇,本文将探讨:微服务架构是如何影响客户端到服务端的通信,并提出一种使用 API 网关的方法. 作者介绍:Chris Richardso ...

  3. 0102-使用 API 网关构建微服务

    一.移动客户端如何访问这些服务 1.1.客户端与微服务直接通信[很少使用] 从理论上讲,客户端可以直接向每个微服务发送请求.每个微服务都有一个公开的端点(https ://.api.company.n ...

  4. 使用 API 网关构建微服务-2

    「Chris Richardson 微服务系列」使用 API 网关构建微服务 Posted on 2016年5月12日 编者的话|本文来自 Nginx 官方博客,是微服务系列文章的第二篇,本文将探讨: ...

  5. [译]Spring构建微服务

    此文为译文,原文地址 介绍 本文通过一个使用Spring.Spring Boot和Spring Cloud的小例子来说明如何构建微服务系统. 我们可以通过数个微服务组合成一个大型系统. 我们可以想象下 ...

  6. 构建微服务:Spring boot

    构建微服务:Spring boot 在上篇文章构建微服务:Spring boot 提高篇中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jp ...

  7. 如何使用 Java 构建微服务?

    [编者按]微服务背后的大理念是将大型.复杂且历时长久的应用在架构上设计为内聚的服务,这些服务能够随着时间的流逝而演化.本文主要介绍了利用 Java 生态系统构建微服务的多种方法,并分析了每种方法的利弊 ...

  8. 构建微服务-使用OAuth 2.0保护API接口

    微服务操作模型 基于Spring Cloud和Netflix OSS 构建微服务-Part 1 基于Spring Cloud和Netflix OSS构建微服务,Part 2 在本文中,我们将使用OAu ...

  9. 基于Spring Cloud和Netflix OSS构建微服务,Part 2

    在上一篇文章中,我们已使用Spring Cloud和Netflix OSS中的核心组件,如Eureka.Ribbon和Zuul,部分实现了操作模型(operations model),允许单独部署的微 ...

随机推荐

  1. WinForm与WPF下跨线程调用控件

    Winform下: public delegate void UpadataTextCallBack(string str,TextBox text); public void UpadtaText( ...

  2. JAVA 冒泡算法

    每种语言都有冒泡JAVA实现如下: public static void main(String args[]){ System.out.println("Start"); int ...

  3. HTML 学习笔记(块 和 类)

    HTML <div> 和 <span> 可以通过<div>和<span>将HTML元素组合起来. HTML块元素 大多数HTML元素被定义为块级元素或者 ...

  4. node&period;js学习的资源整理

    node中文社区 Node.js专业中文社区:https://cnodejs.org/ node文档 node.js 中文api :http://nodeapi.ucdok.com/ node.js入 ...

  5. Android 命令行编译、打包生成apk文件

    一.搭建搭建环境 1. 安装JDK 和 Android SDK   2. 配置环境变量 D:\android-sdk-windows\tools C:\Program Files\Java\jdk1. ...

  6. HTML基础总结&lt&semi;段落&gt&semi;

    HTML 段落 段落是通过 <p> 标签定义的. 实例 <p>This is a paragraph </p><p>This is another pa ...

  7. 射频识别技术漫谈&lpar;16&rpar;——Mifare UltraLight

    Mifare UltraLight又称为MF0,从UltraLight(超轻的)这个名字就可以看出来,它是一个低成本.小容量的卡片.低成本,是指它是目前市场中价格最低的遵守ISO14443A协议的芯片 ...

  8. EBS动态创建账户组合实现

    目的:使用程序动态创建账户组合.如果账户组合存在的话,返回存在的ID,不存在的话就动态创建账户组合并返回id. 实现步骤: 1. 得到账簿的CHART_OF_ACCOUNTS_ID 2. 得到账户弹性 ...

  9. 获取浏览器版本Asp&period;Net

    public static string GetBrowser() { HttpBrowserCapabilities bc = HttpContext.Current.Request.Browser ...

  10. BZOJ 1502&colon; &lbrack;NOI2005&rsqb;月下柠檬树 &lbrack;辛普森积分 解析几何 圆&rsqb;

    1502: [NOI2005]月下柠檬树 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1070  Solved: 596[Submit][Status] ...