vert.x笔记:4.vert.x中调用spring服务

时间:2022-03-16 18:02:04

evenbus事件总线介绍:

在介绍怎么在vert.x中集成spring服务前,我们要先简单介绍一下什么是vert.x的事件总线。
eventbus是vert.x的神经总线,每个vert.x实例维护了一个事件总线。简单来说,vert.x有以下几个概念

寻址:

vert.x将事件消息,通过地址发送到后端的处理程序上。一个地址就是一个全局唯一的字符串。

处理程序:

后端的处理程序,通过地址,将自己注册到事件总线上,并告诉事件总线,我是这个地址的处理程序。

发布/订阅模式:

消息被发布到一个地址,后台所有注册过这个地址的处理程序接收消息并进行处理。

修改pom,加入依赖

在pom.xml中加入以下配置和依赖包:

<properties>
<spring.version>4.1.7.RELEASE</spring.version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>

vert.x集成spring:

创建一个spring service

很简单的服务,输出一个hello spring字符串。

package com.heartlifes.vertx.demo.hello;

import org.springframework.stereotype.Component;

@Component(value = "springService")
public class SpringService {

public String getHello() {
return "hello spring";
}
}
创建SpringVerticle

springVerticle作为事件总线中的后台处理程序,接收事件总线消息,并调用springService完成服务处理。

package com.heartlifes.vertx.demo.hello;

import io.vertx.core.AbstractVerticle;

import org.springframework.context.ApplicationContext;

public class SpringVerticle extends AbstractVerticle {

private SpringService service;

public static final String GET_HELLO_MSG_SERVICE_ADDRESS = "get_hello_msg_service";

public SpringVerticle(ApplicationContext ctx) {
// 从spring上下文获取service
this.service = (SpringService) ctx.getBean("springService");
}

@Override
public void start() throws Exception {
// 唤起事件总线,注册一个事件处理者,或者直译叫事件消费者
vertx.eventBus()
.<String> consumer(GET_HELLO_MSG_SERVICE_ADDRESS)
.handler(msg -> {
// 获取事件内容后,调用service服务
System.out.println("bus msg body is:" + msg.body());
String helloMsg = service.getHello();
System.out.println("msg from hello service is: "
+ helloMsg);
// 将service返回的字符串,回应给消息返回体
msg.reply(helloMsg);
});
}

}
创建ServerVerticle

serverVerticle负责接收前端http请求,并将消息发布到事件总线上,等待后台处理程序处理完该事件后,返回事件处理结果。

package com.heartlifes.vertx.demo.hello;

import io.vertx.core.AbstractVerticle;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;

/**
* 基本代码注释,请参见vert.x笔记:3.使用vert.x发布restful接口
*
* @author john
*
*/

public class ServerVerticle extends AbstractVerticle {

@Override
public void start() throws Exception {
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
router.route("/spring/hello").handler(
// 唤起vert.x的事件总线,并发送一个简单消息
ctx -> vertx.eventBus().<String> send(
SpringVerticle.GET_HELLO_MSG_SERVICE_ADDRESS,// 消息地址
"event bus calls spring service",// 消息内容
result -> {// 异步结果处理
if (result.succeeded()) {
// 成功的话,返回处理结果给前台,这里的处理结果就是service返回的一段字符串
ctx.response()
.putHeader("content-type",
"application/json")
.end(result.result().body());
} else {
ctx.response().setStatusCode(400)
.end(result.cause().toString());
}
}));
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
}
模块部署

整个demo的启动类,负责启动spring容器,部署上面的两个模块,分别是spring模块和服务模块。

package com.heartlifes.vertx.demo.hello;

import io.vertx.core.Vertx;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringMain {

public static void main(String[] args) {
// 注解方式配置,不需要配置文件
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
// 扫描哪些包内的注解
ctx.scan("com.heartlifes.vertx.demo.hello");
ctx.refresh();
Vertx vertx = Vertx.vertx();
// 部署spring模块
vertx.deployVerticle(new SpringVerticle(ctx));
// 部署服务器模块
vertx.deployVerticle(new ServerVerticle());
}

}

http://localhost:8080/spring/hello,界面输出hello spring。
可以看到,使用事件总线后,可以将模块间的耦合度降到最低,仅仅通过事件的发布和订阅,就可以将原来揉成一块的显示服务调用,变成y