java 使用 Eureka 作为使用 REST API 的注册表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33705517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:01:20  来源:igfitidea点击:

Using Eureka as a registry using REST APIs

javaspringrestspring-cloudnetflix-eureka

提问by Bloodysock

We have been using Eureka with our Spring Boot applications for few months now. We have enabled service lookup between applications using @DiscoveryClientannotations. The registrations, lease renewals and deregistration works as expected.

几个月来,我们一直在使用 Eureka 和 Spring Boot 应用程序。我们使用@DiscoveryClient注解启用了应用程序之间的服务查找。注册、续租和注销按预期进行。

Recently, we have encountered a scenario where we have non-Java application component (written in C++), which is exposes 3 REST service endpoints that many of our Spring Boot Java applications would use. We are trying to see if the C++ component can make use of the Eureka server's REST APIs to register itself when it came up, so that the Spring Boot Java applications can perform the usual lookup via Eureka to get in touch with the C++ component.

最近,我们遇到了一个场景,我们有非 Java 应用程序组件(用 C++ 编写),它暴露了 3 个 REST 服务端点,我们的许多 Spring Boot Java 应用程序都会使用这些端点。我们正在尝试查看 C++ 组件是否可以利用 Eureka 服务器的 REST API 在它出现时注册自己,以便 Spring Boot Java 应用程序可以通过 Eureka 执行通常的查找以与 C++ 组件取得联系。

Since I cannot use the Eureka Client in the C++ components (obviously), I started testing direct REST APIs (as described here) using Postman. The registration worked without any problems by sending a JSON payload using POST method to http://eurekaserver:8761/eureka/apps/FOO-APP(with instanceId = 1111 and hostName = foo-app). I can query http://eurekaserver:8761/eureka/appsand can see FOO-APP listed there as expected.

既然不能在C ++组件(显然)使用尤里卡客户端,我开始测试直接的REST API(如描述在这里使用邮差)。通过使用 POST 方法将 JSON 有效负载发送到http://eurekaserver:8761/eureka/apps/FOO-APP(instanceId = 1111 和 hostName = foo-app),注册工作没有任何问题。我可以查询http://eurekaserver:8761/eureka/apps并且可以看到按预期列出的 FOO-APP。

However, when I try the cancel operation using DELETE method to http://eurekaserver:8761/eureka/apps/FOO-APP/1111or http://eurekaserver:8761/eureka/apps/FOO-APP/foo-app, I get a 404 error.

但是,当我尝试使用 DELETE 方法对http://eurekaserver:8761/eureka/apps/FOO-APP/1111http://eurekaserver:8761/eureka/apps/FOO-APP/foo-app进行取消操作时,我收到 404 错误。

With instanceId:

使用实例 ID:

{
  "timestamp": 1447479397996,
  "status": 404,
  "error": "Not Found",
  "message": "Not Found",
  "path": "/eureka/apps/FOO-APP/1111"
}

OR (same outcome for hostName):

或(主机名的结果相同):

{
  "timestamp": 1447479397996,
  "status": 404,
  "error": "Not Found",
  "message": "Not Found",
  "path": "/eureka/apps/FOO-APP/foo-app"
}

I tried different combinations, but I am not able to make this work. I have a feeling I am missing something obvious - may be something small. Any help on this would be appreciated.

我尝试了不同的组合,但我无法完成这项工作。我有一种感觉,我错过了一些明显的东西——可能是一些小东西。对此的任何帮助将不胜感激。

PS:Eureka REST endpoint documentation mentions "v2" in the URL. However, that does not work in my case. Registration (which works for me) does not use "v2" as described above. If someone could validate this, that would be helpful as well. There just doesn't seem to be enough material on this.

PS:Eureka REST 端点文档在 URL 中提到了“v2”。但是,这在我的情况下不起作用。如上所述,注册(对我有用)不使用“v2”。如果有人可以验证这一点,那也会有帮助。似乎没有足够的材料来说明这一点。

回答by Bloodysock

Finally, I have figured out how the canceloperation can be invoked using REST URLs of a Eureka server. This works for Spring Cloud Eureka server, but should also work for the Netflix Eureka server.

最后,我弄清楚了如何cancel使用 Eureka 服务器的 REST URL 调用操作。这适用于 Spring Cloud Eureka 服务器,但也适用于 Netflix Eureka 服务器。

The URL pattern for the canceloperation is as the following:

操作的 URL 模式cancel如下:

DELETE http://eureka_host:eureka_port/eureka/apps/<appName>/<instanceId>

This is how it is documented at the Eureka REST operationspage, but there's very little clarity as to what <instanceId>was supposed to be. As per the documentation, <instanceId>is the hostnameof the host that runs the Eureka client. That did notwork (IP address or host name). I tried passing in the same value that the GETURL gave me (for example, 192.168.55.55) or localhost. That did not work either. I also tried passing in the instanceIdvalue from the GEToutput (which would be the same as the value of eureka.instance.metadataMap.instanceId property). That too, didn't work. I literally had to try different combinations to find this out. The <instanceId>is the concatenation of the hostname and instance ID, separated by :. For example, 192.168.55.55:foo-app-some-random-str.

这就是它在Eureka REST 操作页面上的记录方式,但是对于<instanceId>应该是什么并不清楚。根据文档,<instanceId>是运行 Eureka 客户端的主机的主机名。这并没有工作(IP地址或主机名)。我尝试传入GETURL 给我的相同值(例如,192.168.55.55)或localhost. 那也没有用。我还尝试instanceIdGET输出中传入值(这与 eureka.instance.metadataMap.instanceId 属性的值相同)。那也是行不通的。我真的不得不尝试不同的组合才能找到答案。的<instanceId>是主机名和实例ID的并置,通过分离:. 例如,192.168.55.55:foo-app-some-random-str

Here's an example output of the GEToperation listing the active instance registered with Eureka:

这是GET列出在 Eureka 注册的活动实例的操作的示例输出:

<instance>
  <hostName>192.168.55.55</hostName>
  <app>FOO-APP</app>
  ...
  <metadata>
    <instanceId>foo-app-f4ea7b06fc03a05a06900713f7526a5d</instanceId>
  </metadata>
  ...
</instance>

In this case, the cancelcURL command would look like this:

在这种情况下,cancelcURL 命令将如下所示:

$ curl -X "DELETE" http://eureka_host:eureka_port/eureka/apps/FOO-APP/192.168.55.55:foo-app-f4ea7b06fc03a05a06900713f7526a5d

That would de-register the instance as expected.

这将按预期取消注册实例。

That said,I must confess that I wasn't paying much attention to the Eureka server logs. When you register the Eureka client, the log printed out the fully qualified name of the instance (FOO-APP/192.168.55.55:foo-app-f4ea7b06fc03a05a06900713f7526a5d), which I could have used as my guess.

也就是说,我必须承认我并没有太注意 Eureka 服务器日志。当您注册 Eureka 客户端时,日志会打印出实例的完全限定名称 ( FOO-APP/192.168.55.55:foo-app-f4ea7b06fc03a05a06900713f7526a5d),我可以将其用作我的猜测。

I hope someone fixes this in the Eureka documentation.

我希望有人在Eureka 文档中修复了这个问题。