使用Spring Boot Admin监控Sprint Cloud应用

阅读更多

一、创建服务配置中心应用

       注册中心Eureka Server

       注册中心添加用户认证

 

二、创建Spring Boot Admin Server应用

      Admin Server将自身注册到服务注册中心,这样Admin Server就可以通过注册中心获取所有应用,并自动监控这样应用。

1、创建pom.xml文件



  4.0.0

  com.seasy
  spring-boot-admin-server
  1.0.0
  jar
  
  
  	org.springframework.boot
  	spring-boot-starter-parent
  	2.0.8.RELEASE
  	
  

  
    UTF-8
    UTF-8
    1.8
  

  
	
  	
    	de.codecentric
    	spring-boot-admin-starter-server
    	2.0.5
	
	
    
    	org.springframework.boot
    	spring-boot-starter-security
	
	
	
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    

	
    	org.springframework.boot
    	spring-boot-starter-web
	
  
  
  
    
        
            org.springframework.cloud
            spring-cloud-dependencies
            Finchley.RELEASE
            pom
            import
        
    
  

 

2、创建application.yml配置文件

server:
  port: 3030
  
spring:
  application:
    name: spring-boot-admin-server
  #启用的环境配置
  profiles:
    active:
    - secure

eureka:
  instance:
    hostname: ${spring.cloud.client.ip-address}
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
    #
    lease-renewal-interval-in-seconds: 10  #表示eureka client发送心跳给server端的频率 
    health-check-url-path: /actuator/health  #健康检查页面的URL路径
    #注册给eureka时告诉eureka自己的密码
    metadata-map: 
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}
  client:
    registry-fetch-interval-seconds: 5  #表示eureka client间隔多久去拉取服务注册信息,默认为30秒
    #注册中心地址
    service-url:
      defaultZone:
        http://root:123456@${eureka.instance.hostname}:7001/eureka/

#管理端点的配置
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

#每个环境的配置根据 '---'分隔符隔开      
---
spring:
  profiles: insecure

---
spring:
  profiles: secure
  #登录admin server需要的用户名和密码
  security:
    user:
      name: dev
      password: 123

 

3、启动类

       Spring Security的配置信息也放在启动类中

@SpringBootApplication
@EnableAdminServer //开启监控功能
@EnableEurekaClient //Admin Server也注册到注册中心
public class Main {
	public static void main(String[] args) {
		SpringApplication.run(Main.class, args);
	}
	
	/**
	 * Spring Security配置
	 */
	@Profile("insecure") //对应到application配置文件的spring.profiles属性值
    @Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
        	//permitAll: 无条件允许访问
            http.authorizeRequests().anyRequest().permitAll()
                    .and().csrf().disable();
        }
    }
 
    @Profile("secure")
    @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");
 
            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();
        }
    }
}

 

三、创建Spring Cloud应用

1、创建pom.xml文件

      不需要引入 spring-boot-admin-starter-client 依赖包



  4.0.0
  com.seasy.springcloud
  service-provider-1
  1.0.0
  jar
  
  
  	org.springframework.boot
  	spring-boot-starter-parent
  	2.0.8.RELEASE
  	
  

  
    UTF-8
    UTF-8
    1.8
  
  
  
	
    
        org.springframework.boot
        spring-boot-starter-web
    
    
      	org.springframework.boot
      	spring-boot-starter-actuator
    
    
      	org.springframework.boot
      	spring-boot-starter-test
      	test
    
    
    
    
        org.springframework.cloud
        spring-cloud-starter-netflix-eureka-client
    
  
  
  
    
        
            org.springframework.cloud
            spring-cloud-dependencies
            Finchley.RELEASE
            pom
            import
        
    
  
    
  
      
          
              	org.springframework.boot
              	spring-boot-maven-plugin
          
          
   		  
           		org.apache.maven.plugins
           		maven-compiler-plugin
           		
               		1.8
               		1.8
           		
           
      
  

 

2、创建application.yml配置文件

      不需要配置 spring.boot.admin.client 相关的参数信息

server:
  port: 8001
  
spring:
  application:
    name: service-provider-1

eureka:
  instance:
    hostname: ${spring.cloud.client.ip-address}
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://root:123456@${eureka.instance.hostname}:7001/eureka/

#开启所有的管理端点给Admin Server进行监控
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

 

3、创建启动类

@EnableEurekaClient
@SpringBootApplication
public class Main {
	public static void main(String[] args) {
		SpringApplication.run(Main.class, args);
	}
}

  
使用Spring Boot Admin监控Sprint Cloud应用_第1张图片
 


使用Spring Boot Admin监控Sprint Cloud应用_第2张图片
 

  • 使用Spring Boot Admin监控Sprint Cloud应用_第3张图片
  • 大小: 30.7 KB
  • 使用Spring Boot Admin监控Sprint Cloud应用_第4张图片
  • 大小: 89.1 KB
  • 查看图片附件

你可能感兴趣的:(使用Spring Boot Admin监控Sprint Cloud应用)