SpringBoot实战电商项目mall(20k+star)地址:github.com/macrozheng/…java
Spring Boot Admin 能够对SpringBoot应用的各项指标进行监控,能够做为微服务架构中的监控中心来使用,本文将对其用法进行详细介绍。git
SpringBoot应用能够经过Actuator来暴露应用运行过程当中的各项指标,Spring Boot Admin经过这些指标来监控SpringBoot应用,而后经过图形化界面呈现出来。Spring Boot Admin不只能够监控单体应用,还能够和Spring Cloud的注册中心相结合来监控微服务应用。github
Spring Boot Admin 能够提供应用的如下监控信息:web
这里咱们建立一个admin-server模块来做为监控中心演示其功能。spring
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
复制代码
spring:
application:
name: admin-server
server:
port: 9301
复制代码
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
复制代码
这里咱们建立一个admin-client模块做为客户端注册到admin-server。cookie
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
复制代码
spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:9301 #配置admin-server地址
server:
port: 9305
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
logging:
file: admin-client.log #添加开启admin的日志监控
复制代码
点击wallboard按钮,选择admin-client查看监控信息;架构
监控信息概览;app
logging:
file: admin-client.log #添加开启admin的日志监控
复制代码
Spring Boot Admin结合Spring Cloud 注册中心使用,只需将admin-server和注册中心整合便可,admin-server 会自动从注册中心获取服务列表,而后挨个获取监控信息。这里以Eureka注册中心为例来介绍下该功能。ide
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
复制代码
spring:
application:
name: admin-server
server:
port: 9301
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8001/eureka/
复制代码
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
复制代码
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
复制代码
spring:
application:
name: admin-client
server:
port: 9305
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
logging:
file: admin-client.log #添加开启admin的日志监控
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8001/eureka/
复制代码
@EnableDiscoveryClient
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
复制代码
启动eureka-server,使用application-eureka.yml配置启动admin-server,admin-client;spring-boot
查看注册中心发现服务均已注册:http://localhost:8001/
咱们能够经过给admin-server添加Spring Security支持来得到登陆认证功能。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
复制代码
spring:
application:
name: admin-security-server
security: # 配置登陆用户名和密码
user:
name: macro
password: 123456
boot: # 不显示admin-security-server的监控信息
admin:
discovery:
ignored-services: ${spring.application.name}
server:
port: 9301
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8001/eureka/
复制代码
/** * Created by macro on 2019/9/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(adminContextPath + "/");
http.authorizeRequests()
//1.配置全部静态资源和登陆页能够公开访问
.antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
.anyRequest().authenticated()
.and()
//2.配置登陆和登出路径
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
.logout().logoutUrl(adminContextPath + "/logout").and()
//3.开启http basic支持,admin-client注册时须要使用
.httpBasic().and()
.csrf()
//4.开启基于cookie的csrf保护
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
//5.忽略这些路径的csrf保护以便admin-client注册
.ignoringAntMatchers(
adminContextPath + "/instances",
adminContextPath + "/actuator/**"
);
}
}
复制代码
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminSecurityServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminSecurityServerApplication.class, args);
}
}
复制代码
springcloud-learning
├── eureka-server -- eureka注册中心
├── admin-server -- admin监控中心服务
├── admin-client -- admin监控中心监控的应用服务
└── admin-security-server -- 带登陆认证的admin监控中心服务
复制代码
mall项目全套学习教程连载中,关注公众号第一时间获取。