我通过Eureka Service Discovery在本地运行了Spring Boot Admin(客户端中没有SBA依赖)。现在,我试图在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只列出一个实例,其主机名:端口为标识符。我认为我必须在客户端上进行一些配置,以便在注册时按HTTP标头发送实例ID。但是我不知道怎么办。
答案 0 :(得分:0)
显然,您必须在客户端的Startup / ContextRefresh上将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()
}