我的应用程序需要获取其运行的实例数量(在运行时我的应用程序在我的程序逻辑中使用此信息)。 (VCAP_APPLICATION env变量无法提供此信息。)直接调用API并使用“实例”属性是一个选项,但我不知道如何直接在我的应用程序中调用应用程序API。请告诉我怎么称呼它。以下是我为app API获得的链接:http://apidocs.cloudfoundry.org/218/apps/retrieve_a_particular_app.html
要使用API,首先需要进行身份验证。要进行身份验证,您需要检索授权端点。
要检索授权端点,请发出以下curl请求(我在此示例中使用了关键Web服务,但您可以将https://api.run.pivotal.io替换为您正在使用的云代工api端点。
curl -H 'content-type: application/x-www-form-urlencoded;charset=utf-8' \
-H 'accept: application/json;charset=utf-8' \
https://api.run.pivotal.io/v2/info
你会得到一些看起来像这样的东西:
{
"name": "vcap",
"build": "2222",
"support": "http://support.cloudfoundry.com",
"version": 2,
"description": "Cloud Foundry sponsored by Pivotal",
"authorization_endpoint": "https://login.run.pivotal.io",
"token_endpoint": "https://uaa.run.pivotal.io",
"min_cli_version": null,
"min_recommended_cli_version": null,
"api_version": "2.36.0",
"app_ssh_endpoint": "ssh.run.pivotal.io:2222",
"app_ssh_host_key_fingerprint": "e7:13:4e:32:ee:39:62:df:54:41:d7:f7:8b:b2:a7:6b",
"logging_endpoint": "wss://loggregator.run.pivotal.io:443",
"doppler_logging_endpoint": "wss://doppler.run.pivotal.io:443"
}
获取authorization_endpoint值,在这种情况下它是:
您现在需要获取身份验证令牌。发出以下curl命令,用您的值替换[我的用户名]和[我的密码]和[我的授权端点]。请注意,您应该对您的密码进行网址编码。
curl -H 'content-type: application/x-www-form-urlencoded;charset=utf-8' \
-H 'accept: application/json;charset=utf-8' \
-H 'authorization: Basic Y2Y6' \
-d "username=[my user name]&password=[my password]&grant_type=password" \
[my authorization endpoint]/oauth/token
您将得到如下响应:
{
"access_token": "very_long_token.very_long_token.very_long_token",
"token_type": "bearer",
"refresh_token": "very_long_token.very_long_token.very_long_token",
"expires_in": 599,
"scope": "cloud_controller.read password.write cloud_controller.write openid",
"jti": "shorter_value"
}
您对access_token值感兴趣(access_token,refresh_token和jti已从此示例中的实际值更改)
现在我们终于可以使用api来获取有关我们应用的信息。您可以使用上面提供的链接,但要使用该api端点,您需要应用程序的指南。相反,我建议使用List all Apps端点并在其上使用查询过滤器来获取您的应用信息。这是curl命令(将[my authorization token]替换为上一步中的auth令牌,将[my api endpoint]替换为您用于云代工的api端点,将[我的应用名称]替换为您的应用名称:
curl -H "authorization: bearer [my authorization token]" \
[my api endpoint]/v2/apps?q=name:[my app name] -X GET
您将收到如下所示的消息:
{
"total_results": 1,
"total_pages": 1,
"prev_url": null,
"next_url": null,
"resources": [
{
"metadata": {
"guid": "blah-blah",
"url": "/v2/apps/blah-blah",
"created_at": "time_stamp",
"updated_at": null
},
"entity": {
"name": "my-app",
"production": false,
"space_guid": "blah-blah",
"stack_guid": "blah-blah",
"buildpack": null,
"detected_buildpack": null,
"environment_json": {
},
"memory": 1024,
"instances": 3,
"disk_quota": 1024,
"state": "STOPPED",
"version": "blah-blah",
"command": null,
"console": false,
"debug": null,
"staging_task_id": null,
"package_state": "STAGED",
"health_check_type": "port",
"health_check_timeout": null,
"staging_failed_reason": null,
"staging_failed_description": null,
"diego": false,
"docker_image": null,
"package_updated_at": "time stamp",
"detected_start_command": "",
"enable_ssh": true,
"docker_credentials_json": {
"redacted_message": "[PRIVATE DATA HIDDEN]"
},
"space_url": "/v2/spaces/blah-blah",
"stack_url": "/v2/stacks/blah-blah",
"events_url": "/v2/apps/blah-blah/events",
"service_bindings_url": "/v2/apps/blah-blah/service_bindings",
"routes_url": "/v2/apps/blah-blah/routes"
}
}
]
}
您可以从邮件中获取实例。如果要在原始链接中使用api,可以获取metadata.guid以在该调用中使用。
希望有所帮助!
我正在寻找类似的东西,但使用cf cli,在shell脚本中获取我的应用程序的实例数。我想出了这个并且它有效。
cf app my_app_name|grep instances|cut -d'/' -f2
我理解这篇文章可能没有关系,但如果有人帮助我会很高兴。