前言:Spring Boot Admin是属于springboo应用的,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。
SpringBootAdmin是一个用于管理和监控SpringBoot微服务的社区项目,可以使用客户端注册或者Eureka服务发现向服务端提供监控信息,例如服务健康检查信息、CPU、内存、操作系统信息等。
话不多说,先上两张图看看:
1,下面有三个应用的监控信息被检测.
2,点开consumer这个项目,检测信息如下图所示:
代码如下:
1.pom.xml,这里涉及到父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>
<parent>
<groupId>com.hcmony</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>admin</artifactId>
<packaging>jar</packaging>
<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>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
父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>
<groupId>com.hcmony</groupId>
<artifactId>springcloud</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<modules>
<module>admin</module>
<!--<module>config-server</module>
<module>config-client</module>
<module>consumer</module>
<module>eureka</module>
<module>hystrix</module>
<module>oAuth2</module>
<module>provider</module>
<module>security</module>
<module>sleuth</module>
<module>zuul</module>
<module>config-bus</module>-->
</modules>
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>nexus-releases</name>
<url>http://maven.aliyun.com/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>nexus-snapshots</name>
<url>http://maven.aliyun.com/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<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>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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-openfeign</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<sword.version>0.0.1-SNAPSHOT</sword.version>
</properties>
</project>
2,java类
package com.hcmony;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableAdminServer
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
3,配置文件
server.port=8070
eureka.client.service-url.defaultZone= http://10.10.1.37:8888/eureka/
spring.application.name=admin
eureka.instance.preferIpAddress=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
#配置登录名,密码和安全策略
#spring.profiles.active=vpc
spring.security.user.name=root
spring.security.user.password=root_123
#暴露actuator的所有端点
management.endpoints.web.exposure.include=*
#health endpoint是否必须显示全部细节。默认情况下, /actuator/health 是公开的,并且不显示细节
management.endpoint.health.show-details=always
以上就是搭建admin-server 的代码,
注意事项:
1,所有要检测的服务必须开启健康检测接口如下配置:
#暴露actuator的所有端点
management.endpoints.web.exposure.include=*
#health endpoint是否必须显示全部细节。默认情况下, /actuator/health 是公开的,并且不显示细节
management.endpoint.health.show-details=always
2,要检测的服务必须与admin这个服务都注册在同一个注册中心。
3,下面这个jar必须引入。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
至此springboot-admin 检测服务的健康就完成了。