Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Ribbon to realize load balancing in SpringCloud

Shulou Source: shulou.com Published: 2022-06-02 05:38:26 09月10日 Update

It is believed that many inexperienced people have no idea about how to use Ribbon to achieve load balancing in SpringCloud. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

First add the dependent package for Fegin in the pom file.

Org.springframework.cloud spring-cloud-starter-openfeign

Then add the @ EnableFeignClients annotation to the startup class of the springcloud-ribbon-consumer project to enable feign to make remote calls.

After the addition is complete, create a new class to implement the feign remote call.

The code is as follows:

FeignClient (name= "springcloud-ribbon-consumer2") public interface HelloRemote {@ RequestMapping (value = "/ hello") public String hello (@ RequestParam (value = "name") String name);}

Finally, a new interface is provided for external calls. Here, add a new interface directly to the previous code.

The code is as follows:

@ RestController public class ConsumerController {@ Autowired RestTemplate restTemplate; @ RequestMapping ("/ hello") public String hello () {return restTemplate.getForObject ("http://springcloud-ribbon-consumer2/hello/?name=xuwujing", String.class);} @ Autowired HelloRemote helloRemote @ RequestMapping ("/ hello/ {name}") public String index (@ PathVariable ("name") String name) {System.out.println ("receive request parameter:" + name+ ", forward to other services!"); return helloRemote.hello (name);}}

After adding, restart the springcloud-ribbon-consumer service, and then visit the http://localhost:9006//hello/pancm address again and again to understand the springcloud architecture. Please add: 3536247259

The results of the visit are as follows:

Pancm,Hello World!pancm,Hello World! This is another service! Pancm,Hello World!pancm,Hello World! This is another service! Pancm,Hello World!pancm,Hello World! This is another service! After reading the above, have you mastered how to use Ribbon to achieve load balancing in SpringCloud? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Service code this is equilibrium content interface method more problem helpless for this reason reason parameter this skills files architecture comments articles experience Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Redmi Shulou Technology MySQL macOS