We have completed the two basic components of the Registration Center and the Service Provider. This article introduces the call service using Spring Cloud Ribbon to load balancing on client side.
Load balancing (LB:Load Balancing) is the first problem to be solved. Before microservices, the LB solution was mainly a centralized load balancing solution, and another independent LB between service consumers and service providers. The LB is usually dedicated hardware, such as F5, or software-based, such as VS, HAproxy, etc. There is an address mapping table for all services on the LB. When the service consumer calls a target service, it first initiates a request to the LB. The LB uses a certain policy (such as Round-Robin) to load balancing the request to the target service.
The emergence of microservices provides another idea for the implementation of LB: to integrate LB's functions into the process of service consumers in the form of libraries, rather than provided by a centralized device or server. This solution is called Soft Load Balancing or client load balancing. In Spring Cloud, in conjunction with Eureka's service registration function, the Ribbon sub-project implements load balancing for the REST client.
Serve consumers with Spring Cloud Ribbon
Create a new spring-cloud-sample-tutorial-consumer project
Create a new spring-cloud-sample-tutorial-consumer sub-project under spring-cloud-sample-tutorial
Add ribbon and eureka dependencies
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency></dependencies>
Configure applicationin.properties, register center address
spring.application.name=consumerserver.port=30001eureka.client.service-url.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/
Write UserController, add @LoadBalanced annotation, enable Ribbon load balancing
@Controller@RequestMapping("user")public class UserController { @Bean @LoadBalanced RestTemplate initRestTemplate(){ return new RestTemplate(); } @Autowired private RestTemplate restTemplate; @RequestMapping("add") @ResponseBody public String add(String userName, String age){ return restTemplate.getForEntity("http://PRODUCER/user/add",String.class,userName,age).getBody(); }}Write ConsumerApplication, add @EnableEurekaClient, enable service registration
@EnableEurekaClient@SpringBootApplicationpublic class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class,args); }}Cluster deployment Producer
In order to simulate the cluster's Producer, new application-profile1.properties and application-profile2.properties are created in the producer project.
application-profile1.properties
spring.application.name=producerserver.port=20001eureka.client.service-url.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/
application-profile2.properties
spring.application.name=producerserver.port=20002eureka.client.service-url.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/
In order to test the load effect, we typed out the port of the called service
@Controller@RequestMapping("user")public class UserController { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private DiscoveryClient client; @RequestMapping("add") @ResponseBody public String addUser(String userName, String age) { return "Success from " + client.getLocalServiceInstance().getHost() + ":" + client.getLocalServiceInstance().getPort() ; }}Start the test
Start the Registration Center
Configure Active Profiles to profile1 and profile2 respectively, start twice, and complete the service startup of the registry cluster.
Start a service provider
Start the service provider in the same way as above.
Start serving consumers
The service provider can be done on a stand-alone basis, and the ConsumerApplication can be started normally.
verify
Enter http://localhost:30001/user/add from the browser
Visit again:
It can be seen that our load balancing call service has been successful, and the default is load balancing according to rotation training.
Summarize
This article introduces and completes the call to use Spring Cloud Ribbon for client load balancing.
Next, we will continue to introduce how to use Spring Cloud for service monitoring.
Source code download
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.