Nacos Configuration Center

This article describes the spring cloud integration nacos Case

The official document: https://nacos.io/zh-cn/docs/what-is-nacos.html](https://nacos.io/zh-cn/docs/what-is-nacos.html
stand-alone deployment:

1.git cloning project
git clone https://github.com/alibaba/nacos.git

2. Go to the root directory
cd nacos/

3.maven compilation
mvn -Prelease-nacos clean install -U

3. Go to the directory
cd distribution/target/nacos-server-${version}/nacos/bin1
Note $ {version} According to the version of their choice

4. Start Services
Linux / Unix / Mac environment: sh startup.sh -m standalone
win environment:cmd startup.cmd

After starting the print -


image.png

After a successful start, access the print ip, the default port 8848


image.png

Default Account: nacos
default password: nacos

Next comes integrated nacos Code:

1.maven coordinates

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>${latest.version}</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.
I was more than spring Boot 2.x so choose 0.2.1.RELEASEversion

2. Add the configuration file Note: File names must be so
bootstrap.properties

#指定开发环境
spring.profiles.active=dev
#服务器地址
spring.cloud.nacos.config.server-addr=127.0.01:8848
#默认为Public命名空间,可以省略不写
spring.cloud.nacos.config.namespace=Public
#指定配置群组 --如果是Public命名空间 则可以省略群组配置
spring.cloud.nacos.config.group=DEFAULT_GROUP
#文件名 -- 如果没有配置则默认为 ${spring.appliction.name}
spring.cloud.nacos.config.prefix=member
#指定文件后缀
spring.cloud.nacos.config.file-extension=yaml

If you follow the above nacos Configuration Center must be configured in accordance with the convention!

image.png

Data ID = ${spring.cloud.nacos.config.prefix}.${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} 最终拼接出来的就是:member-dev.yaml  (一定要注意约定!)
Group = ${spring.cloud.nacos.config.group}

The project configuration files into them all, select your own file format, and finally start on it. Currently this configuration is the simplest, but not necessarily meet the multi-switch environment issues, nacos configuration mode to configure multiple, given the official variety: https://github.com/spring-cloud-incubator/spring-cloud- alibaba / wiki / Nacos-config we can a lot of reference.

A lot of personal research program, is currently used to separate configuration environment through the namespace, is to create multiple sets namespace.

image.png

Add namespace:
image.png

Configuration is as follows:

#服务器地址
spring.cloud.nacos.config.server-addr=127.0.01:8848
#namespace 注意这里是nacos生成的字符串而不是dev
spring.cloud.nacos.config.namespace=527026c2-5e3b-4732-a5ac-e40173bf9397

#第一组配置(common配置是公共属性 数据库、redis、mq等,dev全局一套)
spring.cloud.nacos.config.ext-config[0].data-id=common.yaml
spring.cloud.nacos.config.ext-config[0].group=common-config

#业务模块配置(独立配置属性)
spring.cloud.nacos.config.ext-config[1].data-id=member.yaml
spring.cloud.nacos.config.ext-config[1].group=member-config
#动态刷新
spring.cloud.nacos.config.ext-config[1].refresh=true



  1. The higher ext-config [n] greater number loading priority.
  2. According to their business requirements, you can configure multiple ext-config.
  3. When switching environment, you can simply change the local namespace.
  4. ext-config [n] .data-id value must be a file name extension, a file extension Properties can support, and can support yaml / yml
  5. Clustered environment and as a stand-alone configuration

#jekins or manual deployment contract:
the original startup parameters: --spring.profiles.active = dev
need to replace parameters: - spring.cloud.nacos.config.namespace = 43cfdf88-1a5e

1. encountered some problems: when to start the service environment, manually change startup.sh file, specify the corresponding jdk directory
2. strict accordance with the contract to profile name and dataId

If you have any questions, leave a message!

Original Address: https: //www.jianshu.com/p/3750b7be331f

Guess you like

Origin www.cnblogs.com/jpfss/p/12074329.html