0
votes

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
1
can you explain more? provide your configuration? Eureka by default only uses the hostname for uniqueness.spencergibb
added to original postDebopam
Have you tried ${server.port} instead of ${random.value} and passing --server.port=someport?spencergibb
Yes. If I use ${server.port} it will register as 0. Only last client is getting registered. My pain area is now to have a load balancer in place which will route the traffic to both the instances.Debopam
My comment was to NOT set the server.port to 0. But pass a unique port via a the CLI parameter.spencergibb

1 Answers

1
votes

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}}