Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize spring cloud Integrated ribbon load balancing

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to achieve spring cloud integrated ribbon load balancing". In daily operation, I believe many people have doubts about how to achieve spring cloud integrated ribbon load balancing. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to achieve spring cloud integrated ribbon load balancing". Next, please follow the editor to study!

This article is relatively simple to integrate ribbon, if you need more details, please check out more of my blog content.

First create two service providers

Service one, an integrated nacos registry, which can write any interface of the same name

Port configuration 8301

Service 2, the content of the API with the same name is modified, and the other contents are roughly the same as the previous service.

Port is configured as 8302

Create a service consumer

RibbonConfig.java

Package com.example.nacosribbonconsumers.config;import com.netflix.loadbalancer.IRule;import com.netflix.loadbalancer.RoundRobinRule;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.ribbon.RibbonClient;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate @ Configuration// if multiple services can choose different policies / * @ RibbonClients ({@ RibbonClient (name = "other", configuration = OtherConfig.class), @ RibbonClient (name = "provider", configuration = ProviderConfig.class)}) * / @ RibbonClient (name = "nacos-ribbon-provider") public class RibbonConfig {/ / define load balancing rules @ Bean public IRule ribbonRule () {return new RoundRobinRule () / * RoundRobinRule: * polling rule * * RandomRule: * random rule * * WeightedResponseTimeRule: * use the average or percentage of response time to assign weight to each service, if the response time information cannot be collected The polling rule * * BestAvailableRule: * will be used by default to filter out the faulty service according to the circuit breaker, and then select the service with the least concurrency * * ZoneAvoidanceRule: * select the server based on the Zone where the server resides and its performance The default rule * * AvailabilityFilteringRule: * filter out the problematic services according to the circuit breaker rules, and then access the remaining services according to the polling policy * * RetryRule: * first obtain the services according to the RoundRobinRule rules. If you fail to call the service, you will retry within a specified time until the available services are obtained. * / @ Bean @ LoadBalanced public RestTemplate restTemplate () {return new RestTemplate ();}

RibbonTest.java

Package com.example.nacosribbonconsumers.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class RibbonTest {@ Autowired private RestTemplate restTemplate; @ GetMapping (value = "/ ribbon-consumers/ribbon-test") public String printProviderLog () {String result = restTemplate.getForObject ("http://nacos-ribbon-provider/ribbon-test", String.class) Return result;}}

Pom package

Org.springframework.cloud spring-cloud-starter-netflix-ribbon

Configuration file

First start the two service providers, and then after starting the service consumer, browse the

At this point, the study on "how to implement spring cloud integrated ribbon load balancing" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 268

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report