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;
    }
}

转载于:https://www.cnblogs.com/ye-hcj/p/9614109.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值