spring-cloud-cloudfoundry.md 3.6 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1 2 3 4 5 6 7 8
# Spring Cloud for Cloud Foundry

Spring Cloud for CloudFoundry 使得在[Cloud Foundry](https://github.com/cloudfoundry)(平台即服务)中运行[Spring Cloud](https://github.com/spring-cloud)应用程序变得很容易。Cloud Foundry 有一个“服务”的概念,这是一个可以“绑定”到应用程序的中间软件,本质上为它提供了一个包含凭据的环境变量(例如,用于服务的位置和用户名)。

`spring-cloud-cloudfoundry-commons`模块配置了基于反应堆的 Cloud Foundry Java 客户端 V3.0,并且可以独立使用。

`spring-cloud-cloudfoundry-web`项目为 Cloud Foundry 中 WebApps 的一些增强功能提供了基本支持:自动绑定到单点登录服务,并可选地为发现启用粘性路由。

茶陵後's avatar
茶陵後 已提交
9
`spring-cloud-cloudfoundry-discovery`项目提供了 Spring Cloud Commons`DiscoveryClient`的实现,因此你可以`@EnableDiscoveryClient`并提供你的凭据为`spring.cloud.cloudfoundry.discovery.[username,password]`(如果你没有连接到[Pivotal Web 服务](https://run.pivotal.io),也可以`*.url`),然后你可以直接使用`DiscoveryClient`,或者通过`LoadBalancerClient`
茶陵後's avatar
茶陵後 已提交
10 11 12

第一次使用 Discovery 客户机时,由于它必须从 Cloud Foundry 获得一个访问令牌,因此它可能会比较慢。

茶陵後's avatar
茶陵後 已提交
13
## [](#discovery)[1.发现](#discovery)
茶陵後's avatar
茶陵後 已提交
14

茶陵後's avatar
茶陵後 已提交
15
以下是一款带有 Cloud Foundry Discovery 的 Spring Cloud应用:
茶陵後's avatar
茶陵後 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

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
```

它将在主页中显示其应用程序名称。

`DiscoveryClient`可以根据经过身份验证的凭据列出一个空间中的所有应用程序,该空间默认为客户端运行的空间(如果有的话)。如果既不配置组织也不配置空间,那么在 Cloud Foundry 中,它们默认为用户的配置文件。

## [](#single-sign-on)[2.单点登录](#single-sign-on)

茶陵後's avatar
茶陵後 已提交
49
|   |在版本 1.3 中,所有的 OAuth2SSO 和资源服务器功能都移到了 Spring boot<br/>。你可以在[Spring Boot user guide](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/)中找到文档。|
茶陵後's avatar
茶陵後 已提交
50 51
|---|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

茶陵後's avatar
茶陵後 已提交
52
该项目提供了从 CloudFoundry 服务凭据到 Spring 启动特性的自动绑定。例如,如果你有一个名为“SSO”的 CloudFoundry 服务,其凭据包含“client\_id”、“client\_secret”和“auth\_domain”,那么它将自动绑定到你通过`@EnableOAuth2Sso`(从 Spring 启动)启用的 Spring OAuth2 客户端。服务的名称可以使用`spring.oauth2.sso.serviceId`进行参数化。
茶陵後's avatar
茶陵後 已提交
53 54 55 56 57

## [](#configuration)[3.配置](#configuration)

要查看所有 Spring Cloud Foundry 相关配置属性的列表,请检查[附录页](appendix.html)

茶陵後's avatar
茶陵後 已提交
58
如果{{{i[’GoogleAnalyticsObject’]=r;i[r]=i[r]|function(){q=i[r].push(参数)},i[r].l=1\*new date();a=s.createElement(o),m=s.getelementsbyName(0);a.parentsName(1);a.A.SRC=g;m.M.analytnode(gua,m.com.com);(google=document=’,’,’’’’’’’’),’documents’,’’.’’’’’’’’’’’,’’’’’’