springboot成神之——监视器

Posted 叶家伟的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot成神之——监视器相关的知识,希望对你有一定的参考价值。

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成神之——监视器的主要内容,如果未能解决你的问题,请参考以下文章

springboot成神之——RestTemplate访问Rest

springboot成神之——websocket发送和请求消息

java成神之——enum枚举操作

java成神之——文件IO

java成神之——HttpURLConnection访问api

java成神之——安全和密码