从单个主机注册Spring Boot Eureka客户端的多个实例 [英] Register multiple Instances of a Spring Boot Eureka Client from a single host

查看:664
本文介绍了从单个主机注册Spring Boot Eureka客户端的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新

此存储库中的自述文件已更新,以在接受的答案.

The README in this repo has been updated to demonstrate the solution in the accepted answer.

我正在处理一个基于本指南.

如果我启动一个客户端实例,它会正确注册,并且可以通过DiscoveryClient看到自己.如果我用另一个名称启动另一个实例,它也可以正常工作.

If I start up one client instance, it registers properly, and it can see itself through the DiscoveryClient. If I start up a second instance with a different name, it works as well.

但是,如果我启动两个具有相同名称的实例,则仪表板将仅显示1个实例正在运行,而DiscoveryClient仅显示第二个实例.

But if I start up two instances with the same name, the dashboard only shows 1 instance running, and the DiscoveryClient only shows the second instance.

当我杀死第二个实例时,第一个实例可以通过仪表板和发现客户端再次看到.

When I kill the 2nd instance, the 1st one is visible again through the dashboard and the discovery client.

以下是有关我正在执行的步骤以及所看到的内容的更多详细信息:

Here are some more details about the steps I'm taking and what I'm seeing:

Eureka Server

启动服务器

cd eureka-server
mvn spring-boot:run

http://localhost:8761

请注意,尚未注册任何实例"

Note that there are no 'Instances' yet registered

Eureka客户

启动客户

cd eureka-client
mvn spring-boot:run

直接在 http://localhost:8080/

/whoami终结点将显示客户端对其应用程序名称和端口的自知之明

The /whoami endpoint will show the client's self-knowledge of its application name and port

{
  "springApplicationName":"eureka-client",
  "serverPort":"8080"
}

/instances端点最多需要一分钟来更新,但最终应显示已在Eureka Discovery Client中注册的eureka-client的所有实例.

The /instances endpoint will take up to a minute to update, but should eventually show all the instances of eureka-client that have been registered with the Eureka Discovery Client.

[   
    {
      "host":"hostname",
      "port":8080,
      "serviceId":"EUREKA-CLIENT",
      "uri":"http://hostname:8080",
      "secure":false   
    } 
]

您现在也可以再次访问Eureka dashoboard,并在其中列出它.

You can also visit the Eureka dashoboard again now and see it listed there.

使用其他名称吸引其他客户

通过执行以下操作,您可以看到另一个客户端将被注册:

You can see that another client will be registred by doing the following:

cd eureka-client
mvn spring-boot:run -Dspring.application.name=foo -Dserver.port=8081

/whoami端点将显示名称foo和端口8081.

The /whoami endpoint will show the name foo and the port 8081.

在一分钟左右的时间内,/instances端点也将显示有关此foo实例的信息.

In a minute or so, the /instances endpoint will show the information about this foo instance too.

现在在Eureka仪表板上,将注册两个客户端.

On the Eureka dashboard, two clients will now be registered.

使用相同名称

Spin up another client with the same name

现在,仅覆盖端口参数,尝试尝试启动eureka-client的另一个实例:

Now try spinning up another instance of eureka-client by only over-riding the port parameter:

cd eureka-client
mvn spring-boot:run -Dserver.port=8082

http://localhost:8082/whoami端点显示了我们的期望.

The /whoami endpoint for http://localhost:8082 shows what we expect.

在一分钟左右的时间里,/instances端点现在还显示了在端口8082上运行的实例,但是由于某种原因,它没有显示在端口8080上运行的实例.

In a minute or so, the /instances endpoint now shows the instance running on port 8082 also, but for some reason, it doesn't show the instance running on port 8080.

如果我们检查http://localhost:8080上的/instances端点,我们现在也只会看到在8082上运行的实例(即使很明显,8080上的实例正在运行,因为这正是我们要的.

And if we check the /instances endpoint on http://localhost:8080 we also now only see the instance running on 8082 (even though clearly, the one on 8080 is running since that's what we're asking for.

Eureka仪表板仅显示1个正在运行的eureka-client实例.

The Eureka dashboard only shows 1 instance of eureka-client running.

这是怎么回事?

让我们尝试杀死运行在8082上的实例,看看会发生什么.

Let's try killing the instance running on 8082 and see what happens.

当我们在8080上查询/instances时,它仍然只在8082上显示该实例.

When we query /instances on 8080, it still only shows the instance on 8082.

但是一分钟后,它消失了,我们再次在8080上看到了该实例.

But a minute later, that goes away and we just see the instance on 8080 again.

问题是,为什么在同时运行eureka-client的两个实例时都看不到它们?

The question is, why don't we see both instances of eureka-client when they are both running?

推荐答案

对于本地部署,请尝试在eureka-client.properties (或eureka.instance.metadataMap)中配置{namespace} .instanceId属性. .instanceId(如果是基于Spring Cloud的安装程序,则为正确的yaml文件).它深深植根于Eureka服务器计算应用程序列表并比较PeerAwareInstanceRegistryImpl的InstanceInfo的方式-当没有更多具体数据(例如:实例元数据可用)时,它们尝试从主机名获取ID.

For local deployments, try to configure {namespace}.instanceId property in eureka-client.properties (or eureka.instance.metadataMap.instanceId for proper yaml file in case of Spring Cloud based setup). It's deeply rooted in the way Eureka server calculates application lists and compares InstanceInfo for the PeerAwareInstanceRegistryImpl - when no more concrete data (e.g.: instance metadata is available) they try to get the id from the hostname..

不过,我不建议将其用于AWS部署,因为搞乱instanceId会给您带来麻烦,即很难确定哪台计算机托管特定服务-另一方面,我怀疑您是否会在一台计算机上托管两个相同的服务,对吧?

I wouldn't recommend it for AWS deployment though, cause messing around with instanceId will bring you trouble figuring out which machine hosts a particular service - on the other hand I doubt that you'll hosts two identical services on one machine, right?

这篇关于从单个主机注册Spring Boot Eureka客户端的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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