当前位置:网站首页>【微服务】Nacos集群搭建以及加载文件配置
【微服务】Nacos集群搭建以及加载文件配置
2022-07-31 05:56:00 【m0_67394006】
??这里是【微服务】,关注我学习微服务不迷路
??如果对你有帮助,给博主一个免费的点赞以示鼓励
欢迎各位??点赞??评论收藏
??专栏介绍
【微服务】 目前主要更新微服务,一起学习一起进步。
??本期介绍
本期主要介绍Nacos集群搭建以及加载文件配置
文章目录
目录
服务集群
需求
服务提供者搭建集群
服务调用者,依次显示集群中各服务的信息


搭建
1)修改服务提供方的controller,打印服务端端口号

package com.czxy.controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@RestController
public class EchoController {
@Resource
private HttpServletRequest request;
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
int serverPort = request.getServerPort();
return "Hello Nacos Discovery " + string + ":" + serverPort;
}
}
2)编写yml配置

#端口号
server:
port: 8170
spring:
application:
name: service-provider #服务名
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务地址
#端口号
server:
port: 8270
spring:
application:
name: service-provider #服务名
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务地址
3)配置idea启动项
-Dspring.profiles.active=8170

测试
- 启动3个服务(2个服务提供,1个服务消费)

- 查看nacos控制台

加载配置文件顺序
对
3.4.5章节/第4步内容进行详解加载配置文件的顺序(第4步详解)
nacos配置DataId介绍
- nacos 提供了3种方式,配置dataId的加载顺序
A: 共享配置:
spring.cloud.nacos.config.shared-dataids
spring.cloud.nacos.config.refreshable-dataidsB: 加载多配置:
spring.cloud.nacos.config.ext-config[n]C: 内部规则拼接:
spring.cloud.nacos.config.prefix
spring.cloud.nacos.config.file-extension
spring.cloud.nacos.config.group
配置yml文件中的DataId
spring:
application:
name: config-service # 服务名
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848 # nacos 服务地址
shared-dataids: test1.yml # 1.1 共享配置
refreshable-dataids: test1.yml # 1.2 共享配置,动态刷新
ext-config: # 2. 配置多个
- data-id: test2-1.yml
group: DEFAULT_GROUP
refresh: true
- data-id: test2-2.yml
group: DEFAULT_GROUP
refresh: true
prefix: test3 # 3.1 前缀,默认 ${spring.application.name}
file-extension: yaml # 3.2 后缀
group: DEFAULT_GROUP # 3.3 组名
配置console中的DataId
- nacos控制台配置

测试
后面加载的dataId将覆盖前面加载的dataId设置的内容
查看日志

Located property source: [
BootstrapPropertySource {name='bootstrapProperties-test3-demo.yaml'}, BootstrapPropertySource {name='bootstrapProperties-test3.yaml'}, BootstrapPropertySource {name='bootstrapProperties-test2-2.yml'}, BootstrapPropertySource {name='bootstrapProperties-test2-1.yml'}, BootstrapPropertySource {name='bootstrapProperties-test1.yml'}]
多环境配置
- 在Nacos为不同的环境(开发、测试、生产等)中,提供了多个不同管理级别的概念,包括:
Data ID、Group、Namespace。
概念
描述
Data ID
数据唯一标识,可理解为Spring Cloud应用的配置文件名
Group
用来对Data ID做集合管理,相当于小分类
Namespace
用于进行租户粒度的配置隔离。相当于大分类
- 组group配置
spring.cloud.nacos.config.group= #组名称
- 命名空间 namespace配置
spring.cloud.nacos.config.namespace= #namespace的ID
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢

Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model

mysql索引失效的常见9种原因详解

Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software

【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习

浅析伪类和伪元素

深度解析 z-index

【编程题】【Scratch三级】2022.03 冬天下雪了

小实战项目之——吃货联盟订餐系统

Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition

Obtaining server and client information
随机推荐
In-depth analysis of z-index
What is float?What is document flow?Several ways and principles of clearing floats?What is BFC, how to trigger BFC, the role of BFC
多进程全局变量失效、变量共享问题
一文读懂 MongoDB 和 MySQL 的差异
Analysis of the principle and implementation of waterfall flow layout
Database Principles Homework 2 — JMU
二叉树的还原(反序列化)
Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
MySQL笔记下
文件 - 02 上传文件:上传临时文件到服务器
浅层了解欧拉函数
Gradle剔除依赖演示
03-SDRAM:写操作(突发)
新瓶陈酒 --- 矩阵快速幂
英语翻译软件-批量自动免费翻译软件支持三方接口翻译
项目练习——备忘录(增删改查)
Redux状态管理
零样本学习&Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
【Go语言入门教程】Go语言简介
文件 - 04 下载文件: 根据文件下载链接下载文件