使用REST API将Eureka用作注册表 [英] Using Eureka as a registry using REST APIs

查看:384
本文介绍了使用REST API将Eureka用作注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经将Eureka与我们的Spring Boot应用程序结合使用了几个月.我们已使用@DiscoveryClient批注启用了应用程序之间的服务查找.注册,续约和注销工作按预期进行.

最近,我们遇到了一个非Java应用程序组件(用C ++编写)的场景,该组件公开了许多我们的Spring Boot Java应用程序将使用的3个REST服务端点.我们正在尝试查看C ++组件在启动时是否可以利用Eureka服务器的REST API进行自身注册,以便Spring Boot Java应用程序可以通过Eureka执行常规查找以与C ++组件联系. /p>

由于(显然)我无法在C ++组件中使用Eureka Client,因此我开始测试直接REST API(如此处)使用邮递员.通过使用POST方法将JSON有效负载发送到 http://eurekaserver:8761/,注册就可以顺利进行eureka/apps/FOO-APP (instanceId = 1111,hostName = foo-app).我可以查询 http://eurekaserver:8761/eureka/apps ,并且可以看到FOO-APP列为预期的.

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

具有instanceId:

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

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

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

我尝试了不同的组合,但无法完成这项工作.我觉得我缺少明显的东西-可能很小.任何帮助,将不胜感激.

PS::Eureka REST端点文档在URL中提到了"v2".但是,对于我而言,这不起作用.注册(对我有效)不使用如上所述的"v2".如果有人可以验证这一点,那也将有所帮助.似乎没有足够的材料.

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

cancel操作的URL模式如下:

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

这是在 Eureka REST操作页上记录的方式,但对于<instanceId>应该是什么却几乎一无所知.根据文档,<instanceId>是运行Eureka客户端的主机的主机名.这样无效(IP地址或主机名)无法正常工作.我尝试传递与GET URL给我的值相同的值(例如192.168.55.55)或localhost.那也不起作用.我还尝试从GET输出中传递instanceId值(该值与eureka.instance.metadataMap.instanceId属性的值相同).那也行不通.我实际上必须尝试不同的组合才能找到答案. <instanceId>是主机名和实例ID的串联,以:分隔.例如,192.168.55.55:foo-app-some-random-str.

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

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

在这种情况下,cancel cURL命令如下所示:

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

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

话虽如此,我必须承认我对Eureka服务器日志的关注并不多.当您注册Eureka客户端时,日志会打印出实例的完全限定名称(FOO-APP/192.168.55.55:foo-app-f4ea7b06fc03a05a06900713f7526a5d),我可以将其用作猜测.

我希望有人在 Eureka文档中对此进行修复. >

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

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.

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/apps and can see FOO-APP listed there as expected.

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

With instanceId:

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

解决方案

Finally, I have figured out how the cancel operation 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.

The URL pattern for the cancel operation is as the following:

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

This is how it is documented at the Eureka REST operations page, but there's very little clarity as to what <instanceId> was supposed to be. As per the documentation, <instanceId> is the hostname of the host that runs the Eureka client. That did not work (IP address or host name). I tried passing in the same value that the GET URL gave me (for example, 192.168.55.55) or localhost. That did not work either. I also tried passing in the instanceId value from the GET output (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.

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

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

In this case, the cancel cURL command would look like this:

$ 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.

I hope someone fixes this in the Eureka documentation.

这篇关于使用REST API将Eureka用作注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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