Spring Boot Admin在Cloudfoundry中的多个服务实例之间没有区别。



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

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

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

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

显然,您必须在客户端的启动/上下文刷新时将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()
}

最新更新