nacos Instead eureka + config good things, of course, use the config
1, set up the project, attach pom file
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hcj</groupId>
<artifactId>nacos-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-config</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.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>
</project>
2. Create a profile bootstrap.properties
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.application.name=nacos-config
server.port=1909
Many small partners contacted spring boot project, spring cloud projects all the time with application.properties or .yml file as a configuration file, here why use the bootstrap configuration file it? Take a look:
3. Create controller, calling in the config configuration items
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value("${useLocalCache:false}")
private boolean useLocalCache;
@RequestMapping("/get")
public boolean get() {
return useLocalCache;
}
}
Note:
- Spring Cloud native notes @RefreshScope achieve Configure Automatic Updates
- No need to modify the main function
- Do not worry at this time to start the project, config have not configured where's the configuration items read?
4, draw the focus - Added config configuration item to nacos server
There are two ways to add nacos server configuration items:
4.1, calling Nacos Open API released to Nacos Server Configuration
First, by calling Nacos Open API released to Nacos Server Configuration: dataId to nacos-config.properties, group as the default, content useLocalCache = true
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config.properties&group=DEFAULT_GROUP&content=useLocalCache=true"
4.2, using Nacos Server visual interfaces published to Nacos Server Configuration
Then click publish, we can be in the configuration management Nacos Server - see our newly added configuration item configuration list
5, start the service, call interface configuration item to check whether to read
访问:http://localhost:1909/config/get
6, of course Nacos Server UI can also modify, delete, add configuration items
At this point you're done!