Spring Boot Admin 监控

简介

Spring Boot Admin 用于监控基于 Spring Boot 的应用,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。Spring Boot Admin 提供了很多功能,如显示 name、id 和 version,显示在线状态,Loggers 的日志级别管理,Threads 线程管理,Environment 管理等。

具体有什么好处,有什么作用,官网说的很清楚。github地址

spring boot amdin 的服务端

新增项目gmaya-springbootadmin。作为spring boot amdin 的服务端

pom文件

新增pom文件

	    <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableAdminServer

配置文件

修改application.yml

server:
  port: 9100

spring:
  application:
    name: gmaya-springbootadmin
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/
    healthcheck:
      enabled: true # 开启健康检查

# 监控
management:
  endpoints:
    web:
      exposure:
        # 通过HTTP公开所有的端点, 默认是info,health
        include: '*'
  endpoint:
    health:
      # 显示完整信息,#默认是never(简要信息)
      show-details: always

因为是使用的Eureka注册中心,所以不需要单独客户端了, 直接在想要监控的客户端直接暴露端点即可。

客户端修改

比如此时,我想要监控gmaya-wepapi-admin,gmaya-service-admin
只需要在这两个项目的配置类新增

# 监控
management:
  endpoints:
    web:
      exposure:
        # 通过HTTP公开所有的端点, 默认是info,health
        include: '*'
  endpoint:
    health:
      # 显示完整信息,#默认是never(简要信息)
      show-details: always

查看效果

启动注册中心,admin服务端,两个客户端。

注册中心页面

spring boot admin 页面

点进去,可以查看服务的详细信息

添加登录页面

在spring boot amdin 服务端修改

可参考官方示例

pom新增

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

配置文件

spring:
  application:
    name: gmaya-springbootadmin
  security:
    user:
      name: "gmaya"
      password: "gmaya"
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/
    healthcheck:
      enabled: true # 开启健康检查
  instance:
    metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}
     

新增配置类
SecuritySecureConfig,直接将官方给出的示例改造一下拿过来了。

package top.gmaya.gmayaspringbootadmin.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
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;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import de.codecentric.boot.admin.server.config.AdminServerProperties;

/**
 * spring boot admin 官网示例 
 * @author GMaya
 * @dateTime 2020/5/9 10:30
 */
@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");
        successHandler.setDefaultTargetUrl(this.adminContextPath + "/");

        http.authorizeRequests((authorizeRequests) -> authorizeRequests
            .antMatchers(this.adminContextPath + "/assets/**").permitAll()
            .antMatchers(this.adminContextPath + "/login").permitAll().anyRequest().authenticated())
            .formLogin((formLogin) -> formLogin.loginPage(this.adminContextPath + "/login")
                .successHandler(successHandler))
            .logout((logout) -> logout.logoutUrl(this.adminContextPath + "/logout"))
            .httpBasic(Customizer.withDefaults())
            .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringRequestMatchers(
                    new AntPathRequestMatcher(this.adminContextPath + "/instances",
                        HttpMethod.POST.toString()),
                    new AntPathRequestMatcher(this.adminContextPath + "/instances/*",
                        HttpMethod.DELETE.toString()),
                    new AntPathRequestMatcher(this.adminContextPath + "/actuator/**")));

    }
}

重启,查看效果

邮件通知

如果服务下线,会进行邮件通知
在spring boot amdin 服务端修改

pom新增

       <!--邮件通知-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-mail</artifactId>
       </dependency>

配置文件application.yml修改

spring:
  application:
    name: gmaya-springbootadmin
  security:
    user:
      name: "gmaya"
      password: "gmaya"
  mail:
    # 发件人使用的qq邮箱服务
    host: smtp.qq.com
    username: gmaya@qq.com
    # 授权码,不是密码,在qq邮箱设置-账号里面有生成授权码
    password: ceevfekeeeeeeeee
  boot:
    admin:
      notify:
        mail:
          # 收件人,多个中间用,分隔
          to: xxx@xxx.com
          # 发件人
          from: gmaya@qq.com

重启服务,然后将一个客户端关闭

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值