gpt4 book ai didi

cloud-foundry - Cloud Foundry 应用程序正在运行的实例数

转载 作者:行者123 更新时间:2023-12-04 03:56:20 26 4
gpt4 key购买 nike

我的应用程序需要获取其运行的实例数(在运行时,我的应用程序在我的程序逻辑中使用此信息)。(VCAP_APPLICATION 环境变量无法提供此信息。)直接调用 API 并使用“实例”属性是一种选择,但我不知道如何在我的应用程序中直接调用应用程序 API。请让我知道如何调用它。以下是我获得的应用 API 链接: http://apidocs.cloudfoundry.org/218/apps/retrieve_a_particular_app.html

最佳答案

为了使用 API,您首先需要进行身份验证。为了进行身份验证,您需要检索授权端点。

要检索授权端点,请发出以下 curl 请求(我在此示例中使用关键 Web 服务,但您可以将 https://api.run.pivotal.io 替换为您正在使用的 Cloud Foundry 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 值,在本例中为:

https://login.run.pivotal.io

您现在需要获取一个身份验证 token 。发出以下 curl 命令,将 [my user name][my password][my authorization endpoint] 替换为您的值。请注意,您应该对您的密码进行 url 编码。

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 端点,您需要应用程序的 guid。相反,我建议使用 List all Apps 端点并在其上使用查询过滤器来获取您的应用信息。这是 curl 命令(将 [my authentication token] 替换为上一步中的身份验证 token ,将 [my api endpoint] 替换为您用于云代工的 api 端点, 将 [my app name] 替换为您的应用名称:

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 以在该调用中使用。

希望有帮助!

关于cloud-foundry - Cloud Foundry 应用程序正在运行的实例数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32868377/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com