how to create multiple instances of eureka service

2020-07-16 08:03发布

I have created eureka service registry and registered services into that. Currently only one instance of a service is running. How to add multiple instances of a same service? I am developing standalone application. And I am accessing services through Rest Template.I am following https://spring.io/guides/gs/service-registration-and-discovery/

3条回答
看我几分像从前
2楼-- · 2020-07-16 08:46

You will definitely need another instance of eureka once you have that here is the official spring-cloud documentation on eureka peer awareness.

In short all you need to do is make them aware of each other by adding the info about each other in their respective application.yml. Hope this helps. Read the doc to learn in detail.

---
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2/eureka/

---
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1/eureka/

Couple of other links to learn about Eureka github Issue, Understanding Spring Cloud Eureka Server self preservation and renew threshold. Hope this helps.

查看更多
该账号已被封号
3楼-- · 2020-07-16 08:56

If you aim to run multiple instances of one service on a same host, you must:

  1. configure Eureka instance-id with a random number
  2. configure the service use a random port number

These two must be configured individually, e.g.

eureka:
  instance:
    instance-id: ${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${random.value}}

and

server:
  port: 0
查看更多
劳资没心,怎么记你
4楼-- · 2020-07-16 08:59

Each instance would need to have a unique instanceId, normally configured in application.yml using:

...
eureka:
   instance:
     metadataMap:
       instanceId: ${spring.application.name}:${server.port}
...

Adding the port to the instanceId allows to run multiple instances in the same host.

I have also seen adding a random number instead of the port the instance listens on.

I blogged about service registration and discovery using Jersey, Spring, Eureka, Ribbon and Feign with accompanying source code at http://tech.asimio.net/2016/11/14/Microservices-Registration-and-Discovery-using-Spring-Cloud-Eureka-Ribbon-and-Feign.html

And more recently blogged about how to register and discover multiple versions of a service using Eureka and Ribbon at http://tech.asimio.net/2017/03/06/Multi-version-Service-Discovery-using-Spring-Cloud-Netflix-Eureka-and-Ribbon.html.

查看更多
登录 后发表回答