Spring Boot Admin实战代码

SpringBoot Admin服务监控与告警。
这篇教程内容:
1、微服务项目,配合Eureka注册中心,监控在注册中心上面注册的所有服务
2、集成spring security,Admin的登录界面
在这里插入图片描述

一、Spring Boot Admin服务端搭建

SpringBoot版本:2.3.10.RELEASE
SpringBoot Admin版本:2.3.1
SpringCloud版本:Hoxton.SR11
JDK版本:jdk1.8

本项目在前文搭建的环境的基础上,已经构建好了Euraka注册中心、Config配置中心、学生服务模块、课程服务模块。详情看前文:
(一)springcloud实战代码之eureka注册中心
(二)springcloud实战之config配置中心
(三)SpringCloud实战之openfeign服务调用
(四)SpringCloud代码实战之hystrix熔断器

1.1、构建Admin服务端

新建Admin服务→创建启动类→启动类加@EnableAdminServer
注解
在这里插入图片描述

1.2、引入依赖

Admin依赖:

  <!--springboot Admin 服务端依赖-->
  <!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server -->
   <dependency>
       <groupId>de.codecentric</groupId>
       <artifactId>spring-boot-admin-starter-server</artifactId>
       <version>2.3.1</version>
   </dependency>

该依赖在spring-boot-starter-parent依赖中没有声明版本,即使你父工程的pom里面引入了spring-boot-starter-parent,这里依然需要自己手动规定版本号。
Admin的版本号要与SpringBoot的版本尽量一致,这里很容易因为版本问题出错!!!
maven中心库:https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server

其它依赖:
在这里插入图片描述

1.3、bootstrap.yml和配置文件

因为用到了config配置中心,用bootstrap.yml配置配置中心信息

spring:
  application:
    name: admin-service
  profiles:
    active: tzq

  # 配置中心存放配置文件格式:${application.name}-${profiles.active}.yml
  # 例如student-service-tzq.yml、student-service-tzq.properties
  # 通过上述两个配置去配置中心读取对应的配置文件
  cloud:
    config:
      uri: http://localhost:8010    #uri 配置中心服务地址
      fail-fast: true    #开启配置文件失败快速响应
      retry:
        initial-interval: 1000 #请求重试的初始时间间隔(毫秒)
        max-attempts: 6 #最大重试次数
        max-interval: 2000 #最大间隔时间

配置中心的admin-service-tzq.yml配置文件:

#端口
server:
  port: 8769

#服务名称
spring:
  application:
    name: admin-server

#暴露Actuator所有端点和health的详细信息
management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: "*"

疑点? 为什么没有引入Actuator依赖没什么可以用Actuator的management的相关配置?
因为:
Spring Boot Admin服务端依赖下包含:Spring Boot Actuator依赖,不需要再引入。
在这里插入图片描述
除此之外!Eureka注册中心服务端依赖下也包含:Spring Boot Actuator依赖
在这里插入图片描述

1.4、yml和properties文件避坑

配置文件用yml和properties都可以,有一个坑:
properties文件中字符串是不需要加双引号,加了就会报错!
yml文件中可以加。
在这里插入图片描述
在这里插入图片描述

到这里!Admin服务端搭建完成,Admin服务端注册到了Euraka注册中心,它可以找到注册中心里面的其它服务,从而监控其他服务状态(其它服务需要引入Actuator依赖,并开启Actuator端点)

1.5、注册到Euraka注册中心的Admin客户端的依赖和配置

以student-server为例:
在这里插入图片描述
配置文件:开启Actuator端点

# 端口
server.port=8011

# 服务名称
spring.application.name=student-service

# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://8.136.210.255:3306/tzq?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=Admin@123

# 配置actuator的访问端口,如果不配置则默认跟该服务运行端口一样
# management.server.port=7802
# 配置actuator的info信息,info.后面可以自己随便定义
info.name=${spring.application.name}
info.tzq=tzq
# 暴露actuator所有端点
management.endpoints.web.exposure.include=*
# 展示health详细信息
management.endpoint.health.show-details=always


# 开启hystrix熔断器(默认是关闭)
feign.hystrix.enabled=true

到这里环境全部构建完成:
开启注册中心→开启配置中心→开启student-server-开启Admin-server

然后打开浏览器:http://localhost:8769/
(Admin-server端口是8769)
在这里插入图片描述
选择student-server服务,可以看到信息:
在这里插入图片描述

二、集成spring security:添加登录界面

在2.1.0版本后去掉了hystrix dashboard,登录界面默认集成到了spring security模块,只要加上spring security就集成了登录模块。
只需要改变下admin-server工程,需要在admin-server工程的pom文件引入以下的依赖:

2.1、引入依赖

 <!--  spring-boot-starter-security依赖,Admin集成security,配置Admin登录页面-->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
  </dependency>

在这里插入图片描述
父工程引入了spring-boot-starter-parent,这里不需要规定版本

2.2、新建AdminSecurityConfig配置类

新建AdminSecurityConfig配置类(名字自定义),继承WebSecurityConfigurerAdapter

package com.tzq.adminserver.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;

/**
 * @Author tangzhiqian
 * @CreateTime 2021/5/28 13:14
 */

@Configuration
public class AdminSecurityConfig extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;

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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        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();
        // @formatter:on
    }
}


在这里插入图片描述

2.3、添加配置

admin-service-tzq.yml

server:
  port: 8769

spring:
  application:
    name: admin-server
  # 配置Admin登录账户密码
  security:
    user:
      name: "admin"
      password: "admin"


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

在这里插入图片描述
启动admin-server,打开浏览器:http://localhost:8769
在这里插入图片描述
大功告成!!!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值