No instances available for localhost

   When using the RestTemplate class to call the url of other systems, add the annotation @LoadBalanced of the ribbon to access this annotation, and an error will be reported.





The error is as follows:


Because you can't directly access the address here , you need to change the address to the application.name registered on eureka by the ur you call


Modify the code as follows, you can access


For the specific reason, I checked the source code and found it online. It seems that after adding the annotation @LoadBalanced, our restTemplate will go to this class RibbonLoadBalancerClient, and the breakpoint will enter the following page. The serverid must be the service we visit. Name  , when we directly enter the ip, the server obtained is null, and an exception will be thrown



Use the service name as shown below:



Think about it carefully, because the role of ribbon is load balancing, then if you use the ip address directly, it cannot play the role of load balancing, because the same service is called every time, when you use the service name, He will choose the service with the service name according to his own algorithm.


Reprinted: https://blog.csdn.net/november22/article/details/54612454

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324735355&siteId=291194637