当前位置:网站首页>【微服务~Nacos】Nacos之配置中心
【微服务~Nacos】Nacos之配置中心
2022-07-30 01:21: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配置
- 后面加载的文件,将覆盖前面文件的配置内容
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢
随机推荐
会议OA之待开会议&&所有会议
Based on TNEWS 'today's headline news in Chinese short text classification
Selenium上传文件有多少种方式?不信你有我全
Replace the executable file glibc version of the one
验证框架-01
【VMWARE--共享文件】
[MySQL series] MySQL database foundation
1.2Recyclerview实现Item点击事件
【C Primer Plus第九章课后编程题】
Self-study HarmonyOS application development (56) - Use Service to ensure that the application runs continuously in the background
更换可执行文件glibc版本的某一次挣扎
Weekly recommended short video: What is R&D efficiency?It can achieve anti "involution"?
Navicat报错:1045-Access denied for user [email protected](using passwordYES)
自学HarmonyOS应用开发(47)- 自定义switch组件
【Flutter】Flutter inspector 工具使用详解,查看Flutter布局,widget树,调试界面等
My first understanding of MySql, and the basic syntax of DDL and DML and DQL in sql statements
Print linked list from end to beginning
[Flutter] Flutter preloading of mixed development solves the problem of slow page loading for the first time
LeetCode/Scala - without the firstborn of the string of characters, the longest text string back
气路旋转连接器怎么用









