1. What are load balancing and common solutions
-
What is Load Balance
A very important concept in distributed system. When the accessed service has multiple instances, it needs to decide which node the request is sent to according to a "balanced" strategy. This is the so-called load balancing. The principle is to allocate the data traffic to multiple servers for execution, reduce the pressure on each server, and improve the data throughput -
Types of software and hardware load balancing
It is solved by hardware. The common hardware includes commercial load balancers such as NetScaler, F5, Radware and Array, but they are more expensive
It is solved through software. Common software include LVS, Nginx, etc. they are open source load balancing strategies based on Linux system -
There are two kinds of load balancing from the end point of view
Server load balancing
Client load balancing -
Common load balancing strategies (depending on the support of components)
Node polling: each request is allocated to a different back-end server in order
Weight weight configuration: weight is directly proportional to the access ratio. The larger the number, the higher the traffic allocated
Fixed distribution: allocate according to the hash result of access ip according to the request, so that each user can access a back-end server fixedly
Random selection, shortest response time, etc
2. Alibaba cloud integrates Ribbon to realize load balancing
-
What is Ribbon
Ribbon is a client-side load balancing tool, which can be easily integrated with Alibaba cloud through Spring Cloud encapsulation -
Add @ LoadBalanced annotation to order service
@SpringBootApplication @EnableDiscoveryClient public class OrderApplication { public static void main(String [] args){ SpringApplication.run(OrderApplication.class,args); } @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
- The serverInfo field is added to the VideoOrder entity class to record the server information, which is convenient for testing
- Modify VideoController
@RestController @RequestMapping("api/v1/video") public class VideoController { @Autowired private VideoService videoService; @RequestMapping("find_by_id") public Object findVideoById(int videoId, HttpServletRequest request){ Video video = videoService.findById(videoId); video.setServerInfo(request.getServerName()+":"+request.getServerPort()); return video; } }
- Modify OrderController
@RestController @RequestMapping("api/v1/video_order") public class OrderController { @Autowired private RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/save") public Object save(int videoId){ //Video video = restTemplate.getForObject("http://localhost:9000/api/v1/video/find_by_id?videoId="+videoId, Video.class); //List<ServiceInstance> list = discoveryClient.getInstances("xdclass-video-service"); //ServiceInstance serviceInstance = list.get(0); Video video = restTemplate.getForObject("http://xdclass-video-service/api/v1/video/find_by_id?videoId="+videoId, Video.class); VideoOrder videoOrder = new VideoOrder(); videoOrder.setVideoId(video.getId()); videoOrder.setVideoTitle(video.getTitle()); videoOrder.setCreateTime(new Date()); videoOrder.setServerInfo(video.getServerInfo()); return videoOrder; } }
- IDE settings start multiple video services
Modify the server port in turn to start
- Test with postman