原文及更多文章請見個人博客:http://heartlifes.com
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