Opening
This example is based on springboot integrating H2 memory database, realizing unit tests and database-independent and using RestTemplate to consume spring boot's Restful service.
In the Restful service example that uses RestTemplate to consume spring boot, we mentioned that when calling spring boot service, the service URL needs to be written to the configuration file, but no matter which of these two methods, once the IP address changes, the program needs to be changed and the service is redeployed. This problem can be effectively avoided when using Ribbon.
Preface:
There are two ways to implement soft load balancing, namely load balancing on the server and load balancing on the client.
Server-side load balancing: When the browser issues a request to the background, it will first send a request to the reverse proxy server. The reverse proxy server will decide which server to send a request to based on the ip:port mapping table and load balancing policy deployed by the client. Generally, nginx reverse proxy technology is used.
Client load balancing: When the browser makes a request to the background, the client will pull the available service information registered to the server to the service registrant (for example: Eureka Server), and then directly hit which server to send the request according to the load balancing policy. This whole process is done on the client side and does not require the participation of the reverse proxy server.
1. Start Eureka Server
Please refer to this example: Start Eureka Server in spring cloud
2. Start the microservice and register with Eureka Server
spring cloud - Register spring boot service to Eureka Server
In order to demonstrate the effect of load balancing, start another service, please note that the port number needs to be changed to inconsistent
3. Add Ribbon support
1. Add Ribbon's dependencies
2. Add load balancing 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 // Adding load balancing support is very simple. You only need to add the @LoadBalanced annotation to the RestTemplate. Then the RestTemplate has the function of load balancing. If the @LoadBalanced annotation is not added, a java.net.UnknownHostException:springboot-h2 exception will be reported. At this time, the service cannot be called by registering the service name on Eureka Server, because RestTemplate cannot map from the service name to ip:port, and the mapping 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 of the calling microservice
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 original ip:port form to the application name registered on Eureka Server User u = this.restTemplate.getForObject("http://springboot-h2/user/" + id, User.class); System.out.println(u); return u; } }4. Check the status of Eureka Server
5. In the browser, refresh the http://localhost:7904/template/2 address many times
6. Test results
7900 port 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_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?
7901 port 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_.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=?
7904 port service:
User [id=2, username=user2, name=Li Si, age=20, balance=100.00] 2017-01-23 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 above test results, we can see that a total of 7904 port services were adjusted 9 times, of which 7904 port services were adjusted 4 times, and 7901 ports were adjusted 5 times, which was exactly 9 times.
After the above steps, Ribbon is basically used to realize the client load balancing function.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.