快速搭建微服务-服务监控
Spring Boot Admin为基于Spring Boot的基础数据安全端口提供了基础的可视化监控功能。还可以通过Spring Boot Admin的服务端程序对Spring Boot程序提供简单的实时管理(例如修改日志输出级别)。
Spring Cloud集成Spring Boot Admin
Spring Boot Admin服务端
Spring Cloud中集成Spring Boot Admin比较简单,只需要将服务端程序注册到注册中心即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server</artifactId> <version>${admin.server.version}</version> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui</artifactId> <version>${admin.server.version}</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-login</artifactId> <version>${admin.server.version}</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-turbine</artifactId> <version>${admin.server.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> <exclusions> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| spring: application: name: admin-dashboard cloud: consul: discovery: instance-id: ${spring.application.name}:${spring.cloud.consul.discovery.instance_id:${random.value}} boot: admin: discovery: ignored-services: consul routes: endpoints: env,metrics,trace,dump,jolokia,info,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream,turbine.stream turbine: clusters: default location: admin-dashboard
management: security: enabled: false
turbine: cluster-name-expression: new String("default") app-config: service-goods, service-order, service-wechat-admin
security: user: name: 登录用户名 password: 登录密码
|
在服务端的启动类加上 @EnableAdminServer
注解声明当前程序为Spring Boot Admin服务端程序。启动类加上 @EnableTurbine
注解开启断路器监控数据的聚合展示。
上面的 spring-boot-admin-server-ui-login
提供了一个登录界面用于访问Spring Boot Admin。需要搭配 spring-security
一起使用。Spring Security的配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .formLogin().loginPage("/login.html").loginProcessingUrl("/login") .successHandler((request, response, exception) -> response.sendRedirect(request.getContextPath() + "/index.html")).permitAll() .and() .logout().logoutUrl("/logout") .and() .authorizeRequests() .antMatchers("/login.html", "//*.css", "/img/", "/third-party/").permitAll() .anyRequest().authenticated() .and().httpBasic();
} }
|
被监控服务的配置
被监控的服务需要配置如下依赖:
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
|
效果展示