One, foreword
Microservices divide applications into multiple services. If there are many services, it is difficult to manage configuration files. In this case, the configuration management center is required to manage the configuration files of each service application in a unified manner.
Add dependencies
Add the spring-cloud-starter-alibaba-nacos-config dependency to pop.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
Copy the code
Because bootstrap.properties is used, you need to add the spring-cloud-starter-bootstrap dependency; otherwise, the bootstrap configuration will not take effect
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
Copy the code
The complete POM.xml is shown below
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.llh</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consumer</artifactId>
<version>1.0.0</version>
<name>consumer</name>
<description>consumer description</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
</project>
Copy the code
Third, the bootstrap. The properties
Delete application.properties and create bootstrap.properties as follows:
# application name spring. Application. Name = # consumer nacos configuration center spring. Cloud. Nacos. Config. The server - addr = http://localhost:8848 spring.cloud.nacos.config.file-extension=properties spring.cloud.nacos.config.group=DEFAULT_GROUP spring.cloud.nacos.config.namespace=83e9d384-d49e-4f40-84bf-c25612883dcc spring.cloud.nacos.config.username=nacos Spring. Cloud. Nacos. Config. Password = # nacos nacos allocation center Public profile spring.cloud.nacos.config.shared-configs[0].data-id=common.properties spring.cloud.nacos.config.shared-configs[0].refresh=true spring.cloud.nacos.config.shared-configs[0].group=DEFAULT_GROUPCopy the code
- Note:
spring.cloud.nacos.config.namespace
Consistent with namespace in NACOS. - This is configured
common.properties
Common configuration files, such as registries for all services, do not need to be configured for each service.
Create a configuration file with Nacos
Create two configuration files consumer.properties and common.properties
Consumer.properties is the consumer configuration file that simply configures the startup port. Note that the consumer.properties prefix is the same as spring. Application. Name in bootstrap.properties.
Consumer.properties is as follows
# server port. The port = 8888Copy the code
Common.properties is a common configuration file that contains registrie-specific configuration information. The following
# registry related spring. Cloud. Nacos. Discovery. Server - addr = http://localhost:8848 spring.cloud.nacos.discovery.namespace=83e9d384-d49e-4f40-84bf-c25612883dcc spring.cloud.nacos.discovery.username=nacos spring.cloud.nacos.discovery.password=nacosCopy the code
After starting the project, you can see that the port is 8888 and the service is registered with the NACOS registry, but the project isbootstrap.properties
Port and service registration information are not configured. Therefore, port configuration information and registration configuration information are dynamically read from the configuration center
Five, the conclusion
Source code address: github.com/tigerleeli/…
Nacos production environment needs to configure persistence, it is the default store file, this is not safe, generally use Mysql persistence. Production environments may require Nacos clustering, which can be found on the official website.
Synchronize wechat official account: Little Tiger’s technology blog