当前位置:网站首页>【微服务~Nacos】Nacos之配置中心
【微服务~Nacos】Nacos之配置中心
2022-06-30 20:16: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的完整格式如下
${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配置
后面加载的文件,将覆盖前面文件的配置内容
边栏推荐
猜你喜欢

AVL balanced binary tree (I) - concept and C language implementation

Lumiprobe 聚乙二醇化和 PEG 接头丨碘-PEG3-酸研究

Huffman Tree (1) Basic Concept and C - language Implementation

《大厂面试》之JVM篇21问与答

Huffman tree (I) basic concept and C language implementation

Lumiprobe biotin phosphimide (hydroxyproline) instructions

Solve the problems of Devops landing in complex environment with various tools with full stack and full function solutions

Introduction to neural network (Part 1)

Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)

All the important spark summit features were released here last night (with ultra clear video attached)
随机推荐
杰理之触摸按键识别流程【篇】
All the important spark summit features were released here last night (with ultra clear video attached)
The Commission is so high that everyone can participate in the new programmer's partner plan
动态样式绑定--style 和 class
C file pointer
Evolution of screen display technology
哈夫曼樹(一)基本概念與C語言實現
C文件指针
Lambda expression principle analysis and learning (June 23, 2022)
maya房子建模
Web主机iptables防火墙安全脚本
杰理之触摸按键识别流程【篇】
Description of the latest RTSP address rules for Hikvision camera, NVR, streaming media server, playback and streaming [easy to understand]
Openfire在使用MySQL数据库后的中文乱码问题解决
BioVendor sRAGE Elisa试剂盒测试原理和注意事项
Originpro 2021 with installation tutorial
请问海量数据如何去取最大的K个
Testing principle and precautions of biovendor rage ELISA Kit
Black apple server system installation tutorial, black apple installation tutorial, teach you how to install black apple in detail [easy to understand]
Huffman tree (I) basic concept and C language implementation