Creation and use of Nacos online configuration file
(key) offline configuration files can be used for configuration management in the development stage. When the service is online, all local configurations can be added to Nacos configuration management for online configuration management.
1.Nacos client settings
1.1 launch and open the Nacos client page
localhost:8848/nacos
1.2 create a new namespace
As follows:
Namespace name: dev Description: development phase
1.3 switch to configuration management - configuration list, select a new namespace (e.g. dev), and create an online configuration file;
New configuration Data ID:datasource.properties Group:dev Description: data source Configuration format: Properties Configuration content: spring.datasource.username=root spring.datasource.password=123456789 spring.datasource.url=jdbc:mysql://192.168.57.10:3306/qilemo_pms spring.datasource.driver-class-name=com.mysql.jdbc.Driver
1.4 add all local configuration files (application.yml, application.properties, etc.) under the current microservice to the online configuration management of Nacos, and use the above methods to add them one by one.
Data Id Group datasource.properties dev mybatis.yml dev other.yml dev qilemo-product.properties dev
2.Java configuration center configuration item (bootstrap.properties)
2.1 configuration center namespace: (if it is not set, all configuration files will be found under the public namespace by default. If it cannot be found, it will be found from the local configuration file application.yml)
spring.cloud.nacos.config.namespace=1f667456-8ebd-44c2-a4d2-8960c6cd089b
2.2 configuration center configuration Group: (if it is not set, it will be found from the DEFAULT_GROUP group by default. If it cannot be found, it will be found from the local configuration file application.yml)
spring.cloud.nacos.config.group=dev
2.3 configuration center configuration file name (Data ID): (if it is not set, the micro service name or the configuration in the micro service name. Properties will be used by default. If it cannot be found, it will be found from the local configuration file application.properties)
spring.cloud.nacos.config.name=qilemo-product.properties
The first three items are fully configured as follows:
spring.cloud.nacos.config.namespace=b1d1432e-ad7d-40bd-90e4-0420c84c1926 spring.cloud.nacos.config.group=dev #spring.cloud.nacos.config.name=qilemo-product.properties
2.4 if there are multiple configuration files (for example: datasource.properties, mybatis.yml, other.yml, qilemo-product.properties), use the following command to add them;
spring.cloud.nacos.config.extension-configs[0].data-id=datasource.propertiesspring.cloud.nacos.config.extension-configs[0].group=devspring.cloud.nacos.config.extension-configs[0].refresh=truespring.cloud.nacos.config.extension-configs[1].data-id=mybatis-plus.ymlspring.cloud.nacos.config.extension-configs[1].group=devspring.cloud.nacos.config.extension-configs[1].refresh=truespring.cloud.nacos.config.extension-configs[2].data-id=other.ymlspring.cloud.nacos.config.extension-configs[2].group=devspring.cloud.nacos.config.extension-configs[2].refresh=true
The above complete configuration contents are as follows: (the namespace namespace is a global configuration, and the locations of all the following configuration files are found under this namespace. If it is not set, it is found under the public public namespace by default)
spring.cloud.nacos.config.namespace=b1d1432e-ad7d-40bd-90e4-0420c84c1926spring.cloud.nacos.config.group=dev#spring.cloud.nacos.config.name=datasource.propertiesspring.cloud.nacos.config.extension-configs[0].data-id=datasource.propertiesspring.cloud.nacos.config.extension-configs[0].group=devspring.cloud.nacos.config.extension-configs[0].refresh=truespring.cloud.nacos.config.extension-configs[1].data-id=mybatis-plus.ymlspring.cloud.nacos.config.extension-configs[1].group=devspring.cloud.nacos.config.extension-configs[1].refresh=truespring.cloud.nacos.config.extension-configs[2].data-id=other.ymlspring.cloud.nacos.config.extension-configs[2].group=devspring.cloud.nacos.config.extension-configs[2].refresh=true
3. Start the microservice for testing. Before testing, please comment out the configuration information that the local configuration file (application.yml) has been configured online
3.1 the configuration files added above are: datasource properties,mybatis.yml,other.yml,qilemo-product.properties; Apply Just comment out all the contents in the YML configuration file.
#spring:# datasource:# username: root# password: 123456789# url: jdbc:mysql://192.168.57.10:3306/qilemo_pms# driver-class-name: com.mysql.jdbc.Driver# cloud:# nacos:# discovery:# server-addr: localhost:8848# application:# name: qilemo-product##mybatis-plus:# mapper-locations: classpath*:/mapper/**/*.xml# global-config:# db-config:# id-type: auto##server:# port: 20001
4. View endpoint information (please see the following article for details)
If the service can still be started normally without exception when the local data source is unavailable, it indicates that the online configuration has taken effect. At this time, you can check whether the online configuration has been called through the Endpoint. For details, please check:
4.8 Nacos configuration management- Endpoint Information viewing.md