The easy-to-understand Dubbo tutorial (3): Property loading order and check at startup

Foreword

This article will introduce two important functions of Dubbo: attribute loading sequence and startup check.

Property loading order

Coverage strategy

How does Dubbo read the configuration during the application startup phase? For example, we know that Dubbo supports multiple levels of configuration, and will automatically achieve the coverage between the configurations according to the predetermined priority. Then if we configure Dubbo through the virtual machine parameters, we also configure it through the local configuration file dubbo.properties , Which configuration will be preferred first? This is a problem that Dubbo's attribute loading order needs to be solved.

Dubbo supports four configuration sources, as follows:

  1. JVM virtual machine parameter configuration mode, configured by -D parameter
  2. Configuration through dubbo.xml (equivalent to application.properties/application.yml in Spring Boot), that is, external configuration
  3. Configure accordingly via programming interface
  4. Configuration through the local configuration file dubbo.properties

The figure of Dubbo's official document also shows the priority of these four configuration sources. From top to bottom, the priority is lowered in order.
Insert picture description here

to sum up

If JVM virtual machine parameters are passed in, the virtual machine parameters take precedence, otherwise the custom dubbo.xml file takes precedence. If dubbo.xml does not define related attributes, the programming interface takes precedence. Finally, if none of the preceding The corresponding definition is subject to the local configuration file dubbo.properties.

Configuration method
  1. We can configure the public configuration, such as the application name, registration center address and the like. Configured in the local configuration file dubbo.properties
  2. Configurations that will require additional changes are configured in the custom dubbo.xml configuration file
  3. The configuration that needs to be temporarily changed is configured by passing in JVM virtual machine parameters at startup

Check at startup

What is a check at startup

Dubbo will check whether the dependent service is available when it starts, if it is not available, it will throw an exception, so the default startup check is set to true.

We can also turn off the check by setting the check at startup to false. The applicable scenarios are those that do not care about certain services, and the scenario where circular dependencies require one to start first.

In addition, if the Spring container is lazy loaded, or the reference service is delayed through API programming, please turn off the check, otherwise the service will be temporarily unavailable, it will throw an exception and get a null reference, if the check is false at startup, it will always return the reference , When the service is restored, it can automatically connect.

test

We only start the consumer, not the provider. Since the default check is true at startup, the consumer will find that the dependent service is not available when it starts, and it will throw an exception. The test is as follows:
Insert picture description here
we set the startup check to false and add the following configuration in the configuration file:

#关闭所有服务的启动时检查
dubbo.consumer.check=false

Then we restarted the consumer and found that no exception was thrown at startup this time.

Insert picture description here
Then we remotely call the method provided by the provider and find that an exception is thrown when it is called.
Insert picture description here
Insert picture description here
In addition to turning off the startup check of all services, we can also turn off the startup check of the registry and the startup check of a certain service. For the specific configuration, please refer to the official documentation.

Published 151 original articles · praised 317 · 40,000+ views

Guess you like

Origin blog.csdn.net/Geffin/article/details/105666475