目录
- 概述
- 实践
- server端
- pom.xml
- 类配置
- 结果
- client
- pom.xml
- 配置
- 结束
概述
Spring Boot Admin 集权限、日志、异常通知。
实践
server端
pom.xml
<!-- SpringBoot Admin -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<!-- SpringCloud Alibaba Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- SpringCloud Alibaba Sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- SpringBoot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
类配置
@EnableWebSecurity
public class WebSecurityConfigurer
{
private final String adminContextPath;
public WebSecurityConfigurer(AdminServerProperties adminServerProperties)
{
this.adminContextPath = adminServerProperties.getContextPath();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception
{
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
return httpSecurity
.headers().frameOptions().disable()
.and().authorizeRequests()
.antMatchers(adminContextPath + "/assets/**"
, adminContextPath + "/login"
, adminContextPath + "/actuator/**"
, adminContextPath + "/instances/**"
).permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage(adminContextPath + "/login")
.successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout")
.and()
.httpBasic().and()
.csrf()
.disable()
.build();
}
}
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
/**
* 通知发送配置测试用例
*/
@Component
public class StatusChangeNotifier extends AbstractStatusChangeNotifier {
private static final Logger LOGGER = LoggerFactory.getLogger(RuoYiStatusChangeNotifier.class);
public RuoYiStatusChangeNotifier(@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") InstanceRepository repository) {
super(repository);
LOGGER.info("通知发送配置测试用例初始化...");
}
@Override
protected Mono<Void> doNotify(InstanceEvent event,
de.codecentric.boot.admin.server.domain.entities.Instance instance) {
return Mono.fromRunnable(() -> {
if (event instanceof InstanceStatusChangedEvent) {
String instanceName = instance.getRegistration().getName();
String serviceUrl = instance.getRegistration().getServiceUrl();
String status = ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus();
switch (status) {
// 健康检查没通过
case "DOWN":
LOGGER.info("实例名称:{},url:{},发送 健康检查没通过 的通知!", instanceName, serviceUrl);
break;
// 服务离线
case "OFFLINE":
LOGGER.info("实例名称:{},url:{},发送 服务离线 的通知!", instanceName, serviceUrl);
break;
// 服务上线
case "UP":
LOGGER.info("实例名称:{},url:{},发送 服务上线 的通知!", instanceName, serviceUrl);
break;
// 服务未知异常
case "UNKNOWN":
LOGGER.info("实例名称:{},url:{},发送 服务未知异常 的通知!", instanceName, serviceUrl);
break;
default:
LOGGER.info("实例名称:{},url:{},其它情况!", instanceName, serviceUrl);
break;
}
}
});
}
}
结果
client
pom.xml
添加依赖
<!-- SpringBoot Admin Client -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<!-- SpringBoot Actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置
spring:
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
mvc:
pathmatch:
matching-strategy: ant_path_matcher
boot:
admin:
client:
url: http://xx.3xx.xx.131:9100
logging:
file:
name: logs/${spring.application.name}/info.log
Spring Boot Admin提供了基于Web页面的方式实时查看服务输出的本地日志,前提是服务中配置了logging.file.name
结束
Spring Boot Admin服务监控 至此结束。