Springboot-Admin服务监控

Spring-boot-admin 是一个开源组织写的服务监控的项目,git 地址     https://github.com/codecentric/spring-boot-admin

主要功能:

1. 监测服务数量、服务对应的实例数量、以及每个服务上线时长

2. 每个实例的JVM信息、CPU以及进线程等信息

3. 加载的Spring bean 信息、yml 中配置的信息、全局的配置信息、日志级别、session 信息等

4. SpringMVC 全局的映射关系, 也就是 url对应的方法信息以及处理的方法(这个可以理解为查看SpringMVC 保存URL信息的org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry#mappingLookup<RequestMappingInfo, HandlerMethod> 缓存里面的信息,SpringMVC 项

目启动过程中会加载相关的mapping 信息到这个类里面)

1. 搭建一个Spring-boot-admin server 端

1. 新建子项目 cloud-admin-server, 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">
    <parent>
        <artifactId>cloud</artifactId>
        <groupId>cn.qz.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-admin-server</artifactId>

    <dependencies>
        <!--admin server 需要的依赖-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

2. 新增application.yml 文件

spring:
  application:
    name: admin-server

server:
  port: 8801

3. 新增主启动类

package cn.qz.cloud;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AdminServerApplication.class, args);
    }
}

4. 启动后访问8081 端口,查看如下:

 2. admin-client 通过http 注册到admin-server

1. pom 增加如下依赖

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2. yml 增加配置: 主要是增加admin 的url, 配置类在de.codecentric.boot.admin.client.config.ClientProperties

server:
  port: 8081

spring:
  application:
    name: cloud-provider-hystrix-payment
  redis:
    port: 6379
    host: localhost
  session:
    store-type: redis
  boot:
    admin:
      client:
        url: http://localhost:8801

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      #defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
      defaultZone: http://localhost:7001/eureka

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: ALWAYS

3. 启动server, 然后启动client ,到8801 端口即可看到实例信息

3. 从eureka 注册中心直接拿客户端信息

1. 修改cloud-admin-server的pom ,增加如下引用:

        <!--注册中心,从注册中心拿数据-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2. yml 修改

spring:
  application:
    name: admin-server

server:
  port: 8801

eureka:
  client:
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:7001}/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

3. 测试即可,admin-server 会从eureka 拿取服务数据

 4. 查看单个服务信息

    还可以整合k8s等,这个等之后学习完k8s 再来补充。

【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】
原文地址:https://www.cnblogs.com/qlqwjy/p/14777256.html