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