0

我使用 Eureka Service Discovery(在客户端中没有 SBA 依赖)在本地运行 Spring Boot Admin。现在我尝试将它部署在 Cloudfoundry 中。根据文档,2.0.1 版应该“支持 CloudFoundry 开箱即用”。

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

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

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

4

1 回答 1

0

显然,您必须在客户端的启动/上下文刷新中将 Cloudfoundry 生成的 ApplicationId 和 InstanceIndex 设置为 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()
}
于 2018-07-03T12:52:48.687 回答