Detailed description of the use of Ribbon in spring cloud to achieve soft load balancing on the client, cloudribbon

Source: Internet
Author: User
Tags nginx reverse proxy

Detailed description of the use of Ribbon in spring cloud to achieve soft load balancing on the client, cloudribbon

Opening

In this example, springboot integrates the H2 memory database, achieves unit test and database independence, and uses RestTemplate to consume the Restful service of spring boot.

In the Restful service example of using RestTemplate to consume spring boot, we mentioned that when calling the spring boot service, the service URL must be written to the dead or in the configuration file, however, either of the two methods, once the IP address changes, the program needs to be changed and the service needs to be re-deployed. This problem can be effectively avoided when Ribbon is used.

Preface:

There are two ways to implement soft load balancing: Server Load balancer for the server and Server Load balancer for the client.

Server Load balancer: When a browser sends a request to the backend, it first sends the request to the reverse proxy server. The reverse proxy server will map the table based on the ip: port and load balancing policy deployed on the client, to determine which server to send the request, usually using nginx reverse proxy technology.

Client Server Load balancer: When a browser sends a request to the backend, the client pulls the available service information registered to the Server from the service Registrar (for example, Eureka Server), and then according to the Server Load balancer policy, which server is directly hit to send the request. This entire process is completed on the client and does not require the involvement of reverse proxy servers.

1. Start Eureka Server

See this example: Start Eureka Server in spring cloud.

2. Start the microservice and register it on the Eureka Server.

Spring cloud-register spring boot service on Eureka Server

To demonstrate the effect of Server Load balancer, start another service. Note that you need to change the port number to an inconsistent one.

3. Add Ribbon Support

1. Add Ribbon Dependencies

2. Add Server Load balancer support

Package com. chhliu. springboot. restful; import org. springframework. beans. factory. annotation. autowired; import org. springframework. boot. springApplication; import org. springframework. boot. autoconfigure. springBootApplication; import org. springframework. boot. web. client. restTemplateBuilder; import org. springframework. cloud. client. loadbalancer. loadBalanced; import org. springframework. cloud. netflix. eureka. EnableEurekaClient; import org. springframework. context. annotation. bean; import org. springframework. web. client. restTemplate; @ SpringBootApplication @ EnableEurekaClient public class SpringbootRestTemplateApplication {@ Autowired private RestTemplateBuilder builder; @ Bean @ LoadBalanced // Add Server Load balancer support. It's easy to add @ LoadBalanced annotation to RestTemplate, restTemplate has the load balancing function. If the @ LoadBalanced annotation is not added, java.net will be reported. unknown HostException: The springboot-h2 is abnormal and the service cannot be called by registering a service name on the Eureka Server because RestTemplate cannot be mapped from service name to ip: port, the ing function is implemented by LoadBalancerClient. Public RestTemplate restTemplate () {return builder. build ();} public static void main (String [] args) {SpringApplication. run (SpringbootRestTemplateApplication. class, args );}}

3. Modify the URL for calling microservices

Package com. chhliu. springboot. restful. controller; import org. springframework. beans. factory. annotation. autowired; import org. springframework. web. bind. annotation. getMapping; import org. springframework. web. bind. annotation. pathVariable; import org. springframework. web. bind. annotation. restController; import org. springframework. web. client. restTemplate; import com. chhliu. springboot. restful. vo. user; @ RestController public class RestTemplateController {@ Autowired private RestTemplate restTemplate; @ GetMapping ("/template/{id}") public User findById (@ PathVariable Long id) {// change the format of the original ip: port to the Application name registered on the Eureka Server. restTemplate. getForObject ("http: // springboot-h2/user/" + id, User. class); System. out. println (u); return u ;}}

Iv. view Eureka Server Status

5. Refresh the http: // localhost: 7904/template/2 address multiple times in the browser.

Vi. Test Results

Port 7900 service:

Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 

Port 7901 service:

Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 

Port 7904 service:

User [id = 2, username = user2, name = Li Si, age = 20, balance = 100.00] 09:58:05. 682 INFO 7436 --- [erListUpdater-0] c. netflix. config. chainedDynamicProperty: Flipping property: springboot-h2.ribbon.ActiveConnectionsLimit to use NEXT property: niws. loadbalancer. availabilityFilteringRule. activeConnectionsLimit = 2147483647 User [id = 2, username = user2, name = Li Si, age = 20, balance = 100.00] User [id = 2, username = user2, name = Li Si, age = 20, balance = 100.00] User [id = 2, username = user2, name = Li Si, age = 20, balance = 100.00] User [id = 2, username = user2, name = Li Si, age = 20, balance = 100.00] User [id = 2, username = user2, name = Li Si, age = 20, balance = 100.00] User [id = 2, username = user2, name = Li Si, age = 20, balance = 100.00] User [id = 2, username = user2, name = Li Si, age = 20, balance = 100.00] User [id = 2, username = user2, name = Li Si, age = 20, balance = 100.00]

From the test results, we can see that a total of nine calls were made to port 7904, of which port 7904 was called to port 7900 for 4 times and port 7901 for 5 times, which was exactly 9 times.

After the above steps, the client load balancing function is basically implemented using Ribbon.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.