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/

使用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. java编程基础知识及常见例题

    ⒈标识符: 只能包含数字.字母.下划线.$,并且不能以数字开头.语义直观规范 驼峰法则: 如:方法名.变量名采用驼峰法则 帕斯卡命名法: 如: 类.接口.枚举采用帕斯卡命名法包名:网址倒写,com.网 ...

  2. Redis的事务和watch

    redis的事务 严格意义来讲,redis的事务和我们理解的传统数据库(如mysql)的事务是不一样的. redis中的事务定义 Redis中的事务(transaction)是一组命令的集合. 事务同 ...

  3. ELK学习笔记(二)-HelloWorld实例+Kibana介绍

    这次我们通过一个最简单的HelloWolrd来了解一下ELK的使用. 进入logstash的config目录,创建stdin.conf 文件. input{ stdin{ } } output{ st ...

  4. 【Python】 xml解析与生成 xml

    xml *之前用的时候也没想到..其实用BeautifulSoup就可以解析xml啊..因为html只是xml的一种实现方式吧.但是很蛋疼的一点就是,bs不提供获取对象的方法,其find大多获取的都是 ...

  5. JavaScript(第六天)【函数】

    函数是定义一次但却可以调用或执行任意多次的一段JS代码.函数有时会有参数,即函数被调用时指定了值的局部变量.函数常常使用这些参数来计算一个返回值,这个值也成为函数调用表达式的值. 一.函数声明   函 ...

  6. python全栈开发-Day12 三元表达式、函数递归、匿名函数、内置函数

    一. 三元表达式 一 .三元表达式 仅应用于: 1.条件成立返回,一个值 2.条件不成立返回 ,一个值 def max2(x,y): #普通函数定义 if x > y: return x els ...

  7. C语言最后一次作业——总结报告

    1.当初你是如何做出选择计算机专业的决定的?经过一个学期,你的看法改变了么,为什么? 你觉得计算机是你喜欢的领域吗,它是你擅长的领域吗? 为什么? 首先是因为自己想学跟做动画沾边的专业(动画专业因为某 ...

  8. 【Swift】Runtime动态性分析

    Swift是苹果2014年发布的编程开发语言,可与Objective-C共同运行于Mac OS和iOS平台,用于搭建基于苹果平台的应用程序.Swift已经开源,目前最新版本为2.2.我们知道Objec ...

  9. appiun滑动的简单封装

    import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.test ...

  10. 不允许用(a+b)/2这种方式求两个数的均值;如下程序在Linux和32位集成开发环境中运行

    #define MAX(a,b) ((a)>(b)?(a):(b)) #include<stdio.h> int main() { int a = 10; int b = 20; i ...