Spring Boot开发 之 热部署

本文的示例代码参考HotDeploy

目录

  • 开发环境

    • 初始项目

    • 配置Idea

    • 配置Gradle

  • 生产环境

    • Fully-Executable

    • Service

    • Customizing

开发环境

初始项目

spring init -a demo -b 2.0.6.RELEASE --build gradle -dweb -l kotlin HotDeploy

cd HotDeploy

关于更多springboot命令行工具使用 可以参考Kotlin起步

vim src/main/kotlin/com/example/demo/DemoController.kt
package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class DemoController {
    @GetMapping("/demo")
    fun demo(): String {
        return "Version 1"
    }
}
  • 测试
./gradlew bootrun

curl localhost:8080/demo
# Version 1

配置Idea

springboot-hotdeploy-01.png
Spring Boot开发 之 热部署_第1张图片
springboot-hotdeploy-02.png

详细配置可以参考Intellij IDEA Java classes not auto compiling on save

配置Gradle

vim build.gradle
dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('com.fasterxml.jackson.module:jackson-module-kotlin')
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    runtimeOnly('org.springframework.boot:spring-boot-devtools')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}
  • 使用Idea运行项目
curl localhost:8080/demo
# Version 1
vim src/main/kotlin/com/example/demo/DemoController.kt
package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class DemoController {
    @GetMapping("/demo")
    fun demo(): String {
        return "Version 2"
    }
}
  • 测试
curl localhost:8080/demo
# Version 2

生产环境

Fully Executable

./gradlew build

java -jar build/libs/demo-0.0.1-SNAPSHOT.jar

curl localhost:8080/demo
# Version 2
vim build.gradle
bootJar {
    launchScript()
}
./gradlew build

./build/libs/demo-0.0.1-SNAPSHOT.jar

curl localhost:8080/demo
# Version 2

关于Gradle的配置可以参考64. Installing Spring Boot Applications和4.4.4. Making an archive fully executable

Service

echo "server.port=5858" >> application.properties
vim demo.service
[Unit]
Description=demo
After=syslog.target

[Service]
User=dev
ExecStart=/home/dev/temp/demo-0.0.1-SNAPSHOT.jar --spring.config.location=/home/dev/temp/application.properties
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target
sudo cp demo.service /etc/systemd/system/

# flag the application to start automatically on system boot
systemctl enable fourier.service
  • 测试
sudo service demo start

curl localhost:5858/demo
# Version 2
  • 关于Service的配置可以参考64.1 Supported Operating Systems

Customizing

vim demo.conf
JAVA_OPTS='-Xms512m -Xmx512m -XX:MaxPermSize=256m -XX:ReservedCodeCacheSize=256m -Dfile.encoding=UTF-8'
RUN_ARGS=--spring.config.location=/home/dev/temp/application.properties
vim demo.service
[Unit]
Description=demo
After=syslog.target

[Service]
User=dev
ExecStart=/home/dev/temp/demo-0.0.1-SNAPSHOT.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target
  • 测试
sudo service demo restart

curl localhost:5858/demo
# Version 2
  • 关于配置Service启动参数可以参考64.2.3 Customizing the Startup Script

参考

  • spring-projects/spring-loaded

  • 20. Developer Tools Part III. Using Spring Boot

  • Spring Boot — Hot Swapping a Gradle project in IntelliJ with Kotlin support

  • SpringBoot: can't create fully executable jar with 2.0.0-M3

  • Part VI. Deploying Spring Boot Applications

你可能感兴趣的:(Spring Boot开发 之 热部署)