欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring boot监控Actuator-Admin实现过程详解

程序员文章站 2022-04-10 09:13:57
actuator是监控管理,不过没有可视化,这里就引入了admin-ui来解决spring boot admin 分为client端和server端client端是客户端server端是spring-...

actuator是监控管理,不过没有可视化,这里就引入了admin-ui来解决spring boot admin 分为client端和server端client端是客户端server端是spring-boot-admin来监控client的.先来一个client客户端代码;

<?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>
 
 <artifactid>spring-boot-demo-admin-client</artifactid>
 <version>1.0.0-snapshot</version>
 <packaging>jar</packaging>
 
 <name>spring-boot-demo-admin-client</name>
 <description>demo project for spring boot</description>
 
 <parent>
  <groupid>com.xkcoding</groupid>
  <artifactid>spring-boot-demo-admin</artifactid>
  <version>1.0.0-snapshot</version>
 </parent>
 
 <properties>
  <project.build.sourceencoding>utf-8</project.build.sourceencoding>
  <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
  <java.version>1.8</java.version>
 </properties>
 
 <dependencies>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-web</artifactid>
  </dependency>
 
  <dependency>
   <groupid>de.codecentric</groupid>
   <artifactid>spring-boot-admin-starter-client</artifactid>
  </dependency>
 
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-security</artifactid>
  </dependency>
 
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-test</artifactid>
   <scope>test</scope>
  </dependency>
 </dependencies>
 
 <build>
  <finalname>spring-boot-demo-admin-client</finalname>
  <plugins>
   <plugin>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-maven-plugin</artifactid>
   </plugin>
  </plugins>
 </build>
 
</project>

application.yml文件,注意这里的url一定跑配置到server的路径,不然找不到,有点类型erkura的注册中心地址.

server:
 port: 8081
 servlet:
  context-path: /demo
spring:
 application:
  # spring boot admin展示的客户端项目名,不设置,会使用自动生成的随机id
  name: spring-boot-demo-admin-client
 boot:
  admin:
   client:
    # spring boot admin 服务端地址
    url: "http://localhost:8080/"
    instance:
     metadata:
      # 客户端端点信息的安全认证信息
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}
 security:
  user:
   name: xkcoding
   password: 123456
management:
 endpoint:
  health:
   # 端点健康情况,默认值"never",设置为"always"可以显示硬盘使用情况和线程情况
   show-details: always
 endpoints:
  web:
   exposure:
    # 设置端点暴露的哪些内容,默认["health","info"],设置"*"代表暴露所有可访问的端点
    include: "*"

java文件就随便暴露个接口就行

package com.xkcoding.admin.client.controller;
 
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.restcontroller;
 
/**
 * <p>
 * 首页
 * </p>
 *
 * @package: com.xkcoding.admin.client.controller
 * @description: 首页
 * @author: yangkai.shen
 * @date: created in 2018/10/8 2:15 pm
 * @copyright: copyright (c) 2018
 * @version: v1.0
 * @modified: yangkai.shen
 */
@restcontroller
public class indexcontroller {
  @getmapping(value = {"", "/"})
  public string index() {
   return "this is a spring boot admin client.";
  }
}

接下来是server服务端,作为监控平台使用pom.xml

<?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>
 
 <artifactid>spring-boot-demo-admin-server</artifactid>
 <version>1.0.0-snapshot</version>
 <packaging>jar</packaging>
 
 <name>spring-boot-demo-admin-server</name>
 <description>demo project for spring boot</description>
 
 <parent>
  <groupid>com.xkcoding</groupid>
  <artifactid>spring-boot-demo-admin</artifactid>
  <version>1.0.0-snapshot</version>
 </parent>
 
 <properties>
  <project.build.sourceencoding>utf-8</project.build.sourceencoding>
  <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
  <java.version>1.8</java.version>
 </properties>
 
 <dependencies>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-web</artifactid>
  </dependency>
 
  <dependency>
   <groupid>de.codecentric</groupid>
   <artifactid>spring-boot-admin-starter-server</artifactid>
  </dependency>
 
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-test</artifactid>
   <scope>test</scope>
  </dependency>
 </dependencies>
 
 <build>
  <finalname>spring-boot-demo-admin-server</finalname>
  <plugins>
   <plugin>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-maven-plugin</artifactid>
   </plugin>
  </plugins>
 </build>
 
</project>

application.yml

server:
 port: 8080 

运行服务端地址,发现已经检测到了注册过的客户端

Spring boot监控Actuator-Admin实现过程详解

可以看到客户端的信息

Spring boot监控Actuator-Admin实现过程详解

http追踪可以看到请求

Spring boot监控Actuator-Admin实现过程详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。