• Spring Boot]SpringBoot四大神器之Actuator


    论文转载自博客:

    https://blog.csdn.net/Dreamhai/article/details/81077903

    https://bigjar.github.io/2018/08/19/Spring-Boot-Actuator-%E5%81%A5%E5%BA%B7%E6%A3%80%E6%9F%A5%E3%80%81%E5%AE%A1%E8%AE%A1%E3%80%81%E7%BB%9F%E8%AE%A1%E5%92%8C%E7%9B%91%E6%8E%A7/

     

     

    要求spring boot在2.0版本以上

    代码如下

     在之前的系列文章中我们学习了如何进行Spring Boot应用的功能开发,以及如何写单元测试、集成测试等,然而,在实际的软件开发中需要做的不仅如此:还包括对应用程序的监控和管理。

    You build it,You run it, 当我们编写的项目上线后,为了能第一时间知晓该项目是否出现问题,常常对项目进行健康检查及一些指标进行监控。
    Spring Boot-Actuator 就是帮助我们监控我们的Spring Boot 项目的。

    使用
    Spring Boot 最主要的特性就是AutoConfig(自动配置),而对于我们这些使用者来说也就是各种starter,
    Spring Boot-Actuator 也提供了starter,为我们自动配置,在使用上我们只需要添加starter到我们的依赖中,然后启动项目即可。

    整个pom文件依赖如下

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.tuling.cloud</groupId>
      <artifactId>microservice-consumer-order-ribbon-hystrix-fallback</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <!-- 引入spring boot的依赖 -->
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
      </parent>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
       <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.41</version>
    </dependency>
    
    <!--监控中心-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
    
      </dependencies>
    
      <!-- 引入spring cloud的依赖 -->
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR2</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
    
      <!-- 添加spring-boot的maven插件 -->
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    </project>

     spring boot 的版本是2.0版本以上

    Spring Boot Actuator可以帮助你监控和管理Spring Boot应用,比如健康检查、审计、统计和HTTP追踪等。所有的这些特性可以通过JMX或者HTTP endpoints来获得。

    2. 端点(Endpoints)
    执行器端点(endpoints)可用于监控应用及与应用进行交互,Spring Boot包含很多内置的端点,你也可以添加自己的。例如,health端点提供了应用的基本健康信息。
    每个端点都可以启用或禁用。这控制着端点是否被创建,并且它的bean是否存在于应用程序上下文中。要远程访问端点,还必须通过JMX或HTTP进行暴露,大部分应用选择HTTP,端点的ID映射到一个带/actuator前缀的URL。例如,health端点默认映射到/actuator/health。

    注意:
    Spring Boot 2.0的端点基础路径由“/”调整到”/actuator”下,如:/info调整为/actuator/info
    可以通过以下配置改为和旧版本一致:

    management.endpoints.web.base-path=/

     

     

     

     现在我们要暴露所有的端点,配置文件如下

    application.properties

    management.endpoints.web.exposure.include=*
    management.endpoint.health.show-details=always
    通过设置management.endpoints.web.exposure.include为*,我们可以在http://localhost:8080/actuator页面看到如下内容。

     

     

     

     看上面的一个具体的请求为

     

     

     

     

     application.yml

    
    

    #hystrix.command.default.execution.isolation.strategy=Semaphore
    #hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=10
    hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=200009
    hystrix.command.default.circuitBreaker.requestVolumeThreshold=3;
    hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=3000
    hystrix.command.default.circuitBreaker.errorThresholdPercentage=50
    management.security.enabled=false
    management.endpoint.shutdown.enabled=true
    management.endpoint.scheduledtasks.enabled=true
    management.endpoint.scheduledtasks.enabled=true
    management.endpoint.scheduledtasks.enabled=true
    management.endpoint.heapdump.enabled=true
    management.endpoint.threaddump.enabled=true
    management.endpoint.httptrace.enabled=true
    # u52A0u8F7Du6240u6709u7684u7AEFu70B9/u9ED8u8BA4u53EAu52A0u8F7Du4E86 info / health
    management.endpoints.web.exposure.include=*
    management.endpoint.health.show-details=always

    
    

    info.build.artifact=@project.artifactId@
    info.build.name=@project.name@
    info.build.description=@project.description@
    info.build.version=@project.version@

    
    

     例如/info:首先在application.properties文件中添加对应的属性值,符号@包围的属性值来自pom.xml文件中的元素节点。

     

     

     

     



     

     

     

     这里需要注意的是:@RequestMapping(value = "/produces", produces = "application/json"):表示将功能处理方法将生产json格式的数据,此时根据请求头中的Accept进行匹配,如请求头“Accept:application/json”时即可匹配;

    访问"{[/actuator/httptrace],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}","访问httptrace需要请求的头信息是Accept参数是application/vnd.spring-boot.actuator.v2+json或者是application/json;

  • 相关阅读:
    LeetCode 1245. Tree Diameter
    LeetCode 1152. Analyze User Website Visit Pattern
    LeetCode 1223. Dice Roll Simulation
    LeetCode 912. Sort an Array
    LeetCode 993. Cousins in Binary Tree
    LeetCode 1047. Remove All Adjacent Duplicates In String
    LeetCode 390. Elimination Game
    LeetCode 1209. Remove All Adjacent Duplicates in String II
    LeetCode 797. All Paths From Source to Target
    LeetCode 1029. Two City Scheduling
  • 原文地址:https://www.cnblogs.com/kebibuluan/p/11886517.html
Copyright © 2020-2023  润新知