Reputation: 11
When I deploy microservice eureka client and set eureka server's url like below:
eureka.client.serviceUrl.defaultZone=http://10.118.28.94:7701/eureka/,http://10.154.32.88:7701/eureka/
I found the client always register into the first Eureka server(10.118.28.94:7701), and I want to know how to make loadbalance to prevent all the clients register into a same eureka server? Should I adjust the order of the server list manually?
Upvotes: 1
Views: 1736
Reputation: 327
this may confuse you, please follow one step at a time
and follow this link to understand micro services registration with eureka server
or start here
to make eureka clustering, you need to run two instances of eureka server
configure eureka server application.proerties as follow
#EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.port=8081
#Configuration for service registration with Eureka Server
eureka.instance.hostname=peer1
eureka.server.hostname=peer2
eureka.server.port=8082
eureka.client.serviceUrl.defaultZone=http://${eureka.server.hostname}:${eureka.server.port}/eureka/
eureka.server.renewalPercentThreshold=0.49
#self registration Configuration
eureka.client.registerWithEureka: false
eureka.client.fetchRegistry: false
run application outside IDE after packaged eureka server as jar
Note
update your hosts information before running applications
in windows C:\Windows\System32\drivers\etc
open hosts file paste these
127.0.0.1 peer1
127.0.0.1 peer2
now you are ready to run eureka
instance 1 : java -jar eurekaserveryourartifact.jar
to run the second instance, you need to override certain properties in application.proerties by passing them as VM arguments when running application, as below
instance 2 : java -jar -Dserver.port=8082 -Deureka.instance.hostname=peer2 -Deureka.server.hostname=peer1 eureka.server.port=8081 eurekaserveryourartifact.jar
---------------------- Eureka clustering completed till here ------------
access eureka1 : peer1:8081 or localhost:8081
access eureka2 : peer2:8082 or localhost:8082
make sure 1st eureka shows replica as peer2 and 2nd server shows replica as peer1
Now Register your services with eureka
register your services(eureka client) with 1st eureka server, automatically those will register with 2nd eureka server also. and clients should have following configuration in application.proerties as follow.
#reading values from pom.xml
spring.application.name=@artifactId@
#service registration with Eureka Server
eureka.server.port=8081
eureka.server.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://${eureka.server.hostname}:${eureka.server.port}/eureka/ eureka.instance.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}
to test load balancing its better to run multiple instances of service, by overriding server port, as below
service ins1: java -jar serviceartifact.jar
and
service ins2: java -jar -Dserver.port=8082 serviceartifact.jar
Now you can see 2 instances of service registered with eureka1 and eureka2
while accessing these services, if one instance of service goes down another instance (note : both services are running on different ports) url will provided to consumer of service automatically by eureka server.
and we can also do client side loadbalancing using ribbon
Upvotes: 2