Java Micro Services New Generation Nacos- Configuration Management

Foreword

In the above Nacos Java new generation of micro-services have been introduced micro-services framework Nacos and its function in the service registration and discovery, this paper will focus on its other powerful features: configuration management, learning how to use Nacos achieved through a unified management interface, centralized configuration of distributed services to achieve and maintain real-time updates.

This article relates to the following:

  • Nacos Configuration Management Concepts
  • How Nacos Management Configuration
  • Nacos multi-environment configuration management

Introduction Nacos Configuration Management

Nacos provides dynamic configuration services, allow us to change the service application can be configured in real time, so that configuration management becomes more efficient and faster. It stores the application configuration, and other metadata information based on key / value, there is provided a server and client of the external support system is configured as a distributed.

First, understand a few important concepts under Nacos in the configuration management module, can help us better understand and correct use Nacos configuration management.

  • Namespace (Namespace)

    Isolating tenant for configuring particle size, it can be used to segregate to different environments of the configuration, such as resource development test and production environments (such as configuration service) isolation.

  • CI (Configuration Item)

    A specific configurable parameters to their values, usually in the form param-key = param-value of. For example, we configure the system log output level (logLevel = INFO | WARN | ERROR) is a configuration item.

  • Configuration Set (Configuration Set)

    A collection of related or unrelated CI. A configuration file is usually a configuration set, it may contain a data source, the thread pool, log levels and other configuration items.

  • Arrangement collection ID (Data ID)

    Identify a set of configuration ID, used for tissue division system configuration set. A system or application may comprise a plurality of configuration sets. The official recommended Java class package (such as com.taobao.tc.refund.log.level) naming convention defined Data ID to ensure global uniqueness.

  • Configuration packet (Group)

    Grouping configuration set, Data ID for distinguishing the same configuration set. Defaults DEFAULT_GROUP. The configuration packet common scenario: different applications or components using the same type of configuration, such as message queues and configuration database_url Topic configuration.

Start Nacos Configuration Management

To use Nacos configuration management, you first need to start Nacos server, and client access on the server Nacos start specific operations can be found in Nacos Java new generation of micro-services .

Nacos launched the service side, we first configuration management on Nacos a new console in the simplest configuration, as shown:

image-20190623141751675

After successfully saved list can configure the look created records as follows:

image-20190623141953948

Action Bar Each configuration set is provided View and modify the configuration of the clearance, as well as to view additional sample code to guide how we use Nacos read the configuration set on the client side, currently only supports Java, Spring Boot, Spring Cloud, other language version is still perfect language.

! [Image-20190623142049041] ( ww3.sinaimg.cn/large/006tN... integrated manner based technology stack Spring

Nacos Spring

  1. First, add the program to the Spring dependent

    <dependency>
        <groupId>com.alibaba.nacos</groupId>
        <artifactId>nacos-spring-context</artifactId>
        <version>0.2.3-RC1</version>
    </dependency>
    复制代码

    The latest version is available in the maven repository mvnrepository.com get in.

  2. Add @EnableNacosConfigannotations to enable Nacos Spring configuration management services. The following example, we use @NacosPropertySourcenotes to load a configuration set we created earlier, and to specify automatic refresh configuration.

  3. By Nacos the @NacosValueannotation configuration items and attributes binding.

  1. After starting the program, open http://localhost:8080/config/helloto obtain returns the result hello,test, it means reading the configuration as follows success.

  2. After reading success we then try to modify the dynamic refresh characteristic of Nacos configuration information under this configuration item validation. There are two ways to modify, in a manual modification Nacos console, another command line using the API provided by the modified directly:

    curl -X POST "http://127.0.0.1:8848/nacos/v1.0.1/cs/configs?dataId=com.one.learn.nacos.config&group=DEFAULT_GROUP&content=message=nacos-spring"
    复制代码
  3. Visit again http://localhost:8080/config/hello, this time to return to the contents hello,nacos-spring, return information changes show that the program's messagevalue has been dynamically updated.

Nacos Spring Boot

  1. Adding dynamic configuration change depends on:

    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>nacos-config-spring-boot-starter</artifactId>
        <version>0.2.1</version>
    </dependency>
    复制代码

    Note : The version here 0.2.x.RELEASE corresponds Spring Boot 2.x version, version 0.1.x.RELEASE corresponds Spring Boot 1.x version.

  2. In the application.propertiesconfiguration of the address Nacos Server:

nacos.config.server-addr=127.0.0.1:8848
复制代码
  1. Use @NacosPropertySourceload dataIdfor the com.one.learn.nacos.configconfiguration source, and turn on Automatic Updates:

  2. Nacos by the @NacosValueannotation property set value, the step of integrating Spring as content.

  3. Boot class open http://localhost:8080/config/helloobtain return a result hello,nacos, the configuration has been described in the program read successfully.

Nacos Spring Cloud

If you use Spring Cloud program, Nacos provides dependent libraries spring-cloud-starter-alibaba-nacos-configdynamically change to achieve configuration.

  1. Add dependence:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        <version>0.9.0.RELEASE</version>
    </dependency>
    复制代码

    Note : The version 0.2.x.RELEASE corresponds Spring Boot 2.x version, version 0.1.x.RELEASE corresponds Spring Boot 1.x version.

  2. In bootstrap.propertiesthe configuration Nacos server application name and address as follows, in Nacos Spring Cloud, the dataIddefault is spring.application.nameadd propertiesthe file extension, so, in order to read the configuration is correct, we need to configure the Nacos set ID is com.one.learn.nacos.configadjustedcom.one.learn.nacos.config.properties

    spring.cloud.nacos.config.server-addr=127.0.0.1:8848
    spring.application.name=com.one.learn.nacos.config
    复制代码

    Bootstrap.properties used here as a configuration Nacos place, because the load order Spring Boot configuration file, followed by bootstrap.properties -> bootstrap.yml -> application.properties - > application.yml, in applicationconfiguring Nacos the hierarchy can not take effect.

    Note: When using a domain name way to access Nacos, spring.cloud.nacos.config.server-addrways must be configured 域名:port. Even using the domain name, the port can not be omitted. For example Nacos name as abc.com.nacos, listening on port 80, then spring.cloud.nacos.config.server-addr=abc.com.nacos:80.

  3. Spring Cloud notes by native @RefreshScopeupdated automatically achieve configuration:

  4. Run the program, access http://localhost:8080/config/hello, content is returned hello,nacos, it indicates that the configuration reading success.

  5. Nacos console modified configuration items message=Nacos Spring Cloud, visit again http://localhost:8080/config/hello, returns the contents as hello,Nacos Spring Clouddescribed in the program messagevalue has been dynamically updated.

Nacos multi-environment configuration management

Nacos achieve the above basic configuration read and update, then we look Nacos how to manage and configure multiple environments, to note that this feature is only for use in Spring Cloud.

Suppose we have two environmental program: testing, production environment requires management to configure, in Spring Boot program, the default configuration file for application.properties, the traditional way is to use Spring Profile feature, store multiple items in the corresponding environment configuration files, file format application-${env}-properties, and the need spring.profile.activeto configure the environment to which the application when starting.

Nacos Config mainly through dataId and uniquely determined to a group arranged in Nacos Spring Cloud, the dataIdfull format is as follows:

 ${prefix}-${spring.profile.active}.${file-extension}
复制代码
  • prefixThe default is spring.application.namethe value, you can also configure the item spring.cloud.nacos.config.prefixto configure.

  • spring.profile.activeThe current environment is the corresponding profile, details can refer to the Spring Boot documentation .

    Note: When spring.profile.active is empty, the corresponding connector - will not exist, DataID splicing into the format {prefix} {file-extension}.

  • file-exetensionTo configure the data format of the content, by using the configuration can spring.cloud.nacos.config.file-extensionbe configured. Currently only supports propertiesand yamltypes.

group by default DEFAULT_GROUP, you can spring.cloud.nacos.config.groupcustomize the specified name.

  1. Based on the above described configuration rules, we can first create a new configuration set two different environments in Nacos console, as follows

image-20190623171248544

Two configuration items are stored to propertymemory file format.

#nacos-config-prod.properties
server.port=8091
message=nacos-config-prod

#nacos-config-test.properties
server.port=8081
message=nacos-config-test
复制代码
  1. Then in the main configuration file for the program application.propertiesis configured as follows:
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.application.name=nacos-config
spring.cloud.nacos.config.file-extension=properties
spring.profile.active=test
复制代码
  1. Written test controller class ConfigController, the configuration items and attribute values ​​for binding.

  2. Start the program, you can see the following data results, indicating that the port configuration test environment has taken effect.

    Then accessed through a browser http://localhost:8081/config/hello, to return hello,nacos-config-test.

  3. Modify the configuration file application.propertiesin spring.profile.active, and restart the program accesshttp://localhost:8091/config/hello

    spring.cloud.nacos.config.server-addr=127.0.0.1:8848
    spring.application.name=nacos-config
    spring.cloud.nacos.config.file-extension=properties
    spring.profile.active=prod
    复制代码

    Can return results obtained hello,nacos-config-prod, also shows another configuration program environment handover success.

Configuring the Listener

In addition to the active test configuration to take effect outside, we can monitor configuration changes by adding a listener, to achieve very simple to use @NacosConfigListeneryou can, once the specified configuration set ID corresponding to the configuration changed, the listener will receive the callback, will All configuration information is returned as a string.

When a plurality of configuration items, configuration callback received string with a line format, needs its own process.

Of course, @NacosConfigListeneralso supports conversion type, such as transforming Properties object

Namespace Best Practices

Nacos in Namespace as tenant granularity segments exist mainly to solve the multi-multi-tenant environments and data ( configuration and service ) isolate the problem.

  • If only one tenant (user), different namespce may correspond to different environments, in order to achieve isolation configuration environment, the effect is just the section: Nacos similar multi-environment configuration management.

    img

  • If there are multiple tenants, assign a different namespace for each tenant, so that each tenant (user) configuration data and registration data will be attributed to the service under their own namespace, you can achieve multi-tenant configuration data isolation.

    img

    Note: Nacos not yet implemented the distribution and isolation account permissions, so that each tenant can not manage their own configuration, this one function is still planning.

After a brief introduction, and then look at the best practices related to the namespace:

  • How to get the value of the namespace.
  • namespace parameter initialization method.

Create a namespace

In nacos functions on the left side of the console to see a namespace feature, you can click on to see the new namespace button, then this time you can create your own namespace up. After creating a successful, will generate a namespace ID , it is mainly used to avoid namespace name there may be cases of the same name will appear. So when you need to configure the application to specify the namespace, fill in the namespace ID .

image-20190623180238812

Associated command space

Not explicitly specified ${spring.cloud.nacos.config.namespace}under configuration, the default is to use the Nacos Public this namespae. If you need a custom namespace, you can be achieved by the following configurations:

spring.cloud.nacos.config.namespace=b3404bc0-d7dc-4855-b519-570ed34b62d7
复制代码

This configuration file must be placed bootstrap.properties. Further spring.cloud.nacos.config.namespacevalues are namespace corresponding id.

Epilogue

Come here, come here to learn about Nacos configuration management came to an end, of course, in the configuration management and more usage, Nacos official document also describes in great detail, it can also be more access to the official website. I will continue to follow-depth study of other ecological services Spring Cloud Alibaba micro components, welcome interested partners can focus on my small micro-channel public number, the more a week.

No public

The sample code

Sample project: nacos-Actions : github.com/wrcj12138aa...

Environment Support:

  • JDK 8
  • Maven 3.6.0
  • SpringBoot 2.1.0.RELEASE
  • SpringCloud Greenwich.RELEASE
  • SpringCloudAlibaba 0.9.0.RELEASE

reference

Guess you like

Origin juejin.im/post/5d0f841bf265da1b855c60b9