spring boot admin

spring boot admin:用来监控spring boot服务。
原理:把spring boot actuator中endpoint暴露出来的json数据进行可视化显示到界面上。
可以通过Eureka监控注册到其中的服务。

demo使用spring boot2.1版本:https://github.com/tinazhao1985/spring-cloud-demo/tree/master/spring-boot-admin

Eureka Server:
maven:

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

yml:
server:
  port: 8081

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类:
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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


Admin Server:
maven:

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


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


    de.codecentric
    spring-boot-admin-starter-server


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


     org.jolokia
     jolokia-core

yml:
spring:
  application:
    name: admin-server
  profiles:
    active:
      - secure
server:
  port: 8082

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8081}/eureka/

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

---
spring:
  profiles: insecure

---
spring:
  profiles: secure
  security:
    user:
      name: "admin"
      password: "admin"
eureka:
  instance:
    metadata-map:
      user.name: "admin"
      user.password: "admin"

启动类:
@Configuration
@EnableAutoConfiguration
@EnableAdminServer // 开启监控功能
@EnableEurekaClient // 把 Spring Boot Admin注册到Eureka里,就可以发现注册到Eureka里的其他服务实例
public class AdminServerApplication {

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

    @Profile("insecure")
    @Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            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 {
            // @formatter:off
            SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
            successHandler.setTargetUrlParameter("redirectTo");

            http.authorizeRequests()
                    .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
        }
    }
}

Service Demo:
maven:

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


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


    org.springframework.boot
    spring-boot-starter-actuator

yml:
spring:
  application:
    name: service-demo

server:
  port: 8083

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/,http://localhost:8082/eureka/

management:
  endpoints:
    web:
      exposure:
        include: '*'

启动类:
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceDemoApplication {

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

 

依次启动Eureka Server,Admin Server,Service Demo,在浏览器中打开admin UI:http://localhost:8082/login

spring boot admin_第1张图片

登陆后可以看到有两个服务被监控:(admin服务本身和一个demo服务)

spring boot admin_第2张图片

进入demo服务,可以看到该服务的线程、JVM等实时信息。

spring boot admin_第3张图片

你可能感兴趣的:(micro,service)