当前位置:网站首页>Nacos micro service ~ Nacos 】 【 configuration of the center
Nacos micro service ~ Nacos 】 【 configuration of the center
2022-07-30 00:33:00 【Just a small front end】
??这里是【微服务~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 Arch - State Diagram :: Redraw Button State Diagram
- 1592. 重新排列单词间的空格
- Go日志库——logrus
- 月薪15k的阿里测试岗,面试原来这么简单
- 机器人的运动范围
- 【集训DAY16】KC‘s Can 【动态规划】
- Navicat报错:1045-Access denied for user [email protected](using passwordYES)
- Reading notes. This is the psychology: see through the essence of the pseudo psychology (version 10)"
- How to design and implement report collaboration system for instruction set data products——Development practice of industrial collaborative manufacturing project based on instruction set IoT operating
- Finding a 2D Array
猜你喜欢

How do we-media people create explosive articles?These 3 types of articles are most likely to explode

@RequestParam注解的详细介绍
![[Best training DAY16] KC's Can [Dynamic programming]](/img/60/26280222faf72bd94c654b1810381d.png)
[Best training DAY16] KC's Can [Dynamic programming]

Linux-安装MySQL(详细教程)

He used to cells harvested trypsin & release procedure

KDE Frameworks 5.20.0:Plasma迎来诸多改进

『牛客|每日一题』走迷宫

【集训DAY16】ALFA【凸壳】【计算几何】

高德地图jsapi不生效 INVALID_USER_SCODE

Genesis与Axis Ventures互动密切
随机推荐
Replace the executable file glibc version of the one
Worthington优化技术:细胞定量
Worthington Papain & Chymotrypsin & DNase I
557. 反转字符串中的单词 III
工厂模式
Low dropout linear regulator MPQ2013A-AEC1 brand MPS domestic replacement
重建二叉树
“灯塔工厂”的中国路径:智造从点到面铺开
The range of motion of the robot
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
4 hotspot inquiry networks necessary for new media operations
绘制几何图形
Worthington解离酶:胰蛋白酶及常见问题
更换可执行文件glibc版本的某一次挣扎
外包干了五年,废了...
“ 我是一名阿里在职9年软件测试工程师,我的经历也许能帮到处于迷茫期的你 ”
图论:二分图
Towhee 每周模型
【集训DAY16】ALFA【凸壳】【计算几何】
Introduction to Worthington Elastase & Hyaluronidase