欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring boot與ktor整合的實現(xiàn)方法

 更新時間:2020年09月02日 08:42:55   作者:貓哥不懂技術  
這篇文章主要給大家介紹了關于spring boot與ktor整合的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

背景

在用了一陣子 Ktor 之后,深感基于協(xié)程的方便,但是公司的主要技術棧是 SpringBoot,雖然已經(jīng)整合了 Kotlin,但是如果有 Ktor 加持則會更加的方便。因此作了一番研究后,也完全可以實現(xiàn)這樣的整合了。

建立一個 starter

首先新建一個 Kotlin 項目,在其 build.gradle 內(nèi)加入對 SpringBoot 和 Ktor 的依賴,并同時加入對打為 jar 包的代碼:

dependencies {
  implementation "org.springframework.boot:spring-boot-starter-aop:${springBootVersion}"
  implementation "io.ktor:ktor-jackson:${ktorVersion}"
  compile "io.ktor:ktor-server-netty:${ktorVersion}"
  compile "io.ktor:ktor-html-builder:${ktorVersion}"

  testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
  testCompile "io.ktor:ktor-client-apache:${ktorVersion}"
  testCompile "io.ktor:ktor-server-test-host:${ktorVersion}"
}

jar {
  from {
    configurations.runtime.collect { zipTree(it) }
  }
}

task sourceJar(type: Jar) {
  from sourceSets.main.allSource
  classifier 'sources'
}

對于 SpringBoot 來說,工程內(nèi)的 Configuration,Controller,Module 都是必要的,因此也需要 Ktor 可以符合這些約定俗成的組件。

那么就簡單來實現(xiàn)一下吧,首先實現(xiàn) Controller 的代碼,我們只需要讓 SpringBoot 的 Controller 支持 Ktor 的路由寫法就可以了:

interface KRouter {
  fun Routing.route()
}

@ContextDsl
fun Routing.request(
 path: String, 
 body: PipelineInterceptor<Unit, ApplicationCall>
) = route(path) { handle(body) }

然后實現(xiàn)基礎的 Module:

interface KModule {

  fun Application.defaultRegister(
   useCompress: Boolean = false, 
   redirectHttps: Boolean = false, 
   headers: String = ""
  ) {
    install(ContentNegotiation) { jackson { } }
    install(PartialContent) { maxRangeCount = 10 }
    if (useCompress) {
      install(Compression) {
        gzip { priority = 1.0 }
        deflate {
          priority = 10.0
          minimumSize(1024)
        }
      }
    }
    if (redirectHttps) {
      install(HttpsRedirect) {
        sslPort = URLProtocol.HTTPS.defaultPort
        permanentRedirect = true
      }
    }
    if (headers != "") {
      install(DefaultHeaders) {
        headers.toCookieMap().forEach { (t, u) -> header(t, "$u") }
      }
    }
  }

  @ContextDsl
  fun Application.register()
}

在這個 Module 內(nèi),defaultRegister 是通過讀取 application.yml 內(nèi)的配置的參數(shù)來決定的,register 是用來讓用戶覆蓋,并實現(xiàn)額外的模塊注冊。

最后只需要實現(xiàn) Configuration 就可以了,這里實現(xiàn)讀取 yml 并且調用 defaultRegister 等方法:

/**
 * spring.ktor 配置項
 * @param host 服務器主機名
 * @param port 綁定端口
 * @param compress 是否啟用壓縮
 * @param redirectHttps 是否自動重定向到 https
 * @param headers 默認的請求頭
 */
@ConfigurationProperties(prefix = "spring.ktor")
open class KProperties(
    open var host: String = "0.0.0.0",
    open var port: Int = 8080,
    open var compress: Boolean = false,
    open var redirectHttps: Boolean = false,
    open var headers: String = ""
)

用這個類來映射 yml 內(nèi)的配置,并且在取值后即可實現(xiàn)對模塊,路由等的初始化:

@Configuration
@EnableConfigurationProperties(KProperties::class)
open class KConfiguration {

  @Resource
  private lateinit var properties: KProperties

  @Bean
  @ConditionalOnMissingBean
  open fun engineFactory() = Netty

  @Bean
  @ConditionalOnMissingBean
  open fun applicationEngine(
   engineFactory: ApplicationEngineFactory<ApplicationEngine, out ApplicationEngine.Configuration>, 
   context: ApplicationContext
  ): ApplicationEngine {
    return embeddedServer(engineFactory, host = properties.host, port = properties.port) {
      val modules = context.getBeansOfType(KModule::class.java).values
      val routes = context.getBeansOfType(KRouter::class.java).values
      modules.forEach { it.apply {
        defaultRegister(
          useCompress = properties.compress, 
          redirectHttps = properties.redirectHttps, 
          headers = properties.headers)
        register()
      } }
      routing { routes.forEach { it.apply { route() } } }
    }.start()
  }

  @Bean
  @ConditionalOnMissingBean
  open fun application(
    applicationEngine: ApplicationEngine, 
    context: ApplicationContext
  ): Application = applicationEngine.environment.application
}

好了,一個簡單的 starter 就完成了,最后加入一些配置就可以完成:

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.isyscore.ktor.starter.configuration.KConfiguration

然后加入對配置項的描述:

additional-spring-configuration-metadata.json

{
 "properties": [
  {
   "name": "spring.ktor.port",
   "type": "java.lang.Integer",
   "description": "服務啟動時使用的端口號."
  },
  {
   "name": "spring.ktor.host",
   "type": "java.lang.String",
   "description": "服務的主機IP或域名."
  },
  {
   "name": "spring.ktor.compress",
   "type": "java.lang.Boolean",
   "description": "是否啟用壓縮."
  },
  {
   "name": "spring.ktor.redirectHttps",
   "type": "java.lang.Boolean",
   "description": "是否自動重定向到 https."
  },
  {
   "name": "spring.ktor.headers",
   "type": "java.lang.String",
   "description": "默認的請求頭,以分號隔開."
  }
 ]
}

最后我們只需要將這個 starter 發(fā)布到私有的 nexus 就完成了:

$ gradle publish

使用 starter

新建一個 SpringBoot 項目,并引入 starter:

implementation "com.rarnu:spring-boot-starter-ktor:0.0.1"

此時可以先在 yml 內(nèi)加入配置項:

spring:
 ktor:
  port: 9000
  compress: true
  headers: X-Engine=Ktor

然后來實現(xiàn) Configuration,Controller 和 Module:

TestConfiguration.kt

class TestConfiguration {
  @Bean
  fun engineFactory() = TestEngine
}

TestModule.kt

@Component
class TestModule : KModule {
  override fun Application.register() {
  // TODO: install custom plugins
  }
}

TestController.kt

@Controller
class TestController : KRouter {

  override fun Routing.route() {

    request("/") {
      call.respond(mapOf("code" to "001", "msg" to "操作成功。"))
    }

    get("/hello") {
      call.respondText { "OK" }
    }
  }
}

完成后我們只需要寫一個 Application,并且啟動服務即可:

SpringKtorApplication.kt

@SpringBootApplication
open class SpringKtorApplication

fun main(args: Array<String>) {
  runApplication<SpringKtorApplication>(*args)
}

現(xiàn)在就可以編譯項目并且運行程序了:

$ gradle clean build
$ java -jar test-ktor.jar

總結

現(xiàn)在即可使用 Ktor 的寫法來編寫 SpringBoot 的路由了

可以使用 Ktor 協(xié)程

可以使用各種方便的 Ktor 插件

用上 Ktor 后,代碼不麻煩了,心情也好了,效率更高了 :)

到此這篇關于spring boot與ktor整合的文章就介紹到這了,更多相關spring boot與ktor整合內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java?Chassis3的多種序列化方式支持技術解密

    Java?Chassis3的多種序列化方式支持技術解密

    這篇文章主要為大家介紹了Java?Chassis?3多種序列化方式支持技術解密,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • SpringBoot3集成和使用Jasypt的代碼詳解

    SpringBoot3集成和使用Jasypt的代碼詳解

    隨著信息安全的日益受到重視,加密敏感數(shù)據(jù)在應用程序中變得越來越重要,Jasypt作為一個簡化Java應用程序中數(shù)據(jù)加密的工具,為開發(fā)者提供了一種便捷而靈活的加密解決方案,本文將深入解析Jasypt的工作原理,需要的朋友可以參考下
    2024-01-01
  • java xml轉為json的n種方法

    java xml轉為json的n種方法

    本文給大家分享java xml轉為json的兩種方法,每種方法通過實例代碼給大家介紹的非常詳細,小編感覺第一種方法要比第二種方法好些,具體實現(xiàn)代碼跟隨小編一起看看吧
    2021-08-08
  • SpringBoot中maven項目打成war包部署在linux服務器上的方法

    SpringBoot中maven項目打成war包部署在linux服務器上的方法

    這篇文章主要介紹了SpringBoot中maven項目打成war包部署在linux服務器上的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • JVM調優(yōu)參數(shù)的設置

    JVM調優(yōu)參數(shù)的設置

    Java虛擬機的調優(yōu)是一個復雜而關鍵的任務,可以通過多種參數(shù)來實現(xiàn),本文就來介紹一下JVM調優(yōu)參數(shù)的設置,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • Spring Boot 如何使用Liquibase 進行數(shù)據(jù)庫遷移(操作方法)

    Spring Boot 如何使用Liquibase 進行數(shù)據(jù)庫遷移(操作方法)

    在Spring Boot應用程序中使用Liquibase進行數(shù)據(jù)庫遷移是一種強大的方式來管理數(shù)據(jù)庫模式的變化,本文重點講解如何在Spring Boot應用程序中使用Liquibase進行數(shù)據(jù)庫遷移,從而更好地管理數(shù)據(jù)庫模式的變化,感興趣的朋友跟隨小編一起看看吧
    2023-09-09
  • SpringBoot JSON全局日期格式轉換器實現(xiàn)方式

    SpringBoot JSON全局日期格式轉換器實現(xiàn)方式

    這篇文章主要介紹了SpringBoot JSON全局日期格式轉換器,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • Java求一個分數(shù)數(shù)列的前20項之和的實現(xiàn)代碼

    Java求一個分數(shù)數(shù)列的前20項之和的實現(xiàn)代碼

    這篇文章主要介紹了Java求一個分數(shù)數(shù)列的前20項之和的實現(xiàn)代碼,需要的朋友可以參考下
    2017-02-02
  • Java多線程編程中線程鎖與讀寫鎖的使用示例

    Java多線程編程中線程鎖與讀寫鎖的使用示例

    這篇文章主要介紹了Java多線程編程中線程鎖與讀寫鎖的使用示例,鎖是控制程序多線程并發(fā)的重要手段,需要的朋友可以參考下
    2016-04-04
  • Spring Boot入門(web+freemarker)

    Spring Boot入門(web+freemarker)

    這篇文章主要介紹了Spring Boot入門(web+freemarker)的相關資料,需要的朋友可以參考下
    2017-06-06

最新評論