1. 程式人生 > >springboot成神之——監視器

springboot成神之——監視器

pat alt .info 書寫 ret serve source 所有 with

  • Spring Boot 的監視器
    • 依賴
    • 配置
    • 書寫監視控制器
    • 常用的一些內置endpoint
    • 定義actuator/info特殊endpoint
    • actuator/shutdown需要post請求才能訪問

Spring Boot 的監視器

此功能用來控制spring boot程序和查看程序信息

依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.0.4.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

配置

spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

server.tomcat.uri-encoding=UTF-8
# 程序運行端口
server.port=8888
# 監視程序運行端口
management.server.port=8090
# 激活所有的內置Endpoints
management.endpoints.web.exposure.include=*
# 開啟shutdown這個endpoint
management.endpoint.shutdown.enabled=true

書寫監視控制器

package com.springlearn.learn.Controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MainController{

    @ResponseBody
    @RequestMapping("/GetEndPoints")
    public String GetAllPoint(HttpServletRequest request){
        String path = request.getContextPath();
        String host = request.getServerName();
        String endPointPath = "/actuator";
        StringBuilder sb = new StringBuilder();
        
        sb.append("<h2>Sprig Boot Actuator</h2>");
        sb.append("<ul>");
        String url = "http://" + host + ":8090" + path + endPointPath;
        sb.append("<li><a href=‘" + url + "‘>" + url + "</a></li>");
        sb.append("</ul>");
        return sb.toString();
    }
}

// 訪問 http://localhost:8888/GetEndPoints

常用的一些內置endpoint

actuator/health         查看程序健康信息

actuator/metrics        查看監視標準

actuator/beans          列出程序中的Spring BEAN 

actuator/env            列出程序運行所有信息

定義actuator/info特殊endpoint

actuator/info可以自定義一些信息

書寫如下代碼即可訪問

package com.springlearn.learn.selfactuator;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;

@Component
public class BuildInfoActuator implements InfoContributor{

    @Override
    public void contribute(Info.Builder builder) {
        Map<String,String> data= new HashMap<String,String>();
        data.put("build.version", "1.0.0");
        builder.withDetail("buildInfo", data);
    }
}

actuator/shutdown需要post請求才能訪問

可以用來關閉程序

定義如下控制器即可

package com.springlearn.learn.Controller;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
public class ShutDownController {

    @ResponseBody
    @RequestMapping(path = "/shutdown")
    public String actuatorShutdown(){
        String url = "http://localhost:8090/actuator/shutdown";
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
        headers.setContentType(MediaType.APPLICATION_JSON);

        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<String> requestBody = new HttpEntity<>("", headers);
        String e = restTemplate.postForObject(url, requestBody, String.class);

        return "Result: " + e;
    }
}

springboot成神之——監視器