当前位置:网站首页>【微服务~Nacos】Nacos之配置中心
【微服务~Nacos】Nacos之配置中心
2022-07-30 00:19:00 【小前端而已】
??这里是【微服务~Nacos】,关注我学习云原生不迷路
??如果对你有帮助,给博主一个免费的点赞以示鼓励
欢迎各位??点赞??评论收藏
??专栏介绍
【微服务~Nacos】 目前主要更新微服务,一起学习一起进步。
??本期介绍
本期主要介绍微服务~Nacos
文章目录
搭建服务
- 项目名:nacos-config-2.1
添加坐标:
<dependencies>
<!-- web 启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos 服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos 配置-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
- 创建yml配置文件:bootstrap.yml
server:
port: 8072 # 端口号
spring:
application:
name: config-service # 服务名
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848 # nacos 服务地址
prefix: ${spring.application.name} #data ID的前缀,默认服务名
file-extension: yaml # data ID的后缀:config-service.yaml
group: DEFAULT_GROUP # 组名
discovery:
server-addr: 127.0.0.1:8848 #nacos服务地址
创建服务
- 编写启动类
package com.czxy.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestNacosCloudConfigApplication {
public static void main(String[] args) {
SpringApplication.run(TestNacosCloudConfigApplication.class, args);
}
}
- 编写处理类
package com.czxy.nacos.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value("${czxy.message:'默认值'}")
private String msg;
/**
* http://localhost:8072/config/get
*/
@RequestMapping("/get")
public String get() {
return msg;
}
}
查询服务
- 情况1,访问“默认数据”
http://localhost:8072/config/get
情况2,访问yml文件配置信息
czxy:
message: 测试数据
- 情况3:访问nacos中的配置数据
整合知多少
- 在 Nacos Spring Cloud 中,
dataId
的完整格式如下
p r e f i x − {prefix}- prefix−{spring.profile.active}.${file-extension}
显示profile中的数据
- 1)编写profile:application-demo.yml
server:
port: 8073 # 端口号
czxy:
message: demo数据
2)修改启动项
-Dspring.profiles.active=demo
3)删除nacos配置后,测试
nacos 默认配置
- 1)nacos配置
config-service.yaml
的 Data ID
2)测试
nacos 配置 profile
- 1)配置nacos profile :
config-service-demo.yaml
2)测试
nacos配置文件加载顺序
spring boot、nacos各种配置文件的加载顺序
1.bootstrap.yml
2.application.yml
3.application-[profile].yml
4.[serviceName].yml #nacos配置
5.[serviceName]-[profile].yml #nacos配置
- 后面加载的文件,将覆盖前面文件的配置内容
边栏推荐
- EA & UML Sun Gong Yip - Multitasking Programming Super Introductory - (7) About mutex, you must know
- Laravel 预防 SQL 注入
- How Filebeat ensures that the log file is still correctly read when the log file is split (or rolled)
- "The lighthouse factory" of China path: smart roll out from point to surface
- 【励志】科比精神
- Worthington Dissociation Enzymes: Trypsin and Frequently Asked Questions
- 每周推荐短视频:研发效能是什么?它可以实现反“内卷”?
- Docker install MySQL8.0
- Low dropout linear regulator MPQ2013A-AEC1 brand MPS domestic replacement
- 工厂模式
猜你喜欢
[Training DAY16] ALFA [convex hull] [computational geometry]
外包干了五年,废了...
[Best training DAY16] KC's Can [Dynamic programming]
定时器学习
UE4 制作十字准心+后坐力
Music theory & guitar skills
Replace the executable file glibc version of the one
Worthington Dissociation Enzymes: Trypsin and Frequently Asked Questions
EA&UML日拱一卒-多任务编程超入门-(7)关于mutex,你必须知道的
Ubuntu中使用SQLite
随机推荐
EA & UML Sun Gong Yip - Multitasking Programming Super Introductory - (7) About mutex, you must know
News text classification
【集训DAY18】Welcome J and Z 【动态规划】
“ 我是一名阿里在职9年软件测试工程师,我的经历也许能帮到处于迷茫期的你 ”
Introduction to Worthington Elastase & Hyaluronidase
kubernets学习 -环境搭建
Add, delete, modify and query the database
7.27
Mysql internal and external connections
2022年企业直播行业发展洞察
Worthington解离酶:胰蛋白酶及常见问题
Genesis与Axis Ventures互动密切
图像的IO操作
Recurrent Neural Network (RNN)
Graph Theory: Bipartite Graphs
1592. 重新排列单词间的空格
【集训DAY18】有趣的交换【模拟】【数学】
what is a .pro file in qt
Worthington Optimized Technology: Cell Quantification
定时器学习