In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Many beginners are not very clear about the introduction and use of the load balancer Ribbon in SpringCloud. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
At present, the mainstream load schemes are divided into the following two types:
Centralized load balancing, using independent proxies between consumers and service providers, ranging from hardware (such as F5) to software (such as Nginx).
The client makes its own load balancer and does the load according to its own request, and the Ribbon belongs to the client to do the load.
Spring Cloud Ribbon is a client-side load balancing tool based on HTTP and TCP, which is implemented based on Netflix Ribbon. Through the encapsulation of Spring Cloud, we can easily convert the service-oriented REST template request into the service call of the client load balance.
Although Spring Cloud Ribbon is only a tool framework, it does not require independent deployment like service registries, configuration centers, and API gateways, but it exists in almost every micro-service and infrastructure built by Spring Cloud. Because the calls between microservices and the request forwarding of the API gateway are actually implemented through Ribbon (https://github.com/Netflix/ribbon).
Ribbon module
The Ribbon module is described as shown in Table 1 below.
Table 1 introduction of Ribbon module
The named ribbon-loadbalancer load balancing module can be used independently or with other modules. Ribbon's built-in load balancing algorithms are implemented in it. Ribbon-eureka is based on Eureka encapsulated module, which can integrate Eureka quickly and easily. Ribbon-transport implements multi-protocol support based on Netty, such as HTTP, Tcp, Udp, etc. Ribbon-httpclient is based on the REST client encapsulated by Apache HttpClient and integrates the load balancing module, which can be used directly in the project to call the API. Ribbon-exampleRibbon uses code examples through which you can get twice the result with half the effort. Ribbon-core some core and versatile code, some configuration of client-side API and other API definitions. Ribbon usage
We use Ribbon to implement the simplest load balancer invocation function. The interface uses the / user/hello interface provided by "using Eureka to write service providers". Two services need to be started, one is port 8081 and the other is port 8083.
Then create a new Maven project ribbon-native-demo, integrate Ribbon in the project, and add the dependencies shown in the following code to pom.xml.
Com.netflix.ribbonribbon2.2.2com.netflix.ribbonribbon-core2.2.2com.netflix.ribbonribbon-loadbalancer2.2.2io.reactivexrxjava1.0.10
Next, we write a client to call the interface, as shown in the following code.
/ / Service list List serverList = Lists.newArrayList (new Server ("localhost", 8081), new Server ("localhost", 8083)); / / build load instance ILoadBalancer loadBalancer = LoadBalancerBuilder.newBuilder (). BuildFixedServerListLoadBalancer (serverList); / / call 5 times to test the effect for (int I = 0; I < 5) ) {String result = LoadBalancerCommand.builder (). WithLoadBalancer (loadBalancer). Build (). Submit (new ServerOperation () {public Observable call (Server server) {try {String addr = "http://" + server.getHost () +": "+ server.getPort () +" / user/hello "; System.out.println (" call address: "+ addr); URL url = new URL (addr)) HttpURLConnection conn = (HttpURLConnection) url.openConnection (); conn.setRequestMethod ("GET"); conn.connect (); InputStream in = conn.getInputStream (); byte [] data = new byte [in.available ()]; in.read (data); return Observable.just (new String (data)) } catch (Exception e) {return Observable.error (e);}) .toBlocking () .first (); System.out.println ("call result:" + result);}
The above example mainly demonstrates how Ribbon does the load operation and calls the lowest-level HttpURLConnection used by the interface. Of course, you can also use other clients, or execute the program directly with RibbonClient, and you can see the output from the console as follows:
As you can see from the output, the load worked, with three calls for 8083 and two calls for 8081.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.