SpringCloud Greenwich版本集成Springboot Admin监控和管理平台

it2023-06-10  67

前言:

Springboot Admin是Springboot框架下比较优秀的开源监控管理平台,它是基于Springboot自带监控Actuator中的信息,来进行界面化展示,并且还可以提供监控报警、可视化日志、切换日志级别等功能。下面我将分享一下如何在SpringCloud集成Springboot Admin。

正文:

一、版本信息:

SpringCloud  Greenwich版本、Springboot  2.1.4.RELEASE版本、Springboot Admin  2.1.0版本

二、配置Springboot Admin Server:

1.引入依赖架包:

这里引入Springboot Admin相关的架包,还引入了security的架包。security可以使Springboot Admin更加安全,用户访问Springboot Admin界面时会进行登录验证。

<properties> <spring.boot.admin.version>2.1.0</spring.boot.admin.version> </properties> ... ... <dependencies> !--admin-server--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server</artifactId> <version>${spring.boot.admin.version}</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui</artifactId> <version>${spring.boot.admin.version}</version> </dependency> <!--security--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies>

2.Springboot启动类上添加@EnableAdminServer注解:

3.配置SecuritySecureConfig(安全相关配置):

@Configuration public static 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(adminContextPath + "/"); 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() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringAntMatchers( adminContextPath + "/instances", adminContextPath + "/actuator/**" ); } }

4.配置YAML文件: 

spring: ... ... security: user: name: user password: hanxiaozhang boot: admin: ui: brand: "<span>Hanxiaozhang Admin</span>" title: Hanxiaozhang Admin management: endpoints: web: exposure: include: "*" endpoint: health: show-details: always

5.启动:

 三、 配置Springboot Admin Client:

1.引入依赖架包:

<properties> <spring.boot.admin.version>2.1.0</spring.boot.admin.version> </properties> ... ... <dependencies> <!--admin-client--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>${spring.boot.admin.version}</version> </dependency> </dependencies>

2.配置YAML文件:  

spring: ... ... boot: admin: client: url: http://localhost:8018 username: user password: hanxiaozhang management: endpoints: web: exposure: include: "*" endpoint: health: show-details: always info: app: name: hanxiaozhang company: name: hanxiaozhang build: artifactId: @project.artifactId@ version: @project.version@

 3.启动:

 

 

 四、Tips:

如果你想对服务的上下线进行监控,可以继承AbstractStatusChangeNotifier类完成对服务的监控,具体的实现如下:

@Slf4j @Profile({"prod", "test"}) @Component public class CustomNotifier extends AbstractStatusChangeNotifier { public CustomNotifier(InstanceRepository repositpry) { super(repositpry); } @Override protected Mono<Void> doNotify(InstanceEvent event, Instance instance) { return Mono.fromRunnable(() -> { if (event instanceof InstanceStatusChangedEvent) { StatusInfo statusInfo = ((InstanceStatusChangedEvent) event).getStatusInfo(); if (!statusInfo.isUp()) { // 服务名称 String appName = instance.getRegistration().getName(); // 实例名称(在eureka中注册的唯一id) String instanceId = instance.getId().toString(); // 事件发生的时间 String time = event.getTimestamp().toString(); // 服务器的ip URL url = null; try { url = new URL(instance.getRegistration().getServiceUrl()); } catch (MalformedURLException e) { e.printStackTrace(); } // 封装成自定义事件对象 StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("服务名:".concat(appName).concat("(").concat(instanceId).concat(")")); stringBuffer.append("\n地址:".concat(url.getHost())); stringBuffer.append("\n最后状态:".concat(statusInfo.getStatus())); stringBuffer.append("\n时间:".concat(time)); // 事件的全部信息 log.info("微服务节点掉线:[{}]", stringBuffer.toString()); // todo 发送邮件 } } }); } }

 此外,如果你把所有服务都部署到服务器上时,机智的你可能发现红框内地址的域名都是你服务器的主机名,直接点击可能链接失败,我尝试过在YAML配置好IP地址的做法,但是又因服务器内网IP和公网IP的问题没有成功,我最后的解决方案是在电脑的host配置文件中配置主机名与IP地址的映射。

 

最新回复(0)