Spring-Cloud Admin详细操作流程和简单介绍

一、Spring Boot Admin 简介

Spring Boot Admin 用于监控基于 Spring Boot 的应用,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。Spring Boot Admin 提供了很多功能,如显示 name、id 和 version,显示在线状态,Loggers 的日志级别管理,Threads 线程管理,Environment 管理等。基于Cairo-SR3 和 Finchley.SR1。在 Spring Boot 项目中,Spring Boot Admin 作为 Server 端,其他的要被监控的应用作为 Client 端
Spring Boot Admin 是一个管理和监控Spring Boot 应用程序的开源软件。Spring Boot Admin 分为 Server 端和 Client 端,Spring Boot Admin UI部分使用AngularJs将数据展示在前端。
常见的功能或者监控如下:

二、Maven依赖

1.服务端依赖
 
    
	de.codecentric    
	spring-boot-admin-starter-server    
	2.1.5 
 


2. 客户端依赖

 
     
 	org.springframework.boot    
 	spring-boot-starter-web 
 

     
	de.codecentric     
	spring-boot-admin-starter-client     
	2.1.5 

三、配置

  1. 服务端入口类配置

    @Configuration 
    @EnableAutoConfiguration 
    @EnableAdminServer 
    public class SpringBootAdminApplication {    
        
     	public static void main(String[] args) {        
    		SpringApplication.run(SpringBootAdminApplication.class, args);    
    	} 
    
    }
  2. 服务端配置文件

    server:
      port: 8000
    spring:
      application:
        name: admin-server
  3. 客户端配置

    server:
      port: 8000
    spring:
      application:
        name: admin-client 
      #注册到Admin
      boot:
      	admin:
      		client:
      			url: http://localhost:8769 
      			
    management:
      endpoints:
        web:
          exposure:
            include: "*" #暴露所有节点
        health:
          sensitive: false #关闭过滤敏感信息
      endpoint:
        health:
          show-details: ALWAYS  #显示详细信息

四、结合Eureka注册中心

  1. 添加Maven依赖

    注册中心:

    <dependency>    
        <groupId>org.springframework.cloudgroupId>   
        <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId> 
    dependency>

    客户端(监控中心与业务服务):

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        <version>2.1.3.RELEASEversion>
    dependency>
  2. 配置

    注册中心:

    spring:
      application:
      	name: eureka-server 
    server:
      port: 8761
    #注册中心
    eureka:
      instance:
        hostname: localhost
      client:
        registerWithEureka: false
        fetchRegistry: false
        healthcheck:
          enabled: true
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    #健康数据
    management:
      endpoints:
        web:
          exposure:
            include: "*" #暴露所有节点
        health:
          sensitive: false #关闭过滤敏感信息
      endpoint:
        health:
          show-details: ALWAYS  #显示详细信息

    admin监控中心:

    server:
      port: 8000
    
    spring:
      application:
        name: admin-server
        
    #注册中心注册
    eureka:
      client:
      	registryFetchIntervalSeconds: 5
        service-url:
        	defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
      instance:
      	leaseRenewalIntervalInSeconds: 10
        #健康检查路径
        health-check-url-path: /actuator/health
        
    management:
      endpoints:
      	web:
      	  exposure:
      	  	include: *
        health:
        	show-details: ALWAYS

    业务服务:

    spring:
      application:
        name: admin-client 
    server:
      port: 8080 
      
    #注册中心注册 
    eureka:
      client:
      	registryFetchIntervalSeconds: 5
        service-url:
        	defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
      instance:
      	leaseRenewalIntervalInSeconds: 10
        #健康检查路径
        health-check-url-path: /actuator/health
        
    management:
      endpoints:
      	web:
      	  exposure:
      	  	include: *
        health:
        	show-details: ALWAYS

五、集成spring security

  1. Admin监控中心添加依赖

    <dependency>    
        <groupId>org.springframework.bootgroupId>    
        <artifactId>spring-boot-starter-securityartifactId> 
    dependency>
  2. Admin监控中心 添加配置

    #安全配置 
    spring:
      security:
      	user:
      	  name: admin 
          password: admin 
    
    eureka:
      instance:
      	metadata-map:
      	  user:
      	  	name: ${spring.security.user.name} 
            password: ${spring.security.user.password}
  3. Admin监控中心添加配置类

    @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 {
            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();
        }
    }

六、集成Email

  1. Admin监控中心添加依赖

    <dependency>    
        <groupId>org.springframework.bootgroupId>    
        <artifactId>spring-boot-starter-mailartifactId> 
    dependency>
  2. Admin监控中心配置

    #配置邮件 
    spring:
      mail:
      	host: smtp.163.com 
        username: XXXX 
        password: XXXX 
    #当我们已注册的客户端的状态从 UP 变为 OFFLINE 或其他状态
    #服务端就会自动将电子邮件发送到[email protected]
      boot:
      	admin:
      	  notify:
      	  	mail:
      	  	  to: [email protected]

七、最终配置

  1. Admin监控中心

    server:
      port: 8000
    
    spring:
      application:
        name: admin-server
    
      # 安全配置
      security:
        user:
          name: admin
          password: admin
    
      #配置邮件
      #(当我们已注册的客户端的状态从 UP 变为 OFFLINE 或其他状态,服务端就会自动将电子邮件发送到[email protected]。)
      mail:
        host: smtp.qq.com
        username: ********@qq.com
        password: ********@
      boot:
        admin:
          notify:
            mail:
              #从那里发
              from: ********@@qq.com
              #发到哪里
              to: ********@@qq.com
              #配置是否启用邮件通知 false是不起用
              enabled: true
    
    eureka:
      instance:
        hostname: 127.0.0.1
        metadata-map:
          user:
            name: ${spring.security.user.name}
            password: ${spring.security.user.password}
      client:
        registerWithEureka: true
        fetchRegistry: true
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:8761/eureka/
  2. eureka注册中心

    server:
      port: 8761
    eureka:
      instance:
        hostname: localhost
      client:
        registerWithEureka: false
        fetchRegistry: false
        healthcheck:
          enabled: true
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    
    #admin服务治理
    management:
      endpoints:
        web:
          exposure:
            include: "*" #暴露所有节点
        health:
          sensitive: false #关闭过滤敏感信息
      endpoint:
        health:
          show-details: ALWAYS  #显示详细信息
  3. eureka客户端

    server:
      port: 8087
    
    spring:
      application:
        name: client
    mybatis:
      #configuration:
      #不打印所有日志
      #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      type-aliases-package: com.wjs.domain
      mapper-locations: classpath*:mapper/*.xml
    
    # 打印sql error
    logging:
      level:
        com.wjs.expert.mapper: debug
    
    #pagehelper分页插件
    pagehelper:
      helperDialect: mysql
      reasonable: true
      supportMethodsArguments: true
      params: count=countSql
    
    eureka:
      instance:
        hostname: 127.0.0.1
      client:
        registerWithEureka: true
        fetchRegistry: true
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:8761/eureka/
    
    #admin服务治理
    management:
      endpoints:
        web:
          exposure:
            include: "*" #暴露所有节点
        health:
          sensitive: false #关闭过滤敏感信息
      endpoint:
        health:
          show-details: ALWAYS  #显示详细信息

你可能感兴趣的:(Spring-Cloud Admin详细操作流程和简单介绍)