Java Spring Recruitment Interview Question Answer Series: Dubbo-based service governance, service degradation and retry

1 Interview questions

How to perform service governance, service degradation, failure retry and timeout retry based on dubbo?

2 Test site analysis

  • Service governance
    is actually to see if you have the idea of ​​service governance, because this is a problem that people who have done complex microservices will definitely encounter!
  • Service degradation
    involves a must-have topic in complex distributed systems. Because distributed systems call each other back and forth, if any system fails, if you don’t downgrade, you will be paralyzed!
  • Failure retrying. In a
    distributed system, network requests are so frequent. If you accidentally fail once due to network problems, do you have to try again?
  • Retry after timeout
    Same as above, if you accidentally slow down the network and time out, how can you try again?

3 Service governance

3.1 Call link automatic generation

A large-scale distributed system, or take the popular microservice architecture for example, a distributed system consists of a large number of services.

So how do these services call each other? What is the call link?

To be honest, almost no one can figure it out later, because there are too many services, maybe hundreds or even thousands!

Then you need to automatically record the calls between various services in a distributed system based on Dubbo, and then automatically generate the dependencies and call links between each service, make a picture, and display it, everyone You can see it.

服务A   => 服务B   => 服务E
                  => 服务F
       => 服务C
                  => 服务G
       => 服务D

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-FjfMYzvI-1610287278701)(https://ask.qcloudimg.com/http-save/1752328/3z44jb9ogc.png )]

3.2 Service access pressure and duration statistics

It is necessary to automatically count the number of calls and access delay between each interface and service , and it must be divided into two levels:

  • Interface granularity
    How many times each interface of each service is called per day, TP50/TP90/TP99, what are the request delays of the three grades?
  • Starting from the entrance, after
    a complete request link passes through dozens of services, one request is completed, how many times a day the full link goes, and what is the TP50/TP90/TP99 of the full link request delay, respectively

After all these things are settled, we can look at the main pressure of the current system later to determine how to expand and optimize

3.3 Other

  • Service layering (avoid circular dependencies)
  • Call link failure monitoring and alarm
  • Service authentication
  • Monitoring the availability of each service (Interface call success rate? How many 9? 99.99%, 99.9%, 99%)

4 Service degradation

For example, service A calls service B, and service B hangs up. Service A retries to call service B several times, but it still doesn’t work. It is downgraded directly, and a backup logic is used to return a response to the user.

public interface HelloService {

   void sayHello();

}
public class HelloServiceImpl implements HelloService {

    public void sayHello() {
        System.out.println("hello world......");
    }
	
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubbo-provider" />
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <dubbo:protocol name="dubbo" port="20880" />
    <dubbo:service interface="com.javaedge.service.HelloService" ref="helloServiceImpl" timeout="10000" />
    <bean id="helloServiceImpl" class="com.zhss.service.HelloServiceImpl" />

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubbo-consumer"  />

    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <dubbo:reference id="fooService" interface="com.test.service.FooService"  timeout="10000" check="false" mock="return null">
    </dubbo:reference>

</beans>

Now it is mock, if the call fails, null will be returned uniformly

But you can modify the mock to true, and then implement a mock class in the same path as the interface, naming rules:

接口名称 + Mock

Then implement your own downgrade logic in the Mock class:

public class HelloServiceMock implements HelloService {
	public void sayHello() {
	// 降级逻辑
	}
}

5 Failure retry and timeout retry

  • Failure retry
    If the consumer fails to call the provider (such as throwing an exception), it should be retryable at this time, or the call can also be retryed if the call times out. <dubbo:reference id=“xxxx” interface=“xx” check=“true” async=“false” retries=“3” timeout=“2000”/>

The interface of a certain service takes 5s. You can’t wait here. After you configure the timeout here, I wait for 2s. If I haven’t returned yet, I will just withdraw it, and I can’t stay there forever.

If it is timed out, timeout will set the timeout time; if the call fails, it will automatically retry the specified number of times

Let’s talk about how you set these parameters based on your company’s specific scenarios.

  • The timeout is
    generally set to 200ms, we think it can’t exceed 200ms and haven’t returned
  • retries
    3 times, set retries, usually occurs during read request. For
    example, if you want to query certain data, you can set retries. If you don’t read it the first time, report an error, retry the specified number of times, and try to read 2 more times.

Guess you like

Origin blog.csdn.net/weixin_43314519/article/details/112446616