preface

Hello everyone, today we start to share – Dubbo topic Dubbo Dynamic Configuration Center. In the previous chapter we introduced registries in Dubbo and various registry implementations. In this chapter we introduce the Dubbo Dynamic configuration Center. Now let me be quick!

1. Overview of the configuration center

Some people may ask, what is a configuration center? What does it do? This is a scenario we might normally encounter in development. Suppose we have an external interface that needs to dynamically adjust the on/off configuration to achieve dynamic control. Our implementation scheme may include: storing database, writing files and so on. At the same time, we can also manage dynamically through centralized and unified configuration. For example, Apollo, Nacos and Zookeeper provide more powerful functions than traditional database storage, such as multi-tenant, real-time synchronization and distributed features. We can leverage these configuration center features for unified dynamic management of all configuration parameters in our Dubbo.

As we can see from the figure above, the configuration center notifies service consumers and service providers when we change configuration center parameters. This allows us to dynamically adjust the parameters in Dubbo.

2. Configuration center technology selection

We generally measure a configuration center by the following dimensions:

  1. Distributed configuration unified management

  2. Dynamic change notification

  3. security

  4. The real time

Currently, the configuration center mainly includes Eureka(officially not maintained), Apollo, Spring Cloud Config, Nacos. Here’s how they compare:

The function point Spring Cloud Config Apollo Nacos
Open time 2014.9 2016.5 2018.6
Configuring Real-time Push Support (Requires Spring Cloud Bus support) Support (HTTP long polling) Support (HTTP long polling)
Version management Support (Git) support support
Configure a rollback Support (Git) support support
Gray released support support To support
Rights management support support To support
Multiple clusters support support support
Many environmental support support support
Listening to the query support support support
multilingual Only support Java Go, C++, Java, Python, PHP,.net, OpenAPI Java, Python, Node.js, OpenAPI
Single deployment Config Server+Git+Spring Cloud Bus Apollo-quikstart+MySQL Nacos single node
Distributed deployment Config Server+Git+MQ Config+Admin+Portal+MYSQL Nacos+MySQL
Configuration format verification Does not support support support
Communication protocol HTTP and closer HTTP HTTP

Based on the above parameters, it is not difficult to see that Nacos is a good choice in terms of language support, ease of operation and deployment, ease of use and function perfection. While Apollo is indeed powerful in terms of features, Nacos is much easier to use than Apollo, and Nacos is much easier to operate and deploy, while Spring Cloud Config is an immature solution that requires too many dependencies and is cumbersome.

3. Example demonstration

  1. Deploy Nacos

For the deployment of Nacos, I use Docker image to deploy it for convenience. As for the Docker environment, you can check the information by yourself. Since it is used for experimental testing, single-machine mode is adopted here:

  • Download the project

    git clone https://github.com/nacos-group/nacos-docker.git
    cd nacos-docker
    Copy the code
  • The deployment of the mirror

    docker-compose -f example/standalone-derby.yaml up
    Copy the code

    Use the browser input address: http://127.0.0.1:8848/nacos login Nacos console, the diagram below:

The single-machine mode here is intended for test environments, not production environments.

  1. We take the configuration of Dubbo parameters to the Nacos configuration center as an example to demonstrate

    The project structure is as follows:

Service provider code:

/ * * *@author<a href="http://youngitman.tech"> Young IT male </a> *@versionV1.0.0 *@className Application
 * @descriptionDynamically configure the central provider *@JunitTest: {@link  }
 * @dateThe 2020-10-24 21:49 * * /
// Start Dubbo annotation scan
@EnableDubbo(scanBasePackages = "com.muke.dubbocourse.dynamicconfig")
// Activate the Nacos configuration and specify the address where the Nacos service resides
@enablenacosConfig (globalProperties = @nacosProperties (serverAddr = "127.0.0.1:8848")) // Activate the Nacos configuration
// Specify a unique identifier for our externalized configuration: dataId
@NacosPropertySource(dataId = "muke")
public class AnnotationApplication {

    public static void main(String[] args) throws IOException {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

       // Registers the current AnnotationApplication as a configuration class
        context.register(AnnotationApplication.class);

        // Refresh the Spring context
        context.refresh();

        / / blockingSystem.in.read(); }}Copy the code

Service consumer code:

/ * * *@author<a href="http://youngitman.tech"> Young IT male </a> *@versionV1.0.0 *@className Application
 * @descriptionDynamically configure the central service consumer *@JunitTest: {@link  }
 * @dateThe 2020-10-24 21:49 * * /
// Start Dubbo annotation scan
@EnableDubbo
// Activate the Nacos configuration
@EnableNacosConfig 
// Configure the unique identifier of the configuration center: dataId
@NacosPropertySource(dataId = "muke")
public class AnnotationApplication {

    static {
        // Specify the address of the nacos service
        System.setProperty("nacos.server-addr"."127.0.0.1:8848");
    }

    // Reference BookFacade's Dubbo service
    @ DubboReference (version = "0.0.1)"
    private BookFacade bookFacade;

    public static void main(String[] args) throws IOException {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        // Registers the current AnnotationApplication as a configuration class
        context.register(AnnotationApplication.class);

        // Refresh the Spring context
        context.refresh();

        // Get AnnotationApplication from the container
        AnnotationApplication annotationApplication = context.getBean(AnnotationApplication.class);

        // Invoke the service method
        System.out.println("Result=>"+annotationApplication.bookFacade.queryAll());

        / / blockingSystem.in.read(); }}Copy the code

In the above code, we use annotations to configure the service. We also put the registry address, application name and other configuration in the configuration center. As follows:

Here we realize the dynamic configuration center, put all the configuration in a unified place for maintenance.

** Where Data ID is the unique identifier of our application, Group is the Group information, and we use the format of Properties to configure parameters.

4. Summary

In this section, we mainly learn the configuration center in Dubbo, in which we know what is the configuration center, and we also compare the current more mature and popular configuration center from multiple dimensions, we should use the appropriate configuration center according to different requirements in actual application scenarios.

The highlights of this lesson are as follows:

  1. Understand the Dubbo configuration center

  2. Learn about common configuration centers

  3. How to use Nacos to integrate with Dubbo

  4. Learn about the advantages of our different configuration centers

The author

Personally engaged in the financial industry, I have worked in chongqing’s first-class technical team of Yiji Pay, Sijian Technology and an online car hailing platform, and now I am working in a bank responsible for the construction of unified payment system. I have a strong interest in the financial industry. It also practices big data, data storage, automated integration and deployment, distributed microservices, responsive programming, and artificial intelligence. At the same time, he is also keen on technology sharing, creating public accounts and blog sites to share knowledge system. Concern public number: young IT male get latest technical article push!

Blog: Youngitman.tech

Wechat Official Account:

Knowledge planet: