Spring Cloud Nacos combat (5) - the relationship between namespace grouping and DataID

The relationship between Nacos namespace grouping and DataID

Glossary

Namespace _

​ Used for tenant granular configuration isolation. The configuration of the same Group or Data ID can exist under different namespaces. One of the common scenarios of Namespace is the separation and isolation of configurations in different environments, such as the isolation of resources (such as configurations and services) in development and test environments and production environments.

Configure grouping (Group)

A set of configuration sets in Nacos is one of the dimensions of organizational configuration. Group configuration sets by a meaningful string (such as Buy or Trade) to distinguish configuration sets with the same Data ID. When you create a configuration on Nacos, if you do not fill in the name of the configuration group, the name of the configuration group defaults to DEFAULT_GROUP. Common scenarios for configuration grouping: Different applications or components use the same configuration type, such as database_url configuration and MQ_topic configuration.

Configuration Set ID (Data ID)

​ The ID of a configuration set in Nacos. The configuration set ID is one of the dimensions by which configurations are organized. Data IDs are often used to organize configuration sets for partitioned systems. A system or application can contain multiple configuration sets, and each configuration set can be identified by a meaningful name. Data ID usually adopts the naming rules of Java-like packages (such as com.taobao.tc.refund.log.level) to ensure global uniqueness. This naming rule is not mandatory.

​ Configuration set: A collection of related or unrelated configuration items is called a configuration set. In the system, a configuration file is usually a configuration set, which contains the configuration of all aspects of the system. For example, a configuration set may contain configuration items such as data source, thread pool, and log level.

Three relationship

The relationship between these three is similar to the package name and class name in Java. The outermost Namespace can be used to distinguish the deployment environment. Group and DataID logically distinguish two target objects.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-L8jjrMam-1676792124151)(sanzheguanxi.png)]

default

Namespace=public, Group=DEFAULT_GROUP, the default Cluster is DEFAULT

specific situation

​ The default namespace of Nacos is public, and we can use Namespace to achieve isolation. For example, we now have three environments: development, testing, and production environments. We can create three Namespaces, and different Namespaces are isolated.

​ Group itself means grouping, which can divide different microservices into the same group.

​ The rest is specific microservices. A Service can contain multiple Clusters. Nacos defaults the Cluster to DEFAULT, and a Cluster is a virtual division of a specified microservice. For example, if a Service is deployed in the computer rooms of Beijing and Hangzhou, the Service in the Beijing computer room can be named (BJ), and the Service in the Hangzhou computer room can be named (HZ), so that the same The microservices in the computer room call each other to improve performance.

Switch between different environments

DataID scheme

  1. Specify spring.profile.active and the DataID of the configuration file to read different configurations in different environments. Configuration rules: default space + create two DataIds of dev and test:

    1. Create a new dev configuration DataID (already created)
    2. New test configuration DataID

    [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-bjTquGf6-1676792124153)(image-20210926170016223.png)]

  2. The configuration file can be read in multiple environments through the spring.profile.active property

    [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-1qiQs6rx-1676792124153)(image-20210926170339921.png)]

    [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-3YggPOhS-1676792124154)(image-20210926170620844.png)]

    spring:
      profiles:
        # active: dev #表示开发环境
        active: test #表示测试环境
    
  3. test

    Finally visit again: http://localhost:3377/localhost/config/info

    [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-C8PcM1JN-1676792124154)(image-20210926170942161.png)]

Group plan

​ According to the previous explanation, we all know that Group is a group by default: DEFAULT_GROUP, so now we need to separate two groups, one is "Dev development group", and the other is "test test group"

  1. New Group

    DEV_GROUP

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-Rrya5ATC-1676792124155)(image-20210926171610558.png)]

​ TEST_GROUP

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-X1uRPfRG-1676792124155)(image-20210926171904382.png)]

Configuration list, the two DataIDs are the same, but the configuration files are grouped differently:

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-LXvg9RjS-1676792124156)(image-20210926171954022.png)]

  1. After completing the above configuration, the next step is to cooperate with bootstrap+application. The specific method: add a Group configuration under config, which can be configured as DEV_GROUP or TEST_GROUP

​ bootstrap:

# nacos配置
server:
  port: 3377

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #指定yaml格式的配置
        group: TEST_GROUP #增加分组

​ application:

spring:
  profiles:
    # active: dev #表示开发环境	
    # active: test #表示测试环境
    active: info
  1. Test (you can test different groups):

    Finally visit again: http://localhost:3377/localhost/config/info

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-cJeJSqnU-1676792124156)(image-20210926173730298.png)]

Namespace space scheme

​ We have completed the DataID scheme and the Group scheme, and then let’s take a look at the Namespace (namespace) scheme, which is public by default and cannot be deleted. Let’s create two new namespace schemes now

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-ELXLt2v3-1676792124157)(image-20210926174051313.png)]

  1. Create a new dev/test Namespace

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-9Z92ioBd-1676792124157)(image-20210926174422526.png)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-Wlgq1o6i-1676792124157)(image-20210926174807843.png)]

  1. Service Management - Service List Page View

    At this point, we can switch the corresponding namespace, and then switch through the Group and DataID. If we want to switch the namespace, we need to switch through the namespace ID

    [External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-AZTONPDU-1676792124158)(image-20210926174422526.png)]

  2. Write according to the domain name configuration (Namespace+Group+DataId)

    ​ First confirm the namespace used by the namespace namespace ID

    [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-ru1uAwus-1676792124159)(image-20210926175605787.png)]

    # nacos配置
    server:
      port: 3377
    
    spring:
      application:
        name: nacos-config-client
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848 #Nacos服务注册中心地址
          config:
            server-addr: localhost:8848 #Nacos作为配置中心地址
            file-extension: yaml #指定yaml格式的配置
            group: TEST_GROUP # 指定分组
            namespace: 4ba4bf0e-210c-41ce-954c-23538de1dcbc #指定命名空间
    
    spring:
      profiles:
        active: dev #表示开发环境
        # active: test #表示测试环境
        # active: info
    

    Then go back to the Nacos configuration list, add the configuration under the Dev namespace, and add 3 items

    [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-bynn1GVX-1676792124159)(image-20210926180321810.png)]

    [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-4uiXyeW2-1676792124159)(image-20210926180120552.png)]

    [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-5IipOfAc-1676792124160)(image-20210926180520423.png)]

  3. Test: Visit http://localhost:3377/localhost/config/info to test

Guess you like

Origin blog.csdn.net/qq_27566167/article/details/129111081