Spring Cloudfoundry 中的多个服务实例之间的 Boot Admin 不能不同

Spring Boot Admin can't differ between multiple service instances in Cloudfoundry

我通过 Eureka Service Discovery 在本地获得了 Spring Boot Admin 运行(客户端中没有 SBA 依赖)。现在我尝试在 Cloudfoundry 中部署它。根据文档,版本 2.0.1 应该 "support CloudFoundry out of the box".

我的问题是,当我将服务扩展到多个实例时,它们都在相同的主机名和端口下注册。 Eureka 向我显示了所有实例以及我这样配置的 InstanceID:

eureka:
  instance:
    instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}

但是Spring Boot Admin 只列出了一个以hostname:port 作为标识符的实例。我想我必须在客户端上配置一些东西,以便它在注册时发送每个 HTTP Header 的实例 ID。但是我不知道怎么办。

显然,您必须将 Cloudfoundry 生成的 ApplicationId 和 InstanceIndex 设置为客户端 Startup/ContextRefresh 处的 Eureka ApplicationId 和 InstanceId。

CloudFoundryApplicationInitializer.kt

@Component
@Profile("cloud")
@EnableConfigurationProperties(CloudFoundryApplicationProperties::class)
class CloudFoundryApplicationInitializer {

private val log = LoggerFactory.getLogger(CloudFoundryApplicationInitializer::class.java)

@Autowired
private val applicationInfoManager: ApplicationInfoManager? = null

@Autowired
private val cloudFoundryApplicationProperties: CloudFoundryApplicationProperties? = null

@EventListener
fun onRefreshScopeRefreshed(event: RefreshScopeRefreshedEvent) {
    injectCfMetadata()
}

@PostConstruct
fun onPostConstruct() {
    injectCfMetadata()
}

fun injectCfMetadata() {

    if(this.cloudFoundryApplicationProperties == null) {
        log.error("Cloudfoundry Properties not set")
        return
    }

    if(this.applicationInfoManager == null) {
        log.error("ApplicationInfoManager is null")
        return
    }

    val map = applicationInfoManager.info.metadata
    map.put("applicationId", this.cloudFoundryApplicationProperties.applicationId)
    map.put("instanceId", this.cloudFoundryApplicationProperties.instanceIndex)

    }
}

CloudFoundryApplicationProperties.kt

@ConfigurationProperties("vcap.application")
class CloudFoundryApplicationProperties {
    var applicationId: String? = null
    var instanceIndex: String? = null
    var uris: List<String> = ArrayList()
}