一、SrpingBoot Admin 介绍
Spring Boot Admin 是一个开源社区项目,用于管理和监控 SpringBoot 应用程序,展示Spring Boot Admin Client 的 Actuator 端点上的一些监控信息。
了解springcloud架构可以加求求:三五三六二四七二五九
它为应用程序提供以下功能:
显示应用健康状况
关注并下载日志文件
查看jvm系统和环境属性
查看Spring Boot配置属性
支持Spring Cloud的postable / env-和/ refresh-endpoint
轻松的日志级管理
二、SrpingCloud Kubernetes 介绍
Spring Cloud Kubernetes 提供 Kubernetes 环境下服务发现的 Spring Cloud 通用接口实现。主要目的是促进在 Kubernetes 中运行的 Spring Cloud 和 Spring Boot 应用程序的集成。
这里我们主要用 SpringCloud Kubernetes 来为 SpringBoot Admin 提供 Kubernetes 环境下的服务发现。
三、创建 SpringBoot Admin 应用
创建 SpringBoot Admin 应用,且引入 SpringCloud Kubernetes 作为服务发现。
Maven 引入相关依赖
在 Maven 中引入 “spring-boot-admin-starter-server” 与 “spring-cloud-kubernetes-discovery” 依赖。
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
club.mydlq
springboot-admin-k8s
0.0.2
springboot-admin-k8s
demo
1.8
org.springframework.boot
spring-boot-starter-web
de.codecentric
spring-boot-admin-starter-server
2.1.5
org.springframework.cloud
spring-cloud-kubernetes-discovery
1.0.2.RELEASE
org.springframework.boot
spring-boot-maven-plugin
配置 application 文件
加上两个参数:
spring.cloud.kubernetes.discovery.primaryPortName:
spring.cloud.kubernetes.discovery.serviceLabels:
server:
port: 8080
management:
server:
port: 8081 #---指定监控数据端口为8081,避免和 server.port 一致产生风险
endpoints:
web:
exposure:
include: "*"
spring:
application:
name: springboot-admin-k8s
cloud:
kubernetes:
discovery:
primaryPortName: management #---按设要监控 Service 的端口名称
serviceLabels:
admin: enabled #---设置要监控 Service 的 Label 标签
启动类
需要加上四个注解:
@SpringBootApplication:开启 SpringBoot 相关注解,会自动配置相关信息。
@EnableDiscoveryClient:开启 Spring服务发现机制。
@EnableAdminServer:开启 SpringBoot Admin。
@EnableScheduling:开启定时任务,不加此注解服务发现不会执行定时刷新。
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.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableDiscoveryClient
@EnableAdminServer
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}