代码之家  ›  专栏  ›  技术社区  ›  Domo

CloudFoundry中的多个服务实例之间的Spring引导管理不能有所不同

  •  0
  • Domo  · 技术社区  · 7 年前

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

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

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

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

    1 回复  |  直到 7 年前
        1
  •  0
  •   Domo    7 年前

    显然,您必须在客户机的启动/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()
    }
    
    推荐文章