结合eureka监控springboot

admin-server

1、引用

  1. <dependency>
  2. <groupId>de.codecentric</groupId>
  3. <artifactId>spring-boot-admin-starter-server</artifactId>
  4. <version>2.1.5</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.cloud</groupId>
  12. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  13. </dependency>

2、配置

  1. spring:
  2. application:
  3. name: admin-server
  4. server:
  5. port: 8000
  6. eureka:
  7. client:
  8. service-url:
  9. defaultZone: http://localhost:8761/eureka
  10. # 设置监控
  11. management:
  12. endpoints:
  13. web:
  14. exposure:
  15. include: "*"
  16. endpoint:
  17. health:
  18. show-details: ALWAYS

3、启动

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableAdminServer
  4. public class AdminApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(AdminApplication.class, args);
  7. }
  8. }

4、查看页面

image.png

client

1、引用

  1. <dependency>
  2. <groupId>de.codecentric</groupId>
  3. <artifactId>spring-boot-admin-starter-client</artifactId>
  4. <version>2.1.5</version>
  5. </dependency>

2、配置

  1. management:
  2. endpoints:
  3. web:
  4. exposure:
  5. include: "*"
  6. endpoint:
  7. health:
  8. show-details: ALWAYS

3、查看效果

添加安全校验

引入

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>

配置密码

  1. spring:
  2. application:
  3. name: admin-server
  4. security:
  5. user:
  6. name: admin
  7. password: admin
  8. server:
  9. port: 8000
  10. eureka:
  11. instance:
  12. leaseRenewalIntervalInSeconds: 10
  13. health-check-url-path: /actuator/health
  14. metadata-map:
  15. user.name: ${spring.security.user.name}
  16. user.password: ${spring.security.user.password}
  17. startup: ${random.int} #needed to trigger info and endpoint update after restart
  18. client:
  19. registryFetchIntervalSeconds: 5
  20. service-url:
  21. defaultZone: http://localhost:8761/eureka
  22. management:
  23. endpoints:
  24. web:
  25. exposure:
  26. include: "*"
  27. endpoint:
  28. health:
  29. show-details: ALWAYS

添加过滤

  1. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  6. import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  7. @Configuration
  8. public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  9. private final String adminContextPath;
  10. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
  11. this.adminContextPath = adminServerProperties.getContextPath();
  12. }
  13. @Override
  14. protected void configure(HttpSecurity http) throws Exception {
  15. // @formatter:off
  16. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  17. successHandler.setTargetUrlParameter("redirectTo");
  18. successHandler.setDefaultTargetUrl(adminContextPath + "/");
  19. http.authorizeRequests()
  20. .antMatchers(adminContextPath + "/assets/**").permitAll()
  21. .antMatchers(adminContextPath + "/login").permitAll()
  22. .anyRequest().authenticated()
  23. .and()
  24. .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
  25. .logout().logoutUrl(adminContextPath + "/logout").and()
  26. .httpBasic().and()
  27. .csrf()
  28. .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  29. .ignoringAntMatchers(
  30. adminContextPath + "/instances",
  31. adminContextPath + "/actuator/**"
  32. );
  33. }
  34. }

添加邮件提醒

引入

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-mail</artifactId>
  4. </dependency>

设置

  1. spring:
  2. application:
  3. name: admin-server
  4. security:
  5. user:
  6. name: admin
  7. password: admin
  8. # 设置mail
  9. mail:
  10. host: smtp.163.com
  11. username: xxx@163.com
  12. password: xxx
  13. properties:
  14. mail:
  15. smtp:
  16. auth: true
  17. starttls:
  18. enable: true
  19. required: true
  20. boot:
  21. admin:
  22. monitor:
  23. read-timeout: 20000 # 设置超时时间ms
  24. notify:
  25. mail:
  26. from: tianyunperfect@163.com
  27. to: tianyunperfect@163.com

最终版本下载

cloudTest.zip

image.png