Nacos不仅可以作为注册中心使用,还可以作为配置中心使用
父工程pom文件添加模块
<modules>
<module>cloudalibaba-config-3377</module>
</modules>
子工程pom文件添加父工程标签
<parent>
<groupId>com.szyl</groupId>
<artifactId>SpringCloudAlibabaLEA</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
子工程pom文件添加配置中心依赖
本次主要添加配置中心的依赖,这个依赖可以在官网上找到:
<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>
</dependencies>
要注意的是这里我们要配置两个配置文件,因为Nacos同SpringCloud-config一样,在项目初始化时,要保证先从配置中心进行配置拉取,拉取配置之后,才能保证项目的正常启动。
springboot中配置文件的加载时存在优先级顺序的,bootstrap优先级高于application
这里bootstrap配置好了以后,作用是两个,第一个是让3377这个服务注册到Nacos中,第二个作用就是去Nacos中去读取指定后缀为yaml的配置文件:
bootstrap.yaml文件:
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:8848 # Nacos服务注册中心地址
config:
server-addr: localhost:8848 # Nacos作为配置中心地址
file-extension: yaml # 指定yaml格式的配置
application.yml文件:
spring:
profiles:
active: dev # 表示开发环境
package com.example.cloudalibabaconfig3377;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class CloudalibabaConfig3377Application {
public static void main(String[] args) {
SpringApplication.run(CloudalibabaConfig3377Application.class, args);
}
}
这里的@RefreshScope实现配置自动更新,意思为如果想要使配置文件中的配置修改后不用重启项目即生效,可以使用@RefreshScope配置来实现
package com.example.cloudalibabaconfig3377.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope //支持Nacos动态刷新
public class configClientController {
@Value("${config.info}")
private String configinfo;
@GetMapping("/config/info")
public String getConfiginfo(){
return configinfo;
}
}
在Nacos Spring Cloud中,dataId的完整格式如下(详情可以参考官网:):
${prefix}-${spring.profiles.active}.${file-extension}
1.`prefix` 默认为 `spring.application.name` 的值,也可以通过配置项 `spring.cloud.nacos.config.prefix`来配置。
2.`spring.profiles.active` 即为当前环境对应的 profile,详情可以参考 [Spring Boot文档](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles)。 **注意:当 `spring.profiles.active` 为空时,对应的连接符 `-` 也将不存在,dataId 的拼接格式变成 `${prefix}.${file-extension}`**
3.`file-exetension` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.file-extension` 来配置。目前只支持 `properties` 和 `yaml` 类型。
4.通过spring Cloud原生注解'@RefreshScope'实现配置自动更新。
5.所以根据官方给出的规则,我们最终需要再Nacos配置中心添加的配置文件的名字和规则为:
# ${prefix}-${spring.profiles.active}.${file-extension}
# ${spring.application.name}-${spring.profiles.active}.${file-extension}
# nacos-config-client-dev.yaml
# 微服务名称-当前环境-文件格式
dataIdd的完整格式和配置文件关系如下:
添加配置
config: info: nacos config center,version = 1
然后在配置中心就会看到刚刚发布的配置:
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- azee.cn 版权所有 赣ICP备2024042794号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务