【问题标题】:Using Spring Cloud Kubernetes, how do I get the instanceId of the running instance使用 Spring Cloud Kubernetes,如何获取正在运行的实例的 instanceId
【发布时间】:2021-11-23 08:13:29
【问题描述】:

我有一个用 Spring Boot 编写并利用在 Kubernetes 中运行的 Spring Cloud Kubernetes 的微服务,如下所示:

  • Kubernetes 服务 (foo-service)
  • foo-service(pod)的两个实例位于服务后面,每个实例都运行 Spring Boot 应用程序

我可以使用 Spring Cloud Kubernetes DiscoveryClient 列出服务的所有个实例,如下所示:

List<ServiceInstance> allInstances = discoveryClient.getInstances("foo-service")

如何获取正在执行此代码的实例的 instanceId?

【问题讨论】:

  • 我对“当前正在运行的实例”有点困惑……你能详细说明一下吗?
  • 我正在寻找正在执行此代码的foo-service 实例的标识符。
  • 您的意思是执行某些代码的实际 pod?对不起,但这对你有什么影响?

标签: spring-boot kubernetes spring-cloud-kubernetes


【解决方案1】:

instance name 为 Pod 的名字,pod 的名字与 hostname 相同。

所以例如使用

InetAddress.getLocalHost().getHostName()

【讨论】:

  • 不是问题:如何得到它这个名字,实际上,代码在哪里执行?但公平地说,我也对这个问题感到困惑
猜你喜欢
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
  • 2022-11-11
  • 1970-01-01
  • 2021-01-21
  • 2022-08-23
相关资源
最近更新 更多