Application practice of Day04~Nacos configuration center

Core knowledge points

  • The birth background of the configuration center.
  • The mainstream configuration center in the market.
  • Introduction to Nacos configuration center.
  • Nacos configuration center introduction practice.
  • Configuration management model of Nacos configuration center.

Background analysis

We know that in addition to the code, the software also has some configuration information, such as the user name and password of the database, and some things we don't want to write dead in the code, such as thread pool size, log unbinding, algorithm strategy, etc Previously, we wrote the software configuration in a configuration file, such as ini file under Windows or conf file under Linux However, in distributed systems, this approach becomes very unmanageable and error prone If the project is running here and the configuration file is modified, we need to make these configurations take effect. The usual way is to restart the service However, restarting the service will lead to a short suspension of the system service, which will affect the user experience and may bring economic losses Based on this background, the configuration center was born

Configuration center overview

The most basic function of the configuration center is to store a key value pair, the user issues a configuration (configKey), and then the client obtains the configuration item (configValue) When a configuration item is changed, the service internal configuration item can be refreshed dynamically without shutdown

Selection of configuration center

The mainstream configuration centers in the market include Apollo (Ctrip open source), nacos (ALI open source), Spring Cloud Config(Spring Cloud family bucket member) When selecting these configuration centers, we should focus on comprehensive consideration from the aspects of product function, use experience, implementation process and performance. nacos not only provides the registration center, but also has the function of configuration center

Section interview analysis

  • What is a configuration center? (a service that stores project configuration information)
  • Why use configuration center? (centralized management of configuration information and dynamic release of configuration information)
  • What are the mainstream configuration centers in the market? (Apollo,Nacos,Spring Cloud Config, etc.)

Nacos configuration quick start

Step 1: create a ProviderLogController object, for example:

package com.jt.provider.controller;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Based on this, the controller demonstrates the role of the configuration center
 * In this controller, we will be based on the log object
 * Perform log output test
 */
//@Slf4j
@RestController
public class ProviderLogController {
    //Create a log object
    //org. slf4j. Logger (the logging API specification in Java. Based on this specification, there are Log4J,Logback and other log libraries)
    //org.slf4j.LoggerFactory
    //In the getLogger method, the bytecode object of the class in which the log object is created is passed in
    //Remember: in the future, as long as you use log objects in Java, you can create them in the following way
    //If the @ Slf4j annotation is used on the class where the log object is located, we no longer need to create the log manually. lombok will help us create it
   private static Logger log=
           LoggerFactory.getLogger(ProviderLogController.class);
    @GetMapping("/provider/log/doLog01")
    public String doLog01(){//trace<debug<info<warn<error
        System.out.println("==doLog01==");
        log.trace("===trace===");
        log.debug("===debug===");
        log.info("===info====");
        log.warn("===warn===");
        log.error("===error===");
        return "log config test";
    }
}


Step 2: add configuration dependency to the existing SCA provider project, for example:

  <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  </dependency>

Step 3: add the project SCA provider application Change the name of YML to bootstrap YML (highest startup priority) and add the configuration center configuration. The code is as follows:

spring:
  application:
    name: sca-provider
    <!--Registration Center-->
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        <!--Configuration center-->
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yml 

New Nacos configuration

Open the nacos configuration center and create a new configuration, as shown in the figure:

Where, the value of Data ID should be the same as bootstrap Spring defined in YML application. The value of name is the same (service name - if there are multiple services, multiple configuration instances will be created, and different service objects have different configuration instances) After the configuration is published, our configuration will be displayed in the configuration list

Test Nacos data reading

After the configuration is created, start the SCA provider service, open the browser, and enter

http://localhost:8081/provider/log/doLog01 , check the idea console log output Then open the nacos console to dynamically update the log level, access resources and detect the background log output

Then modify the log level of nacos configuration center, refresh the browser, and check whether the log output will change

@Application of RefreshScope annotation

For the nacos configuration center, there are internal

Keywords: Java Spring Cloud Microservices

Added by zrocker on Thu, 30 Dec 2021 16:05:22 +0200