irpas技术客

Spring Boot Admin实战代码_旺仔OO糖

网络 3851

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 大功告成!!!


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Spring #Boot #Admin实战代码 #springboot #Admin服务监控与告警