Use of cluster fault tolerance, load balancing, and service degradation in Spring Cloud Alibaba Dubbo

In fact, we have already introduced Ribbon load balancing. In the case of large traffic, we will increase the number of nodes through horizontal expansion to balance the traffic of requests, thereby improving the performance of the overall service. As a microservice framework, Spring Cloud Alibaba uses Dubbo as a communication framework. Dubbo provides four load balancing strategies, namely random algorithm (random), roundrobin (roundrobin), least active call (leastactive), consistent hash algorithm (Consistenthash). Among them, the random algorithm can set the weight for the server with better performance, and the higher the probability of the request. The rotation algorithm is to set the rotation ratio according to the weight after the convention. The node with the least active calls and slow processing will receive fewer requests, consistent The hash algorithm request parameters are sent to the same server. The following is the load balancing class diagram provided by Dubbo:

When using Dubbo's load balancing strategy, you can add the loadbalance parameter to the @Service annotation. The value of loadbalance is the name of the load balancing strategy, which is the value in the brackets of the load balancing strategy described above. For example, we set the load balancing strategy to For rotation training, @Service annotations are as follows:

@Service(loadbalance="roundrobin")
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

Dubbo's load balancing strategy is relatively simple. Let's continue to introduce Dubbo's service degradation strategy. Service degradation is a protection strategy. When the service pressure is high, unimportant services can be downgraded to ensure the normal operation of core services. For example, every year on Double Eleven, product evaluation and other services are downgraded to ensure that most user transactions are normal. Dubbo provides a Mock configuration method to achieve service degradation. That is to say, when the service provider has an exception, the bottom data is returned through the degradation configuration. The following is an example. We need to implement the service provider interface. The code is as follows:

public class MockHelloServciceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "服务异常";
    }
}

When using Dubbo's service downgrade, we only need to add the mock parameter to the @Reference annotation, whose value is the implementation of the downgrade service, which is the class we wrote above, and set the cluster to failfast. Examples are as follows:

@RestController
public class HelloController {
    @Reference(check = false, mock = "cn.org.microservice.consume.servcice.MockHelloServciceImpl",cluster = "failfast")
    private HelloService helloService;
    @GetMapping("/hello/{name}")
    public String sayHello(@PathVariable String name) {
        return helloService.sayHello(name);
    }
}

In the above service degradation, we used the cluster parameter. This parameter is used to configure Dubbo's fault tolerance and fault tolerance refers to the processing method after the request fails due to network exceptions, service exceptions, etc. when the service caller calls the service again. For service callers, a mechanism is needed to handle this kind of error gracefully. Dubbo provides a total of 6 fault-tolerant mechanisms, which can be extended by themselves. The 6 fault tolerance methods provided by Dubbo are as follows:

Failover Cluster: Automatically switch after failure. When the service call fails, it will switch to other nodes in the cluster to try. The default number of retries is 2, and the number of retries can be modified through the retries attribute.

Failfast Cluster: Fast failure. After the service call fails, an error will be reported immediately, that is, only one call is initiated.

Failsafe Cluster: Failure safe, that is, when an exception occurs, the exception is directly ignored.

Failback Cluster: Automatically reply after failure. When the service call is abnormal, the failed request is recorded in the background and retransmitted regularly.

Forking Cluster: Call multiple services in the cluster in parallel, as long as one returns successfully. The maximum number of parallels can be set through the forks parameter,

Broadcast Cluster: Broadcast calls and all service providers, any service error indicates that the service caller failed.

Configuring fault tolerance in Dubbo is very simple, just add cluster annotations in @Service. The following is an example of Dubbo fault tolerance:

@Service(loadbalance="roundrobin",cluster = "failfast")
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

The following figure shows the implementation class diagram of fault tolerance:

                                                                                                                                                           

Guess you like

Origin blog.csdn.net/wk19920726/article/details/108411066