Nacos数据隔离模型
公司->命名空间->分组->服务
命名空间通常用于隔离不同微服务之间的配置
分组用于隔离相同微服务下不同环境的配置
版本对应关系
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
应用案例
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<spring-cloud-alibaba-version>2.2.9.RELEASE</spring-cloud-alibaba-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba-version}</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.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
yml中配置
spring:
application:
name: message-demo
cloud:
nacos:
discovery:
server-addr: localhost:8848,localhost:8850,localhost:8855
namespace: message #命名空间
group: dev #分组
config:
server-addr: localhost:8848,localhost:8850,localhost:8855
file-extension: yaml #指定文件类型
name: message-demo #配置ID
refresh-enabled: true #自动刷新
namespace: message #命名空间
group: dev #分组
extension-configs: #扩展配置
- data-id: message-sharding.properties
group: DEFAULT_GROUP
refresh: true
shared-configs: #共享配置
- data-id: test-shared.yaml
group: test01
refresh: true
主启动类增加
@EnableDiscoveryClient
注解
服务启动成功之后控制台会打印acos registry, dev message-demo 169.254.64.5:8090 register finished代表注册成功并在Nacos的控制台可以看到服务信息