Home > Back-end >  How to create several instances of a Microservice: SpringBoot and Spring Cloud
How to create several instances of a Microservice: SpringBoot and Spring Cloud

Time:12-29

I am new to the Microservices, I came across several concepts like Service Registry and Load Balancing. I have following questions-

  1. How multiple instances of a particular microservice are created?
  2. How Service Registry using Eureka Server helps in distributing the load on the several instances of Microservice?
  3. In my scenario, I created 3 different microservices and registered them on my service registry-
    Service Registry Configuration-
server:
    port: 8761
eureka:
    instance:
        hostname: localhost
    client:
        register-with-eureka: false #this will ensure that this service
                                    #is not itself registered as a client
        fetch-registry: false

Client Configuration-

eureka:
    instance:
        prefer-ip-address: true
    client:
        fetch-registry: true #true by default
        register-with-eureka: true #true by default
        service-url:
            defaultZone: http://localhost:8761/eureka 

When I stop my services I still see the services as Up and Running on Eureka and get a warning as- enter image description here enter image description here Can somebody please help me find the reason for this problem?

CodePudding user response:

1. To communicate with another instance, you can use Eureka to find the address of the service you want to talk to. Eureka will give you a list of all the available instances of that service, and you can choose which one you want to communicate with.

2. The Eureka server is a microservice that keeps track of the locations of other microservices within the same system. These other microservices register themselves with the Eureka server so that they can be found and contacted by other microservices when needed. The Eureka server acts as a directory for the microservices, allowing them to find and communicate with each other. (not sure if that's what you asked).

3. In order to remove the warning:

You can set a renewal threshold limit in the Eureka server's properties file.

eureka.renewalPercentThreshold=0.85

CodePudding user response:

1. To scale your microservice locally, you can run multiple instances of your Spring Boot application each on a different port.

First update the port on the microservice you wish to scale in the application.yml file to:

server:
   port: 0

This will start the application on a random port each time you run it.

If you run 2 applications now, you will see 1 instance of your microservice on your Eureka dashboard. This is because they both have the same Eureka instance id. To fix this you need to generate a new instance id so add the below in the same application.yml:

spring:
   application:
       name: "hotel-service"
eureka:
   instance:
       instance-id: "${spring.application.name}:${random.value}"

Finally, just run the same application more than once. You can do this on InteliJ by right clicking on the main class and selecting run and then doing that again. Some extra setup may be required to run multiple instance of the same application on IntelliJ, please see link: How do I run the same application twice in IntelliJ?

If you are using Eclipse/STS, right click on project > Run as > Spring Boot App (do this twice or more).

Alternatively if you have maven installed. Open a terminal and run mvn spring-boot:run and then open a new terminal and run the command again.

Now you should see multiple instances of your application on the Eureka dashboard.

Note: In production scaling up a microservice is done on the by the Devops team, for example a container orchestration platform such as Kubernetes can be used to increase the instances of a microservices.

2. Generally an API gateway is used to route incoming network requests to microservices and do the load balancing while service discovery allows microservices to find and communicate with each other.

With Eureka service discovery, the microservices will register on the discovery server. An API gateway will also register on the Eureka server and will do load balancing.

One method of load balancing is the round-robin strategy, where the load balancer will rotate through the available instances in a sequential order. This helps to distribute the load evenly across the instances. There are also other load balancing methods, like Least Connection, Resource Based (Adaptive) etc.

3. The error your getting is due to the self preservation mode which comes with the Eureka server. The Eureka server expects a heartbeat from microservices every 30 seconds by default, if it doesn't receive a heartbeat within 90 seconds it will de-register a microservice from it. In a case where Eureka doesn't receive heartbeat signals from many services, it will de-register each microservice up to a certain limit. Then after it will enter self preservation mode and will not de-register any more microservices and will try to re-establish a connection, this is because a network issue could result in eureka server from not receiving heartbeats.

Since you are developing locally and you stopped running your microservices, you are seeing the expected behaviour of the Eureka server entering self preservation mode, which you can ignore.

  • Related