使用 Kotlin 将 Vertx 和 Springboot 整合

server/2025/2/6 6:19:36/

本篇文章目的是将 Springboot 和 Vertx 进行简单整合。整合目的仅仅是为了整活,因为两个不同的东西整合在一起提升的性能并没有只使用 Vertx 性能高,因此追求高性能的话这是在我来说不推荐。而且他们不仅没有提高很多性能甚至增加了学习成本

一、整合流程

首先呢目标是将Vertx 最终整合成和使用Springboot web 一样简单的 httpserver。
步骤:

  1. 获取Springboot 所有的Bean
  2. 注册路由: 检查Bean 中是否是存在实现了 Router 的方法,并交给 router
  3. 开启服务器,等待请求

二、扫描 Bean

最终目标呢,是实现和使用Springboot 一样简便,所以就需要注解来对需要的方法进行标注

最终效果预览

kotlin">@RouterController
class HelloRouter(val test :PlayerUnlockTechService
) {/*** 注册路由* 正则路由以 ^ 开始** 方法参数可以是 routingContext 或者 router 或者 routingContext 内的任何东西。以及其他的任何东西,或者 bean**/@Rout("/hello")fun hello(response: HttpServerResponse, request: HttpServerRequest) {request.bodyHandler {response.end(test.getPlayerUnlockTechsByBuilding("BD12DC34624208045CCA1ECE32071F20").toString())}}
  1. 创建注解

主要注解有:

  • RouterController 标注类中有 Router 需要的路由实现
  • Rout 标注方法是个路由实现
  • AutoClose 标注方法执行完成后自动关闭连接
kotlin">/**** @author : zimo* @date : 2025/01/03*/
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
@Component
annotation class RouterController/**** @author : zimo* @date : 2025/01/03*/
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class Router(val path: String,val method: String = ""
)@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class Rout(val path: String,val method: String = ""
)@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class RouterGet(val path: String,val method: String = "GET"
)@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class RouterPost(val path: String,val method: String = "POST"
)@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class RouterPut(val path: String,val method: String = "PUT"
)@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class RouterDelete(val path: String,val method: String = "DELETE"
)@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class RouterPatch(val path: String,val method: String = "PATCH"
)@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class RouterHead(val path: String,val method: String = "HEAD"
)@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class RouterOptions(val path: String,val method: String = "OPTIONS"
)@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class AutoClose
  1. 获取 Beans
    在当前类中注入 applicationContext 并通过 applicationContext.beanDefinitionNames 获取所有的 Bean
kotlin">@Component
class RouterProcessor(val applicationContext: ApplicationContext) {/*** 初始化完成后的 bean 列表*/private val initializedBeanInstances by lazy {applicationContext.beanDefinitionNames.filter {it != this.javaClass.simpleName && it != Connection::class.simpleName}.mapNotNull {applicationContext.getBean(it)}}
}
  1. 检测出所有的Router 方法
    检测出是否被标注为了一个 Router 方法,并注册到 router
kotlin">for (method in clazz.methods) {if (method.isAnnotationPresent(io.github.zimoyin.ra3.annotations.Router::class.java)) {val path = method.getAnnotation(io.github.zimoyin.ra3.annotations.Router::class.java).pathrouter.let {if (path.startsWith("^")) it.routeWithRegex(path.replaceFirst("^", ""))else it.route(path)// order 可以放到注解里,这样可以动态设置了}.order(0).handler {// 执行方法的封装invoke(method, bean, it)}return}// ... 其他注解处理
}

三、执行Router方法

这里只有两个重点,一个是自动关闭,一个是执行方法传入的参数实例

  • 自动关闭,如果方法上存在 AutoClose 注解就在执行方法结束后尝试关闭连接
  • 方法参数,从Bean、Context 中获取。如果没有则通过上下文创建 Bean
kotlin">fun invoke(method: Method, bean: Any, routingContext: RoutingContext) {val args = arrayListOf<Any>()val isHasAutoClose = method.isAnnotationPresent(AutoClose::class.java)// 获取方法需要的参数method.parameters.forEach {val bean0 = kotlin.runCatching { applicationContext.getBean(it.name, it.type) }.getOrNull()?: kotlin.runCatching { applicationContext.getBean(it.type) }.getOrNull()if (bean0 != null) {args.add(bean0)} else {args.add(createParameter(it, routingContext))}}//执行方法try {routingContext.request().paramsCharset = "UTF-8"val result = method.invoke(bean, *args.toTypedArray())kotlin.runCatching {// 自动关闭,如果方法上存在 `AutoClose` 注解就在执行方法结束后尝试关闭连接// 获取方法的返回值,并以方法的返回值作为自动关闭的参数if (isHasAutoClose) {val response = routingContext.response()response.putHeader("content-type", "application/json")if (method.returnType == Unit::class.java) {response.end()}if (result == null) {response.end()}if (result is String) {response.end(result)} else if (result is Number || result is Comparable<*>) {response.end(result.toString())} else {kotlin.runCatching {response.end(result.toJsonObject().toString())}.onFailure {response.end()logger.debug("自动关闭连接失败", it)}}}}} catch (e: InvocationTargetException) {kotlin.runCatching { routingContext.response().end("Server Error!!!!") }logger.error("路由执行失败, $method 方法内部存在错误逻辑导致方法执行失败", e)} catch (e: Exception) {kotlin.runCatching { routingContext.response().end("Server Error!!!!") }logger.error("路由执行失败", e)}}

获取 routingContext 中的参数,或者创建一个参数

kotlin">private fun createParameter(value: Parameter, routingContext: RoutingContext): Any {val name = value.nameval type = value.typewhen (name) {"res", "response", "resp" -> return routingContext.response()"req", "request", "requ" -> return routingContext.request()"body", "reqBody", "requestBody" -> return routingContext.body()"headers", "header", "reqHeader", "requestHeader", "reqHeaders", "requestHeaders" -> return routingContext.request().headers()"query", "reqQuery", "requestQuery", "reqQueries", "requestQueries" -> return routingContext.queryParams()"data", "reqData", "requestData" -> return routingContext.data()"params", "reqParams", "requestParams" -> return routingContext.pathParams()"cookie", "reqCookie", "requestCookie" -> return routingContext.cookieMap()"session", "reqSession", "requestSession" -> return routingContext.session()"user", "reqUser", "requestUser" -> return routingContext.user()"bodyAsString", "reqBodyAsString", "requestBodyAsString" -> return routingContext.bodyAsString"bodyAsJson", "reqBodyAsJson", "requestBodyAsJson" -> return routingContext.bodyAsJson"bodyAsBuffer", "reqBodyAsBuffer", "requestBodyAsBuffer" -> return routingContext.body().buffer()"routingContext", "context", "routerContext", "routContext" -> return routingContext"rout", "router" -> return routingContext.currentRoute()"vertx", "vertxContext" -> return routingContext.vertx()"responseHeaders", "responseHeader" -> return routingContext.response().headers()"uri" -> return routingContext.request().uri()"absoluteURI" -> return routingContext.request().absoluteURI()"authority" -> return routingContext.request().authority()"isSSL", "ssl", "isSsl", "isSSl", "isssl", "SSL", "Ssl" -> return routingContext.request().isSSL}// 如果都不是以上的参数则创建一个kotlin.runCatching {applicationContext.autowireCapableBeanFactory.createBean(type)}.onSuccess {return it}throw IllegalArgumentException("Unable to parse parameters:$name")}

四、全部代码

通过 @EventListener(ApplicationReadyEvent::class) 注解来确保,该初始化方法可以在Springboot 启动完成后执行

注意: 需要提前将 Router 注册到Springboot

kotlin">@Component
class RouterProcessor(val applicationContext: ApplicationContext) {private lateinit var router: io.vertx.ext.web.Routerprivate val logger = LoggerFactory.getLogger(RouterProcessor::class.java)@EventListener(ApplicationReadyEvent::class)fun init(event: ApplicationReadyEvent) {kotlin.runCatching {router = applicationContext.getBeanByName("router")for (bean in initializedBeanInstances) {registerBean(bean)}}.onFailure {logger.error(" Vertx WebRouter 初始化失败: ${it.message}", it)}}/*** 初始化完成后的 bean 列表*/private val initializedBeanInstances by lazy {applicationContext.beanDefinitionNames.filter {it != this.javaClass.simpleName &&it != Connection::class.simpleName}.mapNotNull {applicationContext.getBean(it)}}fun registerBean(bean: Any) {val clazz = bean.javaClassfor (method in clazz.methods) {runCatch {registerMethod(method, bean)}}}fun registerMethod(method: Method, bean: Any) {if (method.isAnnotationPresent(io.github.zimoyin.ra3.annotations.Router::class.java)) {val path = method.getAnnotation(io.github.zimoyin.ra3.annotations.Router::class.java).pathrouter.let {if (path.startsWith("^")) it.routeWithRegex(path.replaceFirst("^", ""))else it.route(path)}.order(0).handler {invoke(method, bean, it)}return}if (method.isAnnotationPresent(Rout::class.java)) {val path = method.getAnnotation(Rout::class.java).pathrouter.let {if (path.startsWith("^")) it.routeWithRegex(path.replaceFirst("^", ""))else it.route(path)}.order(0).handler {invoke(method, bean, it)}return}if (method.isAnnotationPresent(RouterPost::class.java)) {val path = method.getAnnotation(RouterPost::class.java).pathrouter.let {if (path.startsWith("^")) it.postWithRegex(path.replaceFirst("^", ""))else it.post(path)}.order(0).handler {invoke(method, bean, it)}return}if (method.isAnnotationPresent(RouterGet::class.java)) {val path = method.getAnnotation(RouterGet::class.java).pathrouter.let {if (path.startsWith("^")) it.getWithRegex(path.replaceFirst("^", ""))else it.get(path)}.order(0).handler {invoke(method, bean, it)}return}if (method.isAnnotationPresent(RouterPut::class.java)) {val path = method.getAnnotation(RouterPut::class.java).pathrouter.let {if (path.startsWith("^")) it.putWithRegex(path.replaceFirst("^", ""))else it.put(path)}.order(0).handler {invoke(method, bean, it)}return}if (method.isAnnotationPresent(RouterPatch::class.java)) {val path = method.getAnnotation(RouterPatch::class.java).pathrouter.let {if (path.startsWith("^")) it.patchWithRegex(path.replaceFirst("^", ""))else it.patch(path)}.order(0).handler {invoke(method, bean, it)}return}if (method.isAnnotationPresent(RouterPatch::class.java)) {val path = method.getAnnotation(RouterDelete::class.java).pathrouter.let {if (path.startsWith("^")) it.deleteWithRegex(path.replaceFirst("^", ""))else it.delete(path)}.order(0).handler {invoke(method, bean, it)}return}if (method.isAnnotationPresent(RouterHead::class.java)) {val path = method.getAnnotation(RouterHead::class.java).pathrouter.let {if (path.startsWith("^")) it.headWithRegex(path.replaceFirst("^", ""))else it.head(path)}.order(0).handler {invoke(method, bean, it)}return}if (method.isAnnotationPresent(RouterOptions::class.java)) {val path = method.getAnnotation(RouterOptions::class.java).pathrouter.let {if (path.startsWith("^")) it.optionsWithRegex(path.replaceFirst("^", ""))else it.options(path)}.order(0).handler {invoke(method, bean, it)}return}}fun invoke(method: Method, bean: Any, routingContext: RoutingContext) {val args = arrayListOf<Any>()val isHasAutoClose = method.isAnnotationPresent(AutoClose::class.java)method.parameters.forEach {val bean0 = kotlin.runCatching { applicationContext.getBean(it.name, it.type) }.getOrNull()?: kotlin.runCatching { applicationContext.getBean(it.type) }.getOrNull()if (bean0 != null) {args.add(bean0)} else {args.add(createParameter(it, routingContext))}}//执行方法try {routingContext.request().paramsCharset = "UTF-8"val result = method.invoke(bean, *args.toTypedArray())kotlin.runCatching {if (isHasAutoClose) {val response = routingContext.response()response.putHeader("content-type", "application/json")if (method.returnType == Unit::class.java) {response.end()}if (result == null) {response.end()}if (result is String) {response.end(result)} else if (result is Number || result is Comparable<*>) {response.end(result.toString())} else {kotlin.runCatching {response.end(result.toJsonObject().toString())}.onFailure {response.end()logger.debug("自动关闭连接失败", it)}}}}} catch (e: InvocationTargetException) {kotlin.runCatching { routingContext.response().end("Server Error!!!!") }logger.error("路由执行失败, $method 方法内部存在错误逻辑导致方法执行失败", e)} catch (e: Exception) {kotlin.runCatching { routingContext.response().end("Server Error!!!!") }logger.error("路由执行失败", e)}}private fun createParameter(value: Parameter, routingContext: RoutingContext): Any {val name = value.nameval type = value.typewhen (name) {"res", "response", "resp" -> return routingContext.response()"req", "request", "requ" -> return routingContext.request()"body", "reqBody", "requestBody" -> return routingContext.body()"headers", "header", "reqHeader", "requestHeader", "reqHeaders", "requestHeaders" -> return routingContext.request().headers()"query", "reqQuery", "requestQuery", "reqQueries", "requestQueries" -> return routingContext.queryParams()"data", "reqData", "requestData" -> return routingContext.data()"params", "reqParams", "requestParams" -> return routingContext.pathParams()"cookie", "reqCookie", "requestCookie" -> return routingContext.cookieMap()"session", "reqSession", "requestSession" -> return routingContext.session()"user", "reqUser", "requestUser" -> return routingContext.user()"bodyAsString", "reqBodyAsString", "requestBodyAsString" -> return routingContext.bodyAsString"bodyAsJson", "reqBodyAsJson", "requestBodyAsJson" -> return routingContext.bodyAsJson"bodyAsBuffer", "reqBodyAsBuffer", "requestBodyAsBuffer" -> return routingContext.body().buffer()"routingContext", "context", "routerContext", "routContext" -> return routingContext"rout", "router" -> return routingContext.currentRoute()"vertx", "vertxContext" -> return routingContext.vertx()"responseHeaders", "responseHeader" -> return routingContext.response().headers()"uri" -> return routingContext.request().uri()"absoluteURI" -> return routingContext.request().absoluteURI()"authority" -> return routingContext.request().authority()"isSSL", "ssl", "isSsl", "isSSl", "isssl", "SSL", "Ssl" -> return routingContext.request().isSSL}kotlin.runCatching {applicationContext.autowireCapableBeanFactory.createBean(type)}.onSuccess {return it}throw IllegalArgumentException("Unable to parse parameters:$name")}fun <T : Any> runCatch(block: () -> T): T? {try {return block()} catch (e: Exception) {logger.error("路由捕获到异常", e)}return null}
}

使用示例

kotlin">/**** @author : zimo* @date : 2025/01/04*/
@RouterController
class HelloRouter(val test :PlayerUnlockTechService
) {/*** 注册路由* 正则路由以 ^ 开始* 请求处理方法: 所有* 方法参数可以是 routingContext 或者 router 或者 routingContext 内的任何东西。以及其他的任何东西,或者 bean**/@Rout("/hello")
//    @RouterGetfun hello(response: HttpServerResponse, request: HttpServerRequest) {request.bodyHandler {response.end(test.getPlayerUnlockTechsByBuilding("BD12DC34624208045CCA1ECE32071F20").toString())}}/*** 注册路由(并自动关闭,将返回值发送会前端)* 正则路由以 ^ 开始* 请求处理方法: GET* 方法参数可以是 routingContext 或者 router 或者 routingContext 内的任何东西。以及其他的任何东西,或者 bean**/@RouterGet("/test/send_message/:message")@AutoClosefun send(response: HttpServerResponse, request: HttpServerRequest):String {return "你好"}}
}

Main 方法

kotlin">/**** @author : zimo* @date : 2025/01/03*/
@SpringBootApplication
@EnableCaching
@MapperScan(basePackages = ["io.github.zimoyin.ra3.mapper"])
class ApplicationStart2(val vertx: Vertx,val router: Router
) {companion object {@JvmStaticfun main(args: Array<String>) {runApplication<ApplicationStart2>(*args)}}@PostConstructfun onStartVertxWeb() {vertx.createHttpServer().requestHandler(router).listen(8090).onSuccess {println("启动成功")}}
}@Configuration
class VertxConfig {@Bean("vertx")fun vertx(): Vertx {return Vertx.vertx()}@Bean("router")fun router(vertx: Vertx): Router? {return Router.router(vertx)}
}

http://www.ppmy.cn/server/165340.html

相关文章

搭建集成开发环境PyCharm

1.下载安装Python&#xff08;建议下载并安装3.9.x&#xff09; https://www.python.org/downloads/windows/ 要注意勾选“Add Python 3.9 to PATH”复选框&#xff0c;表示将Python的路径增加到环境变量中 2.安装集成开发环境Pycharm http://www.jetbrains.com/pycharm/…

p5r预告信生成器API

p5r预告信生成器API 本人将js生成的p5r预告信使用go语言进行了重写和部署&#xff0c;并开放了其api&#xff0c;可以直接通过get方法获取预告信的png。 快速开始 http://api.viogami.tech/p5cc/:text eg: http://api.viogami.tech/p5cc/persona5 感谢p5r风格字体的制作者和…

机器人抓取与操作概述(深蓝)——1

工业机器人&#xff1a;① “臂”的形态 ② “手”的形态 ③ 视觉&#xff0c;力和触觉 1 机器人的不同形态 “臂”的形态 “手”的形态 2 常见的操作任务 操作&#xff1a;插入、推和滑 抓取&#xff1a;两指&#xff08;平行夹爪&#xff09;抓取、灵巧手抓取 落地-产…

Hive:窗口函数(1)

窗口函数 窗口函数OVER()用于定义一个窗口&#xff0c;该窗口指定了函数应用的数据范围 对窗口数据进行分区 partition by 必须和over () 一起使用, distribute by经常和sort by 一起使用,可以不和over() 一起使用.DISTRIBUTE BY决定了数据如何分布到不同的Reducer上&#xf…

设计模式学习(二)

结构型 适配器模式 定义 它允许将一个类的接口转换成客户端期望的另一个接口。适配器模式通常用于使不兼容的接口能够一起工作。 适配器模式的角色 目标接口&#xff08;Target&#xff09;&#xff1a;客户端期望的接口。适配者&#xff08;Adaptee&#xff09;&#xff…

UE Bridge混合材质工具

打开虚幻内置Bridge 随便点个材质点右下角图标 就能打开材质混合工具 可以用来做顶点绘制

Git进阶之旅:tag 标签 IDEA 整合 Git

第一章&#xff1a;tag 标签远程管理 git 标签 tag 管理&#xff1a; 标签有两种&#xff1a; 轻量级标签(lightweight)带有附注标签(annotated) git tag 标签名&#xff1a;创建一个标签git tag 标签名 -m 附注内容 &#xff1a;创建一个附注标签git tag -d 标签名…

CSS 溢出内容处理:从基础到实战

CSS 溢出内容处理&#xff1a;从基础到实战 1. 什么是溢出&#xff1f;示例代码&#xff1a;默认溢出行为 2. 使用 overflow 属性控制溢出2.1 使用 overflow: hidden 裁剪内容示例代码&#xff1a;裁剪溢出内容 2.2 使用 overflow: scroll 显示滚动条示例代码&#xff1a;显示滚…