1. 程式人生 > 實用技巧 >SpringBoot專案使用Actuator遠端關閉服務

SpringBoot專案使用Actuator遠端關閉服務

1、在pom.xml檔案引入依賴

<!-- 執行狀態監控actuator依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--  Web 應用安全依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

2、配置檔案配置

server:
  port: 8100  #web服務埠
  servlet:
    context-path: /buzheng

spring:
  main:
    allow-bean-definition-overriding: true  #是否允許使用相同名稱重新註冊不同的bean實現. 預設是允許
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: asdfghjkl
  # 為防止他人改動
  security:
    user:
      name: buzheng
      password: buzheng

### 執行狀態監控Actuator
management:
 server:
   port: 9011 #執行狀態監控埠
 endpoints:
   web:
     exposure:
       include: "*"
     base-path: /
 #配置可遠端使用    Actuator 關閉服務
 endpoint:
   shutdown:  #關閉服務
     enabled: true

3、執行關閉指令

http://localhost:9011/shutdown POST請求