1.新建nacos配置
2.新建maven项目
一个父工程nacos-config-demo,2个module服务
父工程pom.xml
<?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.leo.nacos</groupId>
<artifactId>nacos-config-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>service1</module>
<module>service2</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.0</version>
</plugin>
</plugins>
</build>
</project>
service1
(1)pom.xml
<?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">
<parent>
<artifactId>nacos-config-demo</artifactId>
<groupId>com.leo.nacos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service1</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
(2)配置 bootstrap.yml
server:
port: 56010
spring:
application:
name: service1
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
file-extension: yaml
namespace: 0c74cab7-f503-4fa8-af6d-25a5b65f598c
group: TEST_GROUP
ext-config[0]:
data-id: ext-config-db.properties
ext-config[1]:
data-id: ext-config-redis.properties
group: GLOBAL_GROUP
ext-config[2]:
data-id: ext-config-refresh.properties
group: REFRESH_GROUP
refresh: true # 支持动态刷新
(3)启动类
package com.simon.nacos.serviceone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class ServiceOneBootstrap {
@Autowired
ConfigurableApplicationContext applicationContext;
public static void main(String[] args) {
SpringApplication.run(ServiceOneBootstrap.class, args);
}
@GetMapping("/configs")
public String getConfigs()
{
return applicationContext.getEnvironment().getProperty("common.name");
}
@GetMapping("/configs2")
public String getConfigs2()
{
String dbUsername = applicationContext.getEnvironment().getProperty("database.username");
String dbPassword = applicationContext.getEnvironment().getProperty("database.password");
String redisHost = applicationContext.getEnvironment().getProperty("redis.host");
String redisPort = applicationContext.getEnvironment().getProperty("redis.port");
String refresh = applicationContext.getEnvironment().getProperty("ext.name");
return dbUsername + dbPassword + redisHost + redisPort + refresh;
}
}
service2
(1)pom.xml
<?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">
<parent>
<artifactId>nacos-config-demo</artifactId>
<groupId>com.leo.nacos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service2</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
service2
(2)配置bootstrap.yml
server:
port: 56020
spring:
application:
name: service2
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
namespace: 0c74cab7-f503-4fa8-af6d-25a5b65f598c
group: TEST_GROUP
(3)启动类
package com.simon.nacos.servicetwo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class ServiceTwoBootstrap {
@Autowired
ConfigurableApplicationContext applicationContext;
public static void main(String[] args) {
SpringApplication.run(ServiceTwoBootstrap.class, args);
}
@GetMapping("/configs")
public String getConfigs()
{
return applicationContext.getEnvironment().getProperty("common.name");
}
}
3.测试结果