当前位置:网站首页>[Microservice~Nacos] Configuration Center of Nacos
[Microservice~Nacos] Configuration Center of Nacos
2022-08-09 23:44:00 【m0_67393828】
??这里是【微服务~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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- SecureCRT 设置超时自动断开连接时长
- 聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
- 1215 – Cannot add foreign key constraint
- Quotefancy ,提供鼓舞人心语录的壁纸网站 - 倾城之链
- 腾讯继续挥舞降本增效“大刀”,外包员工免费餐饮福利被砍了
- Tensorflow中placeholder函数的用法
- Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN
- 【云原生】4.2 DevOps 精讲篇
- 6个规则去净化你的代码
- 【微服务~Nacos】Nacos之配置中心
猜你喜欢

AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis

AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero

数独 | 回溯-7

POWER SOURCE ETA埃塔电源维修FHG24SX-U概述

JS解混淆-AST还原案例

“稚晖君”为2022昇腾AI创新大赛打call&nbsp;期待广大开发者加入

从产品角度看 L2 应用:为什么说这是一个游乐场?

APP automation test framework - UiAutomator2 introductory

一文让你快速了解隐式类型转换【整型提升】!

如何让您的公司内容满足 GDPR 合规性
随机推荐
JSON 基本使用
发送激活邮件「建议收藏」
laravel table migration error [easy to understand]
SecureCRT background color
Multiple reasons for MySQL slow query
hdu 1503 Advanced Fruits(最长公共子序列的应用)
深度剖析 Apache EventMesh 云原生分布式事件驱动架构
上海控安SmartRocket系列产品推介(三):SmartRocket iVerifier计算机联锁系统验证工具
Use zeros(), ones(), fill() methods to generate data in TF
STC8H Development (15): GPIO Drives Ci24R1 Wireless Module
json事例
2.1.5 大纲显示问题
【EF】数据表全部字段更新与部分字段更新
SQLi-LABS Page-2 (Adv Injections)
角度和弧度的相互换算
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
How to Make Your Company Content GDPR Compliant
JS解混淆-AST还原案例
ACM MM 2022 | Cloud2Sketch: Painting with clouds in the sky, AI brush strokes
abstract class or interface