Search code examples
springspring-cloudnetflix-zuulspring-cloud-netflixnetflix-ribbon

Load Balancing micro services using Spring cloud Netflix OSS


I have a microservice built using Spring Boot and Netflix OSS. I have used Central config server, Eureka and Zuul. Because of scalability multiple instances of the services is running on different port. All the instances are registered in Eureka but requests are going to only last registered server.

How to load balance services. Should I use ribbon in Zuul to load balance? Please let me know how to achieve load balancing on same service running on multiple instances.

If there is a code change required a snippet then please post a code snippet as well.

Application Config

spring.application.name=book-service
server.port=0

eureka.client.region = default
eureka.client.registryFetchIntervalSeconds = 5
eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:10082/eureka/

#eureka.instance.metadataMap.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}
eureka.instance.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}
eureka.instance.leaseRenewalIntervalInSeconds=5
eureka.instance.leaseExpirationDurationInSeconds=5

Eureka Config

spring.application.name=discovery
server.port=10082

eureka.instance.hostname=localhost

eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:10082/eureka/
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

spring.session.store-type=hash-map

ZUUL Config

spring.application.name=gateway
server.port=10080

eureka.client.region = default
eureka.client.registryFetchIntervalSeconds = 5

management.security.sessions=always

zuul.routes.book-service.path=/book-service/**
zuul.routes.book-service.sensitive-headers=Set-Cookie,Authorization
hystrix.command.book-service.execution.isolation.thread.timeoutInMilliseconds=600000

#zuul.routes.rating-service.path=/rating-service/**
#zuul.routes.rating-service.sensitive-headers=Set-Cookie,Authorization
#hystrix.command.rating-service.execution.isolation.thread.timeoutInMilliseconds=600000

zuul.routes.discovery.path=/discovery/**
zuul.routes.discovery.sensitive-headers=Set-Cookie,Authorization
zuul.routes.discovery.url=http://localhost:8082
hystrix.command.discovery.execution.isolation.thread.timeoutInMilliseconds=600000

logging.level.org.springframework.web.=debug
logging.level.org.springframework.security=debug
logging.level.org.springframework.cloud.netflix.zuul=debug

spring.session.store-type=hash-map

Solution

  • Registering application properly with Eureka will do the trick.

    Below entries will uniquely register services with Eureka.

    #eureka.instance.metadataMap.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}
    eureka.instance.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}