Eureka 与Spring Boot Admin配合,搭建客户端界面健康检查
Eureka 与Spring Boot Admin配合使用:
在Eureka搭建完成的基础上,配置生产者或者消费者使用Spring Boot Admin进行客户端式健康检查:
application,yml配置修改:
# #server: # port: 8001 #eureka: # instance: # prefer-ip-address: true # 注册服务时,使用服务的IP地址 # client: # service-url: # defaultZone: http://localhost:8761/eureka/ #spring: # application: # name: provider-ticket server: port: 8001 spring: application: name: SpringBootAdmin security: user: name: sadmin password: 123456 boot: admin: ui: title: SpringBootAdmin management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS eureka: instance: metadata-map: user.name: sadmin user.password: 123456 easeRenewalIntervalInSeconds: 10 health-check-url-path: /actuator/health ip-address: 127.0.0.1 prefer-ip-address: true instance-id: ${eureka.instance.ip-address}:${server.port} client: registryFetchIntervalSeconds: 5 serviceUrl: defaultZone: http://localhost:8761/eureka/
pom.xml依赖修改:
//额外加入这两个依赖 <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.1.6</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
application.java 启动类的修改:
package com.ellisonpei.providerticket; import de.codecentric.boot.admin.server.config.AdminServerProperties; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; import org.springframework.security.web.csrf.CookieCsrfTokenRepository; /** * @author pei */ @EnableDiscoveryClient @EnableAdminServer @SpringBootApplication public class ProviderTicketApplication { public static void main(String[] args) { SpringApplication.run(ProviderTicketApplication.class, args); } @Configuration public static 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() .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and() .logout().logoutUrl(adminContextPath + "/logout").and() .httpBasic().and() .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringAntMatchers( adminContextPath + "/instances", adminContextPath + "/actuator/**" ); } } }
启动测试: