当前位置:网站首页>[微服务]Nacos
[微服务]Nacos
2022-06-26 23:09:00 【fate _zore】
Nacos
注册服务到nacos
Nacos是SpringCloudAlibaba的组件,而SpringCloudAlibaba也遵循SpringCloud中定义的服务注册、服务发现规范。因此使用Nacos和使用Eureka对于微服务来说,并没有太大区别。
主要差异在于:
- 依赖不同
- 服务地址不同
引入依赖
在cloud-demo父工程的pom文件中的<dependencyManagement>中引入SpringCloudAlibaba的依赖:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
然后在user-service和order-service中的pom文件中引入nacos-discovery依赖:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
注意:不要忘了注释掉eureka的依赖。
配置nacos地址
cloud:
nacos:
server-addr: localhost:8848
注意:不要忘了注释掉eureka的地址
重启

服务分级存储模型
一个服务可以有多个实例,例如我们的user-service,可以有:
- 127.0.0.1:8081
- 127.0.0.1:8082
- 127.0.0.1:8083
假如这些实例分布于全国各地的不同机房,例如:
- 127.0.0.1:8081,在上海机房
- 127.0.0.1:8082,在上海机房
- 127.0.0.1:8083,在杭州机房
Nacos就将同一机房内的实例 划分为一个集群。
也就是说,user-service是服务,一个服务可以包含多个集群,如杭州、上海,每个集群下可以有多个实例,形成分级模型,如图:

微服务互相访问时,应该尽可能访问同集群实例,因为本地访问速度更快。当本集群内不可用时,才访问其它集群。例如:

杭州机房内的order-service应该优先访问同机房的user-service。
集群配置
修改user-service的application.yml文件,添加集群配置:
spring:
cloud:
nacos:
server-addr: localhost:8848
discovery:
cluster-name: HZ # 集群名称
重启两个user-service实例后,我们可以在nacos控制台看到下面结果:

更改负载均衡策略
user-server:
ribbon:
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule # 负载均衡规则
或者
@Bean
public IRule iRule(){
return new NacosRule();
}
权重配置
实际部署中会出现这样的场景:
服务器设备性能有差异,部分实例所在机器性能较好,另一些较差,我们希望性能好的机器承担更多的用户请求。
但默认情况下NacosRule是同集群内随机挑选,不会考虑机器的性能问题。
因此,Nacos提供了权重配置来控制访问频率,权重越大则访问频率越高。
在nacos控制台,找到user-service的实例列表,点击编辑,即可修改权重:



注意:如果权重修改为0,则该实例永远不会被访问
环境隔离
Nacos提供了namespace来实现环境隔离功能。
- nacos中可以有多个namespace
- namespace下可以有group、service等
- 不同namespace之间相互隔离,例如不同namespace的服务互相不可见

创建namespace
默认情况下,所有service、data、group都在同一个namespace,名为public:

我们可以点击页面新增按钮,添加一个namespace:

然后,填写表单:

就能在页面看到一个新的namespace:

服务配置namespace
给微服务配置namespace只能通过修改配置来实现。
例如,修改order-service的application.yml文件:
spring:
cloud:
nacos:
server-addr: localhost:8848
discovery:
cluster-name: HZ
namespace: 492a7d5d-237b-46a1-a99a-fa8e98e4b0f9 # 命名空间,填ID
重启order-service后,访问控制台:


此时访问order-service,因为namespace不同,会导致找不到userservice,控制台会报错
Nacos与Eureka的区别
Nacos的服务实例分为两种l类型:
临时实例:如果实例宕机超过一定时间,会从服务列表剔除,默认的类型。
非临时实例:如果实例宕机,不会从服务列表剔除,也可以叫永久实例。
配置一个服务实例为永久实例:
spring:
cloud:
nacos:
discovery:
ephemeral: false # 设置为非临时实例
Nacos和Eureka整体结构类似,服务注册、服务拉取、心跳等待,但是也存在一些差异:

Nacos与eureka的共同点
- 都支持服务注册和服务拉取
- 都支持服务提供者心跳方式做健康检测
Nacos与Eureka的区别
- Nacos支持服务端主动检测提供者状态:临时实例采用心跳模式,非临时实例采用主动检测模式
- 临时实例心跳不正常会被剔除,非临时实例则不会被剔除
- Nacos支持服务列表变更的消息推送模式,服务列表更新更及时
- Nacos集群默认采用AP方式,当集群中存在非临时实例时,采用CP模式;Eureka采用AP方式
边栏推荐
- Openpyxl module
- Why don't I recommend going to sap training institution for training?
- 【混合编程jni 】第十一篇之JNA详情
- 固有色和环境色
- [hybrid programming JNI] details of JNA in Chapter 11
- A simple and crude method for exporting R language list to local
- Leetcode - the best time to buy or sell stocks
- Weaving dream collection plug-ins are recommended to be free collection plug-ins
- 让敏捷回归本源——读《敏捷整洁之道》有感
- ASP. Net core create MVC project upload file (buffer mode)
猜你喜欢

简单测试轻量级表达式计算器Flee

數據清洗工具flashtext,效率直接提昇了幾十倍數

Extensions de l'éditeur d'unityeditor - fonctions de table

您的连接不是私密连接

Unity: the referenced script (unknown) on this behavior is missing“

软件工程导论——第四章——形式化说明技术

Implement the queue through two stacks

go语言中的私聊功能处理

颜色搭配和相关问题

Microservices and container choreography in go
随机推荐
ASP.Net Core创建MVC项目上传文件(缓冲方式)
[Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!
[cloud native topic -51]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware redis microservice deployment process
WordPress collection plug-ins are recommended to be free collection plug-ins
Different subsequence problems I
Wechat applet is authorized to log in wx getUserProfile
代码之外:写作是倒逼成长的最佳方式
Are there any risks for the top ten securities companies to register and open accounts? Is it safe?
通过两个stack来实现Queue
【测试】最火的测试开发学习路线内容再次大更新,助力通关大厂测开
Redcap is ready to come out. It is indispensable to build a "meta universe"
Unity4.6版本下载
Development and learning route of golang language
中金证券经理的开户链接开户买股票安全吗?有谁知道啊
【LeetCode】1984. Minimum difference between highest and lowest of K scores
Solid and ambient colors
[mixed programming JNI] Part 6: operation of strings and arrays in native
Learun low code OA system construction platform
一篇文章带你学会容器逃逸
The client implements client Go client type definition connection