86. 发现

这是一个带有 Cloud Foundry 发现的 Spring Cloud 应用程序:

app.groovy.

@Grab('org.springframework.cloud:spring-cloud-cloudfoundry')
@RestController
@EnableDiscoveryClient
class Application {

  @Autowired
  DiscoveryClient client

  @RequestMapping('/')
  String home() {
    'Hello from ' + client.getLocalServiceInstance()
  }

}

如果您在没有任何服务绑定的情况下运行它:

$ spring jar app.jar app.groovy
$ cf push -p app.jar

它将在主页中显示其 app name。

DiscoveryClient可以根据通过身份验证的凭据列出空间中的所有应用程序,其中空间默认为 client 正在运行的应用程序(如果有)。如果既未配置 org 也未配置空间,则它们默认为 Cloud Foundry 中用户的 profile。