Nacos installation
download
Download: github.com/alibaba/nac… Other versions can be downloaded: github.com/alibaba/nac…
configuration
- Unzip the download
- Go to the conf directory and find the nacos-mysql.sql file
- Open mysql, create a database called nacos, and import the SQL file from the previous step
- Go back to the conf directory and find the application.properties file
- Mysql > configure database address, account and password by default
Start the
- Open CMD and go to the bin directory
- Type the command
startup.cmd -m standalone
- The browser open http://127.0.0.1:8848/nacos/index.html
- The default account password is nacOS
The service registry
Pom file configuration
Reference is good, direct copy may be wrong. Note: After testing, you cannot register with NACOS without importing web dependencies.
<? The XML version = "1.0" encoding = "utf-8"? > < 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" > <artifactId>core</artifactId> <description> Core code </description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> The < version > 2.3.0. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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-sentinel</artifactId> </dependency> <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-config</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>Copy the code
Start class annotations
Annotate @SpringCloudApplication
@SpringCloudApplication
public class CoreApplication {
public static void main(String[] args) throws InterruptedException { SpringApplication.run(CoreApplication.class, args); }}Copy the code
Yml configuration
spring:
application.name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 127.0. 01.: 8848
Copy the code
Start the project
Open the NACOS background and you can see that the service has been registered successfully
Configuration center
Add the bootstrap.properties file
spring.application.name=nacos-provider
Spring. Cloud. Nacos. Config. Server - addr = 127.0.0.1:8848
spring.cloud.nacos.config.file-extension=yaml
spring.profiles.active=prod
# Disable auto refresh configuration (enabled by default)
#spring.cloud.nacos.config.refresh.enabled = false
Copy the code
Enter the nacOS background to add the configuration
- The format of the Data Id is
${spring.application.name}-${profile}. ${file-extension:properties}
or${spring.application.name}. ${file-extension:properties}
- According to the bootstrap.properties configuration, the Data Id is nacos-provider-prod.yaml
- If we don’t configure it
spring.profiles.active
, can be written as nacos-provider.yaml
- We added two configuration files, one nacos-provider-dev.yaml and one nacos-provider-prod.yaml. Modify bootstrap.properties
spring.profiles.active
You can switch to a different configuration.
Summary of configuration Center
In the past, we wrote the configuration information in the YML file of the project. If the configuration file was modified, the project would need to be restarted. Under the microservice architecture, we would have multiple services. In this case, the configuration can be written to the NACOS configuration center, and the configuration can be uniformly modified in the NACOS background. The service can read the latest configuration without restarting.