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

Cloud foundry cloud foundry应用程序正在运行的实例数,cloud-foundry,Cloud Foundry,我的应用程序需要获取其运行的实例数(在运行时,我的应用程序在我的程序逻辑中使用此信息)。 (VCAP_应用程序环境变量无法提供此信息。) 直接调用API并使用“实例”属性是一个选项,但我不知道如何在我的应用程序中直接调用应用程序API。请告诉我怎么称呼它。 以下是我获得的应用程序API链接: 要使用API,首先需要进行身份验证。为了进行身份验证,您需要检索授权端点 要检索授权端点,请发出以下curl请求(在本例中,我使用的是pivotal web服务,但您将替换为您正在使用的cloud foun

我的应用程序需要获取其运行的实例数(在运行时,我的应用程序在我的程序逻辑中使用此信息)。 (VCAP_应用程序环境变量无法提供此信息。) 直接调用API并使用“实例”属性是一个选项,但我不知道如何在我的应用程序中直接调用应用程序API。请告诉我怎么称呼它。 以下是我获得的应用程序API链接:
要使用API,首先需要进行身份验证。为了进行身份验证,您需要检索授权端点

要检索授权端点,请发出以下curl请求(在本例中,我使用的是pivotal web服务,但您将替换为您正在使用的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"
}
{
"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"
}
{
"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"
  }
}
]
}
抓取授权\u端点值,在这种情况下,它是:

您现在需要获取身份验证令牌。发出以下curl命令,用您的值替换[我的用户名][我的密码][我的授权端点]。请注意,您应该对密码进行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
您将得到如下响应:

{
"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"
}
{
"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"
}
{
"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"
  }
}
]
}
您对访问令牌值感兴趣(访问令牌、刷新令牌和jti已从本例中的实际值更改)

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

curl -H "authorization: bearer [my authorization token]" \
[my api endpoint]/v2/apps?q=name:[my app name] -X GET
您将收到如下消息:

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

我知道这篇文章可能没有关系,但如果它能帮助别人,我会很高兴的。

尽管现在回复时间已经晚了-另一种解决方案是使用类似Eureka的服务发现组件,并让您的应用程序注册到它。从发现客户端,您可以获得此应用程序的实例数


但请注意,在Eureka上,实例的状态最终会保持一致。我们将此模式用于群集广播用例。

您能否解释一下如何使用此信息?在应用程序中使用此信息并不常见。除了了解实例的数量外,您还必须在发生更改时监控状态s、 我正在对运行在cf中的Spring boot应用程序进行性能测试。现在我需要知道该应用程序正在运行的实例数。我使用它只是为了显示该应用程序正在这么多实例上运行。我在cf dev list论坛上发布了这个问题,并得到回复说:-让该应用程序直接调用API,看看“实例”是什么属性设置为。但我不知道如何调用我的应用程序API。如果我的应用程序还有其他方法可以了解#NoOfInstances,请让我知道。谢谢。你能在性能测试应用程序中使用cf命令行工具吗?请尝试cf应用程序--帮助显示格式。它将返回特定应用程序的实例数。我确信它正在使用上面提到的api。感谢它在命令行中工作。现在,我的应用程序在cf中运行时,需要获取实例数,并且它正在程序逻辑中使用此信息。我应该如何处理我的代码以获取我的应用程序正在运行的实例数。嗯,那么你正在cf中作为应用程序运行性能测试,并点击其他应用程序在cf中运行?如果您分享更多详细信息,我可能会有所帮助。我使用curl来显示流程中使用的基本命令。根据您的编程语言,您可以使用oauth库和json解析来获取所需的信息。实际上,我会建立一个单独的微服务来封装此行为,并提供您的性能测试应用程序调用微服务以获取它所需的信息。我没有像“性能测试”应用程序这样的单独应用程序。我有一个应用程序,它需要获取其在cf中运行的实例数。通常,这个#numberofInstances将由我们在manifest.yml中提供。在命令行中,我可以像您上面所说的那样获取这些信息,但是我的应用程序如何在运行时获取此信息。我的springboot web应用程序通过浏览器访问以显示一些数据,同时我还需要显示#numberofInstances。若要显示此信息,我的程序应首先在运行时获取此信息。使用Cloud Foundry,每个应用程序都在其自己的独立容器中运行。它旨在不受限制其他容器中运行的自身的其他实例。Cloud Foundry确保所需数量的实例正在运行。如果您正在构建一个显示所有正在运行的应用程序及其实例的仪表板,则该仪表板将是一个单独的应用程序,并将使用oauth和json解析从上面的URI中提取信息。我不构建纽约仪表板在我的项目中,那么在这里应该做些什么来完成这项工作。