1. Spring Cloud 服务发现

Spring Boot Admin服务端可以使用Spring Clouds 的 DiscoveryClient 来发现服务。 其优点是客户端不必依赖 spring-boot-admin-starter-client 了。 你只需在admin服务端添加一个 DiscoveryClient 的实现 - 其它一切都会自动配置完成。

1.1. 使用SimpleDiscoveryClient进行静态配置

Spring Cloud提供了一个 SimpleDiscoveryClient。它允许我们通过静态配置的方式指定客户端应用:

pom.xml
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter</artifactId>
</dependency>
application.yml
spring:
  cloud:
    discovery:
      client:
        simple:
          instances:
            test:
              - uri: http://instance1.intern:8080
                metadata:
                  management.context-path: /actuator
              - uri: http://instance2.intern:8080
                metadata:
                  management.context-path: /actuator

1.2. 其它方式的服务发现客户端

Spring Boot Admin同时也支持其它Spring Cloud DiscoveryClient 的实现(比如Eureka, Zookeeper, Consul, …​)。 只需要将他们添加到Spring Boot Admin服务端并正确配置即可。 这里有一份 使用Eureka作为实现 的示例。

1.3. 转换服务实例

服务的注册信息中是通过 ServiceInstanceConverter 来进行转换的。Spring Boot Admin提供了一个默认转换方式,还有一个Eureka转换方式的实现。 这些都是自动配置并选择的。

你也可以通过SBA服务端配置选项,来修改应用注册时使用哪些信息以及哪些实例元数据。元数据的值会优先于服务端的配置。 如果这么多选项仍然满足不了您的需求,那么您也可以自定义一个`ServiceInstanceConverter`。
在使用Eureka的时候,healthCheckUrl`选项会被Eureka用在健康检查上, 它也可以通过 `eureka.instance.healthCheckUrl 属性配置在您的客户端上。
表格 1. 实例支持的元数据选项
Key Value 默认值

user.name
user.password

访问接口所使用的凭证。

management.scheme

服务URL中使用的scheme,用于访问actuator接口。

management.address

服务URL中使用的address,用于访问actuator接口。

management.port

服务URL中使用的port,用于访问actuator接口。

management.context-path

服务URL后添加的上下文路径,用于访问actuator接口。

${spring.boot.admin.discovery.converter.management-context-path}

health.path

服务URL后添加的路径,用于健康检查。如果配置了 EurekaServiceInstanceConverter 则会被忽略。

${spring.boot.admin.discovery.converter.health-endpoint}

表格 2. 服务发现配置可选项
属性名 说明 默认值

spring.boot.admin.discovery.enabled

开启admin服务端的DiscoveryClient支持。

true

spring.boot.admin.discovery.converter.management-context-path

当management-url被 DefaultServiceInstanceConverter 转换时,这个路径会拼接到发现服务的service-url后面。

/actuator

spring.boot.admin.discovery.converter.health-endpoint-path

当health-url被 DefaultServiceInstanceConverter 转换时,这个路径会拼接到发现服务的management-url后面。

"health"

spring.boot.admin.discovery.ignored-services

在发现服务的时候,这些服务会被忽略,不会作为服务注册到服务端。支持一些简单的表达式(比如 "foo*", "*bar", "foo*bar*")。

spring.boot.admin.discovery.services

在发现服务的时候,这些服务会被包含进来,作为服务注册到服务端。支持一些简单的表达式(比如 "foo*", "*bar", "foo*bar*")。

"*"

spring.boot.admin.discovery.ignored-instances-metadata

如果服务实例的元数据被列表匹配到至少一项,那么这个服务就会被忽略。 (例如 "discoverable=false")

spring.boot.admin.discovery.instances-metadata

如果服务实例的元数据被列表匹配到至少一项,那么这个服务就会被引入进来。 (例如 "discoverable=false")

1.4. CloudFoundry

如果您想将应用部署到CloudFoundry上,那么就 必须 配置 vcap.application.application_id 以及 vcap.application.instance_index 这两个选项,它们会被添加到元数据中,这样才能正确注册到Spring Boot Admin服务端。 下面是一个针对Eureka的简单配置:

application.yml
eureka:
  instance:
    hostname: ${vcap.application.uris[0]}
    nonSecurePort: 80
    metadata-map:
      applicationId: ${vcap.application.application_id}
      instanceId: ${vcap.application.instance_index}