
配置中心configclient开发
nacos作为统一配置中心:
1.它管理的配置文件方式是在自己所在的服务器上形成一个版本库,因此不需要再创建远程版本库
2.nacos作为统一配置中心管理配置文件时,同样也存在版本控制
3、编写主启动类org.springframework.boot spring-boot-starter-webcom.alibaba.cloud spring-cloud-starter-alibaba-nacos-discoverycom.alibaba.cloud spring-cloud-starter-alibaba-nacos-config
package com.zww.spring.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClientApplication_8888 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication_8888.class,args);
}
}
4、进入nacos管理页面添加配置文件
-
1.新建命名空间
-
2.输入命名空间信息
-
3.点击 配置管理,再点击 配置列表
-
4.选择study命名空间
-
5.点击+按钮
-
6.编辑配置文件
-
7.编辑完成点 发布
5、编写bootstrap.yml配置文件,拉取远端配置#告诉config server地址 spring: cloud: nacos: server-addr: 127.0.0.1:8848 #告诉从哪个命名空间获取配置 config: namespace: f15dc566-c615-4dbe-88f9-0c812ea6e2f6 #告诉从哪个组进行配置获取 group: DEFAULT_GROUP #第一种拉取远端配置文件方式 #从哪个组拉取哪个配置文件 name: configclient-dev #拉取这个名字的哪个后缀文件 file-extension: properties #第一种拉取远端配置文件方式 #spring.cloud.nacos.config.prefix=configclient #spring.profiles.active=dev #spring.cloud.nacos.config.file-extension=properties6、编写controller层package com.zww.spring.springcloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DemoController { @Value("${constomer.username}") private String username; @GetMapping("/demo") public String demo(){ return "demo ok!!!"+username; } }7、启动测试 配置自动刷新 1、在controller层添加@RefreshScope实现自动刷新 2、修改远端配置文件 3、重新访问
-
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)