SpringBoot Admin 2.0

Spring Boot Admin 用于管理和监控一个或者多个Spring Boot应用, Spring Boot Admin 分为Server端和Client端,Client通过http向Server端注册, 也可以结合Spring Cloud 的服务注册组件Eureka 进行注册,本项目使用Eureka进行注册发现。

github地址:https://github.com/liangxiaobo/SpringBootAdminDemo

首先看项目结构分为:

  • eureka-server 发现注册服务
  • spring-boot-admin-server SpringBootAdmin服务管理端
  • spring-boot-admin-client SpringBootAdmin客户端


    SpringBoot Admin 2.0_第1张图片
    c1.png

Eureka Server

eureka-server项目的搭建过程请看 spring cloud 搭建集群Eureka Server ,这里就简单说下

pom.xml 依赖

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

启动类中添加注释 @EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

application.yml

spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8761

成功启动后访问:http://localhost:8761

spring-boot-admin-server

admin-server中的认证模块一起说看完整的pom.xml



    4.0.0

    com.spring.boot.admin.server
    spring-boot-admin-server
    0.0.1-SNAPSHOT
    jar

    spring-boot-admin-server
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        2.0.1
        Finchley.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            de.codecentric
            spring-boot-admin-starter-server
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-security
        
        
            org.jolokia
            jolokia-core
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
            
                de.codecentric
                spring-boot-admin-dependencies
                ${spring-boot-admin.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

spring-boot-admin-server的启动类:

@Configuration
@EnableAutoConfiguration
@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
public class SpringBootAdminServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminServerApplication.class, args);
    }
   
}

spring-boot-admin-server也做为Eureka Client端;

增加一个安全配置类:

@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 {
        // @formatter:off
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");

        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().disable();
        // @formatter:on
    }

}

application.yml

server:
  port: 8766
spring:
  application:
    name: admin-server
  security:
    user:
      name: "admin"
      password: "admin"


eureka:
  instance:
      leaseRenewalIntervalInSeconds: 10
      health-check-url-path: /actuator/health
      prefer-ip-address: true
      metadata-map:
            user.name: ${spring.security.user.name}
            user.password: ${spring.security.user.password}
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

这其中 security.user.name和security.user.password是访问admin-server的权限

security:
    user:
      name: "admin"
      password: "admin"

注意eureka的实例下要添加 eureka.instance.metadata-map.user.name和eureka.instance.metadata-map.user.name 否则在安全模式下无法注册

eureka:
  instance:
      metadata-map:
            user.name: ${spring.security.user.name}
            user.password: ${spring.security.user.password}

现在启动访问 spring-boot-admin-server http://localhost:8766

SpringBoot Admin 2.0_第2张图片
c2.png
SpringBoot Admin 2.0_第3张图片
c3.png

下面这张图中间部分是因为我用火狐浏览器截图整个网页的原因


SpringBoot Admin 2.0_第4张图片
c4.png

spring-boot-admin-client

完成的pom.xml



    4.0.0

    com.spring.boot.admin.client
    spring-boot-admin-client
    0.0.1-SNAPSHOT
    jar

    spring-boot-admin-client
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Finchley.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-security
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-webflux
        
        
            org.jolokia
            jolokia-core
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.security
            spring-security-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


启动类不用修改

@SpringBootApplication
public class SpringBootAdminClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminClientApplication.class, args);
    }
}

application.yml

spring:
  application:
    name: admin-client
  security:
    user:
      name: "client"
      password: "client"

management:
  endpoints:
    web:
      exposure:
        include: "*"
logging:
  file: /var/log/sample-boot-application.log
  pattern:
    file: clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    prefer-ip-address: true
    metadata-map:
      user.name:  ${spring.security.user.name}
      user.password:  ${spring.security.user.password}
  client:
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8764

security.user.name、 security.user.password 是SpringBootAdmin客户端的认证,同理 Eureka实例必须加上

eureka:
  instance:
    metadata-map:
      user.name:  ${spring.security.user.name}
      user.password:  ${spring.security.user.password}

现在启动admin-client,在admin-server中发现多了一个


SpringBoot Admin 2.0_第5张图片
c7.png

如果启动多个admin-client实例,会看到在admin-server中admin-client下面有两个实例


SpringBoot Admin 2.0_第6张图片
c8.png

配置邮件通知

当客户端触发UP或OFFLINE或其它状态时,会发邮件通知

首先在 spring-boot-admin-server的pom.xml中添加依赖


    org.springframework.boot
    spring-boot-starter-mail

然后在application.yml中添加 配置JavaMailSender

###################
  # 邮件通知配置
  ##################
spring:
   mail:
      host: smtphm.qiye.163.com
      username: # 用户名
      password: # 密码
   boot:
     admin:
        notify:
          mail:
          from: # 发件人
          to: # 收件人
          enabled: true

配置完成,依次启动eureka-server、spring-boot-admin-server、spring-boot-admin-client
当admin-client成功注册时,停掉admin-client,就会收到一个邮件通知


SpringBoot Admin 2.0_第7张图片
d1.png

你可能感兴趣的:(SpringBoot Admin 2.0)