当前位置:网站首页>[Microservice~Nacos] Configuration Center of Nacos
[Microservice~Nacos] Configuration Center of Nacos
2022-07-30 01:24:00 【Books of Coriander】
??这里是【微服务~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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- How Filebeat ensures that the log file is still correctly read when the log file is split (or rolled)
- Detailed introduction of @RequestParam annotation
- Navicat error: 1045-Access denied for user [email protected](using passwordYES)
- 微信小程序开发之图片压缩方案
- LeetCode / Scala - 无重复字符最长子串 ,最长回文子串
- Navicat报错:1045-Access denied for user [email protected](using passwordYES)
- ufw 设置防火墙规则
- 【微服务~Nacos】Nacos之配置中心
- Missing X64 mfc140u. DLL file - > application cannot normal boot (0 xc000007b) solution
- STM32 - OLED display experiment
猜你喜欢

LeetCode/Scala - without the firstborn of the string of characters, the longest text string back

畅玩西安全攻略

9 common mistakes testers fall into

Fabric Private Data Case

Linux - install MySQL (detailed tutorial)

经济衰退时期的对比:如今更像历史上的哪段时期?

Leetcode70. 爬楼梯

Reconstruction of binary tree

数据流图、数据字典

vscode 工作区配置插件 配置不同工作环境
随机推荐
Replace the executable file glibc version of the one
[Flutter] Flutter preloading of mixed development solves the problem of slow page loading for the first time
遇到bug的解决办法,测试再也不背锅了
自学HarmonyOS应用开发(49)- 引入地图功能
高德地图jsapi不生效 INVALID_USER_SCODE
【微服务~Nacos】Nacos之配置中心
这是一道非常有争议的题,我的分析如下: TCP/IP在多个层引入了安全机制,其中TLS协议位于______。 A.数据链路层 B.网络层 C.传输层 D.应用层
不要急,没有一朵花,从一开始就是花,也不要嚣张,没有一朵花,
如何在AWS里面的SQL server设置混合登陆
[MySQL series] MySQL database foundation
【LeetCode每日一题】——872.叶子相似的树
Navicat如何连接MySQL
servlet执行详解
Unity便携式 VR 的实现
CMake Tutorial Tour (1)_Basic starting point
【LeetCode每日一题】——404.左叶子之和
工厂模式
How Navicat Connects to MySQL
Nacos micro service ~ Nacos 】 【 configuration of the center
泰克Tektronix示波器软件TDS420|TDS430|TDS460上位机软件NS-Scope