Spring Boot之Admin监控服务

生活中,最使人疲惫的往往不是道路的遥远,而是心中的郁闷;最使人痛苦的往往不是生活的不幸,而是希望的破灭;最使人颓废的往往不是前途的坎坷,而是自信的丧失;最使人绝望的往往不是挫折的打击,而是心灵的死亡。所以我们要有自己的梦想,让梦想的星光指引着我们走出落漠,走出惆怅,带着我们走进自己的理想。

导读:本篇文章讲解 Spring Boot之Admin监控服务,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

Spring Boot Admin监控服务

Spring Boot Admin(SBA)是一款基于Actuator开发的开源软件:https://github.com/codecentric/spring-boot-admin,以图形化界面的方式展示Spring Boot应用的配置信息、Beans信息、环境属性、线程信息、JVM状况等。

Spring Boot Admin文档: https://codecentric.github.io/spring-boot-admin/2.3.1/

搭建SBA服务端

搭建一个SBA服务端(Server),其他被监控的Spring Boot应用作为客户端(Client),客户端通过HTTP的方式将自己注册到服务端,以供服务端进行监控服务。

引入依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
	<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-server</artifactId>
			<version>2.3.1</version>
		</dependency>

		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-server-ui</artifactId>
			<version>2.3.1</version>
		</dependency>

开启Admin监控

在Spring Boot入口类中加入@EnableAdminServer注解开启监控功能

@SpringBootApplication
@EnableAdminServer
public class DemoApplication {

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

配置

server:
  port: 8888

spring:
  application:
    name: admin-server

访问控制台

http://localhost:8888/
在这里插入图片描述

搭建客户端

引入依赖

		<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>
			<version>2.3.1</version>
		</dependency>

配置

# 打开客户端的监控
management:
  endpoints:
    web:
      exposure:
        include: '*'
server:
  port: 9999
  
spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://localhost:8888 #admin server的地址列表
        instance:
          service-url: http://127.0.0.1:9999 #当前系统地址

查看控制台

Admin 服务端会自动检查到客户端的变化
在这里插入图片描述
页面会展示被监控的服务列表,点击详项目名称会进入此应用的详细监控信息 , 这些信息大多都来自于 Spring Boot Actuator 提供的接口。

在这里插入图片描述

提醒功能

SBA提供了强大的提醒功能,能够在发生服务状态变更的时候发出告警。

添加邮件预警

默认情况下对于被检测的应用启动或者停止的时候会触发预警。

添加依赖

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

配置

spring:
  application:
    name: admin-server
  mail:
    # 配置 SMTP 服务器地址
    host: smtp.163.com
    # 发送者邮箱
    username: XXX@163.com
    # 配置密码,注意不是真正的密码,而是刚刚申请到的授权码
    password: JIZDE33434KZSVOTZ
    # 端口号:465或者994
    port: 465
    protocol: smtps
    properties:
      mail:
        # 表示开启 DEBUG 模式
        debug: true
        smtp:
          socketFactoryClass: javax.net.ssl.SSLSocketFactory
          auth: true
          starttls:
            enable: true
            required: true
            
  boot:
    admin:
      notify:
        mail:
          from: XXX@163.com
          to: XXX@onmicrosoft.com

查看邮件内容

在这里插入图片描述

安全保护

admin-server安全保护

添加Spring Security依赖

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

配置用户和密码

spring:
  security:
    user:
      name: admin
      password: admin

在这里插入图片描述

此时 , 由于admin-server进行了安全认证 , 因此admin-client端也需要配置下 admin-server的账号和密码。

在这里插入图片描述
客户端配置

spring:
  boot:
    admin:
      client:
        url: http://localhost:8888 #admin server 的地址列表
        instance:
          service-url: http://127.0.0.1:9999 #当前系统地址
          name: client
        # admin-server若开启安全保护,则客户端注册到admin-server,需配置 admin-server的账号和密码
        password: admin
        username: admin

重启客户端,发现控制台有日志警告

 WARN 28024 --- [gistrationTask1] d.c.b.a.c.r.ApplicationRegistrator       : Failed to register application as Application(name=client, managementUrl=http://127.0.0.1:9999/actuator, healthUrl=http://127.0.0.1:9999/actuator/health, serviceUrl=http://127.0.0.1:9999) at spring-boot-admin ([http://localhost:8888/instances]): 401 : [no body]. Further attempts are logged on DEBUG level

由于admin-client 注册到 admin-server时,admin-server端有个http Basic认证,通过了认证后 admin-client才能注册到 admin-server上。

admin-server禁用安全性

@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll()  
            .and().csrf().disable();
    }
}

在这里插入图片描述

admin-client安全保护

	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
spring:
  application:
    name: admin-client
  # admin-client 的用户名和密码
  security:
    user:
      name: admin123
      password: admin123
  boot:
    admin:
      client:
        url: http://localhost:8888 #admin server 的地址列表
        # 配置 admin-server的账号和密码
        username: admin
        password: admin
        instance:
          service-url: http://127.0.0.1:9999 #当前系统地址
          name: client
          metadata:
          # 这里配置admin-client的账号和密码
            user.name: ${spring.security.user.name}
            user.password: ${spring.security.user.password}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/137018.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!