Nacos (seven): Nacos share configuration

Foreword

This article Reference article:
SpringCloud Alibaba - Nacos Config custom shared configuration

Prospects Review:

Previous chapters have introduced the most basic usage springcloud project combines Nacos, this paper introduce Nacos as a distribution center, how to read the shared configuration

My environment

  • Windows10
  • JDK8
  • SpringCloud:Finchley.RELEASE
  • SpringBoot:2.0.4.RELEASE
  • spring-cloud-alibaba-dependencies:0.2.2.RELEASE
  • Nacos-server:1.0.1

Demo of this project continue to be used before the polymerization engineering article Nacos, before the junior partner if not the environment, to be the source address download

Scene Description

After a project to increase the number of service profiles corresponding increase, there will be multiple configuration files in the same configuration, then we can apply the same configuration independent, as the project shared configuration files for each service, each service can sharing configuration by reading Nacos

Here with the next demo demo, if feasible

  • demo project: nacos-config-share
  • Profile: nacos-config-share.yml
  • Share profile: shareconfig1.yml, shareconfig2.yml

Create a project

As always, created or named in a polymerization engineering Nacos nacos-config-sharesub-project, before it was pom.xml file dependencies are consistent with the project, if you do not have previous project can refer to the source address

1, modify the startup class springbootNacosConfigShareApplication.java

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RefreshScope
public class NacosConfigShareApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigShareApplication.class, args);
    }

    @Value("${nacos.share}")
    private String share;


    @Value("${share.config1}")
    private String shareConfig1;

    @Value("${share.config2}")
    private String shareConfig2;

    @RequestMapping("/getValue")
    public String getValue() {
        return share;
    }

    @RequestMapping("/getShare1")
    public String getShare1() {
        return shareConfig1;
    }

    @RequestMapping("/getShare2")
    public String getShare2() {
        return shareConfig2;
    }
}

2, modify the project's profilebootstrap.yml

spring:
  application:
    name: nacos-config-share
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        prefix: ${spring.application.name}
        file-extension: yml
        shared-dataids: shareconfig1.yml,shareconfig2.yml
        refreshable-dataids: shareconfig1.yml,shareconfig2.yml

As can be seen from the configuration file, the shared-dataidsspecified configuration file to read the shared attributes DataID, the plurality of file ,partition
using refreshable-dataidsthe specified shared profile support auto-refresh

New Profile

Here we demonstrate, temporarily join Namespace, created directly in the public space and test

Create profiles nacos-config-share.yml, detailed as follows:

  • DataId:nacos-config-share.yml
  • Configuration format:YAML
  • Configuring Content:
    server: port: 9984 nacos: share: nacos-config-share

Create a shared profile 1 shareconfig1.yml, detailed as follows:

  • DataId:shareconfig1.yml
  • Configuration format:YAML
  • Configuring Content:
    share: config1: 这里是共享配置文件1

Create a shared profile 1 shareconfig2.yml, detailed as follows:

  • DataId:shareconfig2.yml
  • Configuration format:YAML
  • Configuring Content:
    share: config2: 这里是共享配置文件2

Once created, the configuration list as shown below:

shareconfig

Start the test

Directly start the project, if successful start. You can see the log information as follows:

startup

Start class interface to access provided, the test can get to values ​​shared configuration file

访问127.0.0.1:9984/getValue,返回:nacos-config-share
访问127.0.0.1:9984/getShare1,返回:这里是共享配置文件1
访问127.0.0.1:9984/getShare2,返回:这里是共享配置文件2

Under re-test refreshable-dataidsautomatically refresh configuration is in effect

In Nacos console to modify the configuration file shared shareconfig2.ymlvalues of:这里是共享配置文件2这里是共享配置文件2

After editing and saving, re-request 127.0.0.1:9984/getShare2, which returns the following results:

Here is a shared configuration file is shared here 2 Profile 2

By the above described return results specified in the configuration file shared-dataidsand refreshable-dataidscan read the configuration file sharing and auto-refresh.

The demand for change

Now suppose you want to read shareconfig3.ymland shareconfig4.ymlfile it as a Group SHARE3_GROUPand SHARE4_GROUPthat share the profile with the project itself not in the same Group Profiles in ( 上边的例子是全都在DEFAULT_GROUP分组) If you continue to use that method on top, you can not read the shared configuration file

Then you can use another configuration ext-config, it can be loaded custom configuration specified by the userDataID、Group以及是否自动刷新

And ext-configis a collection of ( List), to specify support for multiple profiles.

New Shared Profile

Create configuration profiles shareconfig3.ymland shareconfig4.ymlpay attention to their Group Properties

  • DataId:shareconfig3.yml
  • Group:SHARE3_GROUP
  • Configuration format:YAML
  • Configuring Content:
    share: config3: 这里是共享配置文件3,Group:SHARE3_GROUP
  • DataId:shareconfig4.yml
  • Group:SHARE4_GROUP
  • Configuration format:YAML
  • Configuring Content:
    share: config4: 这里是共享配置文件4,Group:SHARE4_GROUP

Creating success page as follows:

shareconfig34

Modify the project code

1, the starting class NacosConfigShareApplication.javafollowing new tag

    @Value("${share.config3}")
    private String shareConfig3;

    @Value("${share.config4}")
    private String shareConfig4;


    @RequestMapping("/getShare3")
    public String getShare3() {
        return shareConfig3;
    }

    @RequestMapping("/getShare4")
    public String getShare4() {
        return shareConfig4;
    }

2, modify the project configuration file bootstrap.yml, increase ext-configconfiguration

spring:
  application:
    name: nacos-config-share
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        prefix: ${spring.application.name}
        file-extension: yml
        shared-dataids: shareconfig1.yml,shareconfig2.yml
        refreshable-dataids: shareconfig1.yml,shareconfig2.yml
        ext-config:
          - data-id: shareconfig3.yml
            group: SHARE3_GROUP
            refresh: true
          - data-id: shareconfig4.yml
            group: SHARE4_GROUP
            refresh: true

Start testing

The project has been modified to be seen

  1. Under the project itself nacos profiles belong DEFAULT_GROUP, the default read
  2. Under shareconfig1.yml, shareconfig2.yml belong DEFAULT_GROUP, by shared-dataidsreading specified
  3. shareconfig3.yml, shareconfig4.yml fall 非DEFAULT_GROUPunder, by ext-configcustomizing read configuration properties

Start the project, test all of the configuration file can be read correctly

访问127.0.0.1:9984/getValue,返回:nacos-config-share
访问127.0.0.1:9984/getShare1,返回:这里是共享配置文件1
访问127.0.0.1:9984/getShare2,返回:这里是共享配置文件2这里是共享配置文件2
访问127.0.0.1:9984/getShare3,返回:这里是共享配置文件3,Group:SHARE3_GROUP
访问127.0.0.1:9984/getShare4,返回:这里是共享配置文件4,Group:SHARE4_GROUP

Modify shareconfig4.ymlthe configuration of content: 这里是共享配置文件4,Group:SHARE4_GROUP,支持自动刷新After saving, called again 127.0.0.1:9984/getShare4, returns the following:

Here is a shared configuration file 4, Group: SHARE4_GROUP, support auto-refresh

After the call interface found two ways to load sharing configuration can be read properly, and can be used together. ext-configThe way to achieve the sharing of user-defined configuration profiles.

to sum up

The above demo has been demonstrated Nacos sharing arrangement implemented in two ways, two ways for different scenarios, summarized as follows:

  • shared-dataidsthe way:
    • Suitable for a shared profile with the project default configuration file in the same Group, two direct command can handle
    • Advantages: easy to configure
    • Disadvantages: only in the same Group
  • ext-configthe way:
    • It can be customized shared configuration file to be read by the developer DataId, Group, refresh properties, so just to solve the shared-dataidslimitations of existence.
    • Advantages: can be shared-dataidsused in conjunction with the program, the user-defined configuration. Strong flexibility
    • Disadvantages: Configuration error-prone, be familiar with the YAML syntax

They have their own strengths can be seen in two ways, so if you need to use a shared configuration in development, we can be on the specific circumstances choose their most appropriate solution.

This article Source : https: //github.com/larscheng/larscheng-learning-demo/tree/master/Nacos



Guess you like

Origin www.cnblogs.com/larscheng/p/11416392.html