SpringBoot——监控中心(Admin )

版权声明: https://blog.csdn.net/typ1805/article/details/86289199

一、简介

          Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。 应用程序作为Spring Boot Admin Client向为Spring Boot Admin Server注册(通过HTTP)或使用SpringCloud注册中心(例如Eureka,Consul,Zookeeper)发现。 UI是的AngularJs应用程序,展示Spring Boot Admin Client的Actuator端点上的一些监控。

常见的功能或者监控如下:

  • 显示健康状况
  • 显示详细信息,例如:JVM和内存指标、micrometer.io指标、数据源指标、缓存指标等
  • 显示构建信息编号
  • 关注并下载日志文件
  • 查看jvm系统和环境属性
  • 查看Spring Boot配置属性
  • 支持Spring Cloud的postable / env-和/ refresh-endpoint
  • 轻松的日志级管理
  • 与JMX-beans交互
  • 查看线程转储
  • 查看http跟踪
  • 查看auditevents
  • 查看http-endpoints
  • 查看计划任务
  • 查看和删除活动会话(使用spring-session)
  • 查看Flyway / Liquibase数据库迁移
  • 下载heapdump
  • 状态变更通知(通过电子邮件,Slack,Hipchat等)
  • 状态更改的事件日志(非持久性)

二、单独集成SpringBoot Admin

搭建版本:

  • Spring Boot版本为:2.1.1.RELEASE
  • Spring Cloud版本为:Finchley.SR2
  • Spring Boot Admin 版本为:2.1.0 

parent的pom.xml文件:

<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.SR2</spring-cloud.version>
	</properties>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

1、搭建SpringBoot Admin Server端(admin-server)

添加相关依赖:

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

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

在启动类添加注解@EnableAdminServer,开启AdminServer的功能:

package com.example.demo.admin;

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);
	}

}

在application.yml配置工程名和端口:

spring:
  application:
    name: admin-server
server:
  port: 8081

2、搭建SpringBoot Admin Client端(admin-client)

添加相关依赖:

<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-web</artifactId>
		</dependency>

在application.yml配置应用名和端口信息,以及向admin-server注册的地址为http://localhost:8081,最后暴露自己的actuator的所有端口信息:

server:
  port: 8082
spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://localhost:8081
management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: '*'

3、分别启动admin-server和admin-client,浏览器访问:http://localhost:8081

点击wallboard可以查看admin-client具体的信息,比如内存状态信息:

查看spring bean的情况:

三、使用Eureka为注册中心集成

SpringBoot和SpringCloud的版本和上面的一样,没有变化。

1、搭建注册中心(eureka-server)

添加eureka依赖:

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>

在application.yml配置端口信息,以及defaultZone和防止自注册,系统暴露eureka-server的actuator的所有端口:

server:
  port: 8001
spring:
  application:
    name: eureka-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka
    register-with-eureka: false
    fetch-registry: false
management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: '*'

启动类添加注解 @EnableEurekaServer,启动Eureka Server:

package com.example.demo.admin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

2、搭建SpringBoot Admin Server端(eureka-admin-server)

添加依赖:

  1. 引入admin-server的起步依赖、web的起步依赖、eureka-client的起步依赖;
  2. 集成spring security:2.1.0版本中去掉了hystrix dashboard,登录界面默认集成到了spring security模块,只要加上spring security就集成了登录模块;
  3. 集成邮箱报警功能:在spring boot admin中,也可以集成邮箱报警功能,比如服务不健康了、下线了,都可以给指定邮箱发送邮件。
<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-server</artifactId>
			<version>2.1.0</version>
		</dependency>

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

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-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-mail</artifactId>
		</dependency>

在application.yml配置:

  1. 应用名、端口信息。并向注册中心注册,注册地址为http://localhost:8001,最后将actuator的所有端口暴露出来;
  2. 配置spring security的用户名和密码,这时需要在服务注册时带上metadata-map的信息;
  3. 配置邮件相关的配置,发邮件者、用户名、密码(授权码)、收件者等信息,配置邮件后,当我们已注册的客户端的状态从 UP 变为 OFFLINE 或其他状态,服务端就会自动将电子邮件发送到上面配置的地址。
server:
  port: 8002

spring:
  application:
    name: eureka-admin-server
  security:
    user:
      name: "admin"
      password: "admin"
  mail:
    host: smtp.163.com
    username: [email protected]
    password: admin #此处的密码是授权码
  boot:
    admin:
      notify:
        mail:
          to: [email protected]

eureka:
  client:
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8001}/eureka/
    registry-fetch-interval-seconds: 5
  instance:
    health-check-url-path: /actuator/health
    lease-renewal-interval-in-seconds: 10
    metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}

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

新建配置类SecuritySecureConfig继承WebSecurityConfigurerAdapter:

package com.example.demo.admin.config;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

/**
 * 路径:com.example.demo.admin.config
 * 类名:
 * 功能:《用一句描述一下》
 * 备注:
 * 创建人:typ
 * 创建时间:2019/1/11 11:05
 * 修改人:
 * 修改备注:
 * 修改时间:
 */
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter{

    private final String adminContextPath;

    public SecuritySecureConfig(AdminServerProperties adminServerProperties){
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        http.authorizeRequests()
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler )
                .and()
                .logout().logoutUrl(adminContextPath+"/logout")
                .and()
                .httpBasic()
                .and()
                .csrf()
                .disable();
    }
}

启动类添加@EnableAdminServer注解,开启admin server的功能,加上@EnableDiscoveryClient注解开启eurke client的功能:

package com.example.demo.admin;

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;

@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class EurekaAdminServerApplication {

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

}

3、搭建SpringBoot Admin Client端(eureka-admin-client)

添加依赖:

由于2.1.0采用webflux,引入webflux的起步依赖,引入eureka-client的起步依赖,并引用actuator的起步依赖。

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

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

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

在application.yml配置应用名、端口、向注册中心注册的地址,以及暴露actuator的所有端口:

server:
  port: 8003
spring:
  application:
    name: eureka-admin-client
eureka:
  instance:
    health-check-url-path: /actuator/health
    lease-renewal-interval-in-seconds: 10
  client:
    registry-fetch-interval-seconds: 5
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8001}/eureka/
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

在启动类加上@EnableDiscoveryClie注解,开启DiscoveryClient的功能:

package com.example.demo.admin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaAdminClientApplication {

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

}

启动工程,在浏览器上访问:http://localhost:8002/,登录输入配置好的用户名和密码(admin/admin),如下显示:

输入配置的用户名和密码,登录:

查看注册中信息,浏览器访问:http://localhost:8001/

 

源码下载:

https://download.csdn.net/download/typ1805/10910479

猜你喜欢

转载自blog.csdn.net/typ1805/article/details/86289199