说个笑话:“你的代码没有Bug!”。 谁也不能保证本身的代码没有Bug,本身的代码可以100%知足业务性能要求,当线上的服务出问题后,如何快速定位问题、解决问题,这才是一个好程序猿的标志。 通俗一点说,就是快速地将本身挖的坑填平,最好没有其余人发现。html
Spring Boot开发之初已经充分考虑服务监控需求,提供了spring-boot-starter-actuator监控模块,在项目用依赖该模块就能够开启相应的监控endpoit。vue
经常使用的endpoit包括如下:git
endpoints | 说明 |
---|---|
beans | 全部的Spring Bean信息 |
health | 应用健康信息 |
metrics | 应用各方面性能指标 |
mappings | 应用全部的Request Mapping路径 |
heapdump | 下载内存快照 |
具体可参考 官方文档。web
目前经常使用Spring Boot Admin进行Spring Boot应用服务监控,它提供了一个简洁美观的监控页面,底层基于Spring Boot Actuator实现。 Spring Boot Admin包括客户端和服务端:spring
spring-boot-examples-admin-server
项目,添加如下依赖:<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
SecuritySecureConfig
,基于Spring Security实现Admin Server的安全控制,参考代码以下:/** * @author: cent * @email: 292462859@qq.com * @date: 2019/2/11. * @description: <p> * Security配置类,用于配置admin server的安全控制 */ @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() //静态文件容许访问 .antMatchers(adminContextPath + "/assets/**").permitAll() //登陆页面容许访问 .antMatchers(adminContextPath + "/login").permitAll() //其余全部请求须要登陆 .anyRequest().authenticated() .and() //登陆页面配置,用于替换security默认页面 .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and() //登出页面配置,用于替换security默认页面 .logout().logoutUrl(adminContextPath + "/logout").and() .httpBasic().and() .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringAntMatchers( "/instances", "/actuator/**" ); } }
@EnableAdminServer
启用Admin Server。/** * @author: cent * @email: 292462859@qq.com * @date: 2019/2/11. * @description: */ @EnableAdminServer @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
application.yml
以下:spring: application: name: spring-boot-examples-admin-server security: user: name: admin password: 123456 server: port: 8090
spring-boot-examples-admin-client
,添加如下依赖:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> </dependency>
ACTUATOR_ADMIN
,后续配置需使用。/** * @author: cent * @email: 292462859@qq.com * @date: 2019/2/11. * @description: */ @Configuration public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() //拦截全部endpoint,拥有ACTUATOR_ADMIN角色可访问,不然需登陆 .requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ACTUATOR_ADMIN") //静态文件容许访问 .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() //根路径容许访问 .antMatchers("/").permitAll() //全部请求路径能够访问 .antMatchers("/**").permitAll() .and().httpBasic(); } }
application.yml
配置以下(具体配置项做用见注释):spring: application: name: spring-boot-examples-admin-client boot: admin: client: url: "http://localhost:8090/" #spring admin server访问地址 username: admin #spring admin server用户名 password: 123456 #spring admin server密码 instance: metadata: user.name: ${spring.security.user.name} #客户端元数据访问用户 user.password: ${spring.boot.admin.client.password} #客户端元数据访问密码 security: user: name: client #客户端用户名 password: 123456 # 客户端密码 roles: ACTUATOR_ADMIN #拥有角色,用于容许自身访问 # 开放全部endpoint,实际生产根据自身须要开放,出于安全考虑不建议所有开放。 management: endpoints: web: exposure: include: "*" logging: path: logs/ file: admin-client server: port: 8091
分别启动spring-boot-examples-admin-server和spring-boot-examples-admin-client。安全
访问http://localhost:8090,可看到登陆页面。 app
登陆成功后,application页面。 ide
Wallboard页面。 spring-boot
应用监控详情页面
性能
码云:https://gitee.com/centy/spring-boot-examples
Spring Boot Admin使用在Spring Cloud中使用时,能够与Eureka结合在一块儿使用,Spring Boot Admin能够经过Eureka发现其它注册到Eureka中的服务。