1. 设置Spring Boot Admin Server

首先要设置你的服务端。为此可以(通过start.spring.io)生成一个简单的boot项目。由于Spring Boot Admin Server可作为servlet或者webflux应用运行,所以需要增加相应的Spring Boot Starter。在本示例中使用servlet web starter。
1.在依赖中增加Spring Boot Admin Server starter
pom.xml

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.0.4</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.使用@EnableAdminServer开启Spring Boot Admin Server配置

@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminApplication.class, args);
    }
}

也可参见spring-boot-admin-sample-servlet项目为给程序加密。

2. 注册客户端应用

注册应用到SBA服务端,可使用SBA客户端或者使用Spring Cloud Discovery(Eureka、Consul等)。在SBA服务端使用静态配置是个简单的选择。

2.1. Spring Boot Admin 客户端

每个要注册的应用必须有Spring Boot Admin客户端。为了保证站点安全也可增加spring-boot-starter-security

1.增加spring-boot-admin-starter-client依赖
pom.xml

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.0.4</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2.通过配置Spring Boot Admin服务端的URL来开启SBA客户端
application.properties

spring.boot.admin.client.url=http://localhost:8080  ❶
management.endpoints.web.exposure.include=* ❷

❶ Spring Boot Admin服务端注册的URL
❷ 由于大部分端口Spring Boot 2是默认不开启的,需要手动全部开启。在生产环境中需要慎重开放这些端口。

3.开启actuator端点:

@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll() ❶ 
            .and().csrf().disable();
    }
}

❶ 为了简洁暂时先关闭加密。可查看security section开启加密端点。

2.2. Spring Cloud Discovery

如果应用程序中已经使用了Spring Cloud Discovery则不必使用SBA客户端。只要在Spring Boot Admin服务端增加DiscoveryClient即可,其他设置将自动配置。
下面的步骤使用Eureka,但其他的Spring Cloud Discovery也是支持的。下面是的ConsulZookeeper例子。
1.增加spring-cloud-starter-eureka依赖
pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.增加@EnableDiscoveryClient开启服务发现

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class SpringBootAdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminApplication.class, args);
    }

    @Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().anyRequest().permitAll() ❶ 
                .and().csrf().disable();
        }
    }
}

❶ 为了简洁暂时先关闭加密。可查看security section开启加密端点。
3.配置服务发现客户侧的配置
application.yml

eureka:  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #needed to trigger info and endpoint update after restart
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/

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

❶ Eureka 客户端配置
❷ 由于大部分端口Spring Boot 2是默认不开启的,需要手动全部开启。在生产环境中需要慎重开放这些端口。

© 2020 haozi. All rights reserved.

results matching ""

    No results matching ""