springboot admin 监控搭建
一.admin服务端搭建:
1.新建一个springboot项目,版本2.3.7.RELEASE,
引入pom文件:
<!--springAdmin--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency> <!--springboot 的安全框架--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!--告警邮件通知--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2.添加注解:
@EnableAdminServer @SpringBootApplication public class SpringBootAdminerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminerApplication.class, args); } }
3.配置
yml配置文件
# 应用服务 WEB 访问端口
server.port: 9999
spring: application: name: spring-boot-adminer # ##############boot admin 配置############## boot: admin: notify: mail: to: xx@qq.com monitor: info-interval: 5s info-lifetime: 5s status-interval: 5s status-lifetime: 5s # ##############安全认证配置############### security: user: name: admin password: 123456 # ##############邮件配置############## mail: host: smtp.qq.com username: xx@qq.com password:
安全配置类:官方配置,可直接使用
@Configuration(proxyBeanMethods = false) class SecurityConfig extends WebSecurityConfigurerAdapter { private final AdminServerProperties adminServer; private final SecurityProperties security; public SecurityConfig(AdminServerProperties adminServer, SecurityProperties security) { this.adminServer = adminServer; this.security = security; } @Override protected void configure(HttpSecurity http) throws Exception { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); successHandler.setDefaultTargetUrl(this.adminServer.path("/")); http.authorizeRequests( (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll() .antMatchers(this.adminServer.path("/actuator/info")).permitAll() .antMatchers(this.adminServer.path("/actuator/health")).permitAll() .antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated() ).formLogin( (formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and() ).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults()) .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringRequestMatchers( new AntPathRequestMatcher(this.adminServer.path("/instances"), HttpMethod.POST.toString()), new AntPathRequestMatcher(this.adminServer.path("/instances/*"), HttpMethod.DELETE.toString()), new AntPathRequestMatcher(this.adminServer.path("/actuator/**")) )) .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600)); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser(security.getUser().getName()) .password("{noop}" + security.getUser().getPassword()).roles("USER"); } }
二、客户端搭建
1.在需要被监控的应用中引入pom依赖:
<!--spring-boot 的actuator 端点--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--spring-boot admin 的客户端--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> </dependency>
2.客户端配置:
spring:
# #############bootAdmin的监控配置#############
boot:
admin:
client:
# ############bootAdmin的服务端的地址和用户密码############
url: http://localhost:9999
username: admin
password: 123456
instance:
prefer-ip: true
# springboot actuator 端点 management: endpoints: web: exposure: # ##############开放所有端点############## include: '*' endpoint: health: # ##############显示监控的详细信息############## show-details: always
三、基于微服务注册中心的方式配置:admin-server会自动从注册中心拉取各个服务的信息进行监控,就不需要在每个服务服务节点单独进行配置了。
1.注册中心服务搭建
假设使用的是eureka注册中心,则注册中心服务引入pom依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
yml配置:
spring: application: name: eureka-server server: port: 8761 eureka: client: service-url: defaultZone: http://localhost:8761/eureka register-with-eureka: false fetch-registry: false management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS
2.admin-server更改配置
引入新的pom依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
yml配置文件中添加配置:
eureka: client: registryFetchIntervalSeconds: 5 service-url: defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/ instance: leaseRenewalIntervalInSeconds: 10 health-check-url-path: /actuator/health
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password} management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS
启动类添加注解:@EnableDiscoveryClient
@EnableDiscoveryClient @EnableAdminServer @SpringBootApplication public class SpringBootAdminerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminerApplication.class, args); } }