spring boot 整合Vert.x
在后台接口开发中,没有必要引入一些tomcat,或jetty这些web容器,同时提高系统的性能,提高系统的并发性,Vert.x 是以非阻塞IO的思想来实现高性能,非阻塞IO的实现,依据netty为底层基于Netty,大大降低了传统阻塞模型中线程对于操作系统的开销,因此相比较传统的阻塞模型,异步模型能够很大层度的提高系统的并发量。
下表是一个整合的小案例
@Component
public class StaticServer extends AbstractVerticle{
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
//请求路径
router.get("/v1/customer")
//.produces("application/json")
//使用evenloop线程执行
//.handler(this::getCustomerById);
//创建新的线程执行(一般用于执行阻塞调用)
.blockingHandler(this::getById);
router.route().handler(StaticHandler.create());
// 开启监听端口
vertx.createHttpServer().requestHandler(router).listen(8080);
}
/**
* 模拟调用方法
* @param rc
*/
private void getById(RoutingContext rc) {
//获取请求参数
Long id = Long.parseLong(rc.request().getParam("id"));
System.out.println(id);
/*
* 返回请求值
*/
rc.response().
setStatusMessage("ok") // http响应状态描述值
.setStatusCode(200)// http响应状态码
.end("this is ok"); //返回内容
}
}
配置springboot启动类
@SpringBootApplication
public class App {
@Autowired
private StaticServer staticServer;
public static void main(String[] args)
{
try {
// 不让springboot以web启动
new SpringApplicationBuilder(App.class).web(WebApplicationType.NONE).run(args);
} catch (Exception e) {
e.printStackTrace();
}
}
@PostConstruct
public void deployVerticle() {
Vertx.vertx(new VertxOptions()).deployVerticle(staticServer);
}
}
然后浏览器访问http://127.0.0.1:8080/v1/customer?id=11 即可
后台会获取到请求参数id为11