Springboot into God--the Monitor

Source: Internet
Author: User

    • Spring Boot's Monitor
      • Depend on
      • Configuration
      • Writing monitoring Controller
      • Some of the commonly used built-in endpoint
      • Defining Actuator/info Special Endpoint
      • Actuator/shutdown requires a POST request to access
Spring Boot's Monitor

This feature is used to control spring boot programs and view program information

Depend on
<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>
Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=truespring.datasource.username=rootspring.datasource.password=123spring.datasource.driver-class-name=com.mysql.jdbc.Driverserver.tomcat.uri-encoding=UTF-8# 程序运行端口server.port=8888# 监视程序运行端口management.server.port=8090# 激活所有的内置Endpointsmanagement.endpoints.web.exposure.include=*# 开启shutdown这个endpointmanagement.endpoint.shutdown.enabled=true
Writing monitoring Controller
  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; @Controllerpublic class maincontroller{@ResponseBody @ Requestmapping ("/getendpoints") public String Getallpoint (HttpServletRequest request) {string path = Request.get        ContextPath ();        String host = Request.getservername ();        String Endpointpath = "/actuator";                StringBuilder sb = new StringBuilder ();        Sb.append (" 
Some of the commonly used built-in endpoint
actuator/health         查看程序健康信息actuator/metrics        查看监视标准actuator/beans          列出程序中的Spring BEAN actuator/env            列出程序运行所有信息
Defining Actuator/info Special 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;@Componentpublic 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 requires a POST request to access
Can be used to close the program define the following controller can 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;@ Controllerpublic class Shutdowncontroller {@ResponseBody @RequestMapping (path = "/shutdown") public String Actua        Torshutdown () {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 into God--monitor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.