mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 jenkins 示例
This commit is contained in:
@@ -20,9 +20,18 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Actuator 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<!-- 设置构建的 jar 包名 -->
|
||||
<finalName>${artifactId}</finalName>
|
||||
<!-- 使用 spring-boot-maven-plugin 插件打包 -->
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@@ -2,6 +2,11 @@ package cn.iocoder.springboot.lab40.jenkinsdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
@@ -10,4 +15,26 @@ public class Application {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Component
|
||||
public class Listener implements ApplicationListener<ApplicationEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
if (event instanceof ApplicationReadyEvent) {
|
||||
this.sleep(5);
|
||||
// 用于模拟关闭比较慢。
|
||||
} else if (event instanceof ContextClosedEvent) {
|
||||
this.sleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
private void sleep(int seconds) {
|
||||
try {
|
||||
Thread.sleep(seconds * 1000L);
|
||||
} catch (InterruptedException ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
11
lab-41/lab-41-demo01/src/main/resources/application-dev.yaml
Normal file
11
lab-41/lab-41-demo01/src/main/resources/application-dev.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
11
lab-41/lab-41-demo01/src/main/resources/application-pre.yaml
Normal file
11
lab-41/lab-41-demo01/src/main/resources/application-pre.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
11
lab-41/lab-41-demo01/src/main/resources/application-uat.yaml
Normal file
11
lab-41/lab-41-demo01/src/main/resources/application-uat.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
@@ -1,2 +0,0 @@
|
||||
server:
|
||||
port: 8079
|
||||
43
lab-41/lab-41-demo02/pom.xml
Normal file
43
lab-41/lab-41-demo02/pom.xml
Normal file
@@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-41-demo02</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<!-- 实现对 SpringMVC 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Actuator 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<!-- 设置构建的 jar 包名 -->
|
||||
<finalName>${artifactId}</finalName>
|
||||
<!-- 使用 spring-boot-maven-plugin 插件打包 -->
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springboot.lab40.jenkinsdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Demo02Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Demo02Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package cn.iocoder.springboot.lab40.jenkinsdemo.actuate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.context.event.ApplicationFailedEvent;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextClosedEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ServerHealthIndicator extends AbstractHealthIndicator implements ApplicationListener<ApplicationEvent> {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
/**
|
||||
* 是否在服务中
|
||||
*/
|
||||
private volatile boolean inService = false;
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) {
|
||||
if (inService) {
|
||||
builder.up();
|
||||
} else {
|
||||
builder.down();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
if (event instanceof ApplicationReadyEvent) {
|
||||
this.handleApplicationReadyEvent((ApplicationReadyEvent) event);
|
||||
} else if (event instanceof ApplicationFailedEvent) {
|
||||
this.handleApplicationFailedEvent((ApplicationFailedEvent) event);
|
||||
} else if (event instanceof ContextClosedEvent) {
|
||||
this.handleContextClosedEvent((ContextClosedEvent) event);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void handleApplicationReadyEvent(ApplicationReadyEvent event) {
|
||||
this.inService = true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void handleApplicationFailedEvent(ApplicationFailedEvent event) {
|
||||
this.inService = false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void handleContextClosedEvent(ContextClosedEvent event) {
|
||||
// 标记不提供服务
|
||||
this.inService = false;
|
||||
|
||||
// sleep 等待负载均衡完成健康检查
|
||||
for (int i = 0; i < 5; i++) { // TODO 20 需要配置
|
||||
logger.info("[handleContextClosedEvent][优雅关闭,第 {} sleep 等待负载均衡完成健康检查]", i);
|
||||
try {
|
||||
Thread.sleep(1000L);
|
||||
} catch (InterruptedException ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package cn.iocoder.springboot.lab40.jenkinsdemo.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/demo")
|
||||
public class DemoController {
|
||||
|
||||
@GetMapping("/echo")
|
||||
public String echo() {
|
||||
return "echo";
|
||||
}
|
||||
|
||||
}
|
||||
11
lab-41/lab-41-demo02/src/main/resources/application-dev.yaml
Normal file
11
lab-41/lab-41-demo02/src/main/resources/application-dev.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
11
lab-41/lab-41-demo02/src/main/resources/application-pre.yaml
Normal file
11
lab-41/lab-41-demo02/src/main/resources/application-pre.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
11
lab-41/lab-41-demo02/src/main/resources/application-uat.yaml
Normal file
11
lab-41/lab-41-demo02/src/main/resources/application-uat.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
11
lab-41/lab-41-demo02/src/main/resources/application.yaml
Normal file
11
lab-41/lab-41-demo02/src/main/resources/application.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
11
lab-41/lab-41-demo02/target/classes/application-dev.yaml
Normal file
11
lab-41/lab-41-demo02/target/classes/application-dev.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
11
lab-41/lab-41-demo02/target/classes/application-local.yaml
Normal file
11
lab-41/lab-41-demo02/target/classes/application-local.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
11
lab-41/lab-41-demo02/target/classes/application-pre.yaml
Normal file
11
lab-41/lab-41-demo02/target/classes/application-pre.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
11
lab-41/lab-41-demo02/target/classes/application-uat.yaml
Normal file
11
lab-41/lab-41-demo02/target/classes/application-uat.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 8079
|
||||
|
||||
management:
|
||||
server:
|
||||
port: 8078 # 自定义端口,避免 Nginx 暴露出去
|
||||
|
||||
endpoint:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
@@ -13,6 +13,7 @@
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>lab-41-demo01</module>
|
||||
<module>lab-41-demo02</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user