Cloud Foundry应用程序正在运行的实例数 [英] number of instances the cloud foundry app is running

查看:87
本文介绍了Cloud Foundry应用程序正在运行的实例数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

my app needs to get the number of instances in which its running, (in runtime my app uses this info in my program logic). (VCAP_APPLICATION env variables cannot provide this info. ) Calling API directly and using "instances" attribute is an option, but I dont know how to call apps API directly in my app. Please let me know how to call it. Below is the link I got for app API: http://apidocs.cloudfoundry.org/218/apps/retrieve_a_particular_app.html

推荐答案

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

In order to use the API, you first need to authenticate. In order to authenticate, you need to retrieve the authorization endpoint.

要检索授权端点,请发出以下curl请求(在此示例中,我使用的是关键Web服务,但是您可以将 https://api.run.pivotal.io 替换为您所使用的Cloud Foundry API端点

To retrieve the authorization endpoint, issue the following curl request (I am using pivotal web services in this example, but you would replace https://api.run.pivotal.io with the cloud foundry api endpoint you are using.

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 值,在这种情况下为:

Grab the authorization_endpoint value, in this case it is:

https://login.run.pivotal.io

您现在需要获取身份验证令牌。发出以下curl命令,用您的值替换 [我的用户名] [我的密码] [我的授权端点] 。请注意,您应该对密码进行url编码。

You now need to grab an authentication token. Issue the following curl command replacing the [my user name] and [my password] and [my authorization endpoint] with your values. Please note that you should url encode your password.

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

您将收到如下所示的响应:

You will get a response that looks like this:

{
"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已从实际值更改)

You are interested in the access_token value (access_token, refresh_token, and jti have been changed from the actual values in this example)

N现在我们终于可以使用api来获取有关我们应用程序的信息了。您可以使用上面提供的链接,但是要使用该api端点,您需要应用程序的guid。相反,我建议使用列出所有应用端点,并在其上使用查询过滤器以获取您的应用信息。这是curl命令(将 [我的授权令牌] 替换为上一步中的身份验证令牌,将 [我的api端点] 替换为用于Cloud Foundry的api端点,将 [我的应用名称] 替换为您的应用名称:

Now we are finally at the point where we can use the api to get the information about our app. You could use the link you provided above, but to use that api endpoint, you need the guid of your app. Instead I would recommend using the List all Apps endpoint and use a query filter on it to get your app information. Here is the curl command (replace [my authorization token] with your auth token from the previous step, replace [my api endpoint] with the api endpoint you use for cloud foundry, replace [my app name] with the name of your app:

curl -H "authorization: bearer [my authorization token]" \
[my api endpoint]/v2/apps?q=name:[my app name] -X GET

您将收到一条如下所示的消息:

You'll receive a message that looks like this:

{
"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。

You can grab the instances from the message. If you want to use the api in your original link, you can grab the metadata.guid for use in that call.

希望有帮助!

这篇关于Cloud Foundry应用程序正在运行的实例数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆