当前位置:网站首页>[micro service]nacos
[micro service]nacos
2022-06-26 23:25:00 【fate _ zore】
Nacos
Sign up for service to nacos
Nacos yes SpringCloudAlibaba The components of , and SpringCloudAlibaba Also follow SpringCloud Service registration defined in 、 Service discovery specification . Therefore use Nacos And use Eureka For microservices , There's no big difference .
The main difference is :
- Depending on the difference
- Different service addresses
Introduce dependencies
stay cloud-demo Parent project pom In the document <dependencyManagement> Introduction in SpringCloudAlibaba Dependence :
<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>
And then in user-service and order-service Medium pom Introduce in the file nacos-discovery rely on :
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
Be careful : Don't forget to comment it out eureka Dependence .
To configure nacos Address
cloud:
nacos:
server-addr: localhost:8848
Be careful : Don't forget to comment it out eureka The address of
restart

Service hierarchical storage model
One service There can be multiple example , For example, our user-service, There can be :
- 127.0.0.1:8081
- 127.0.0.1:8082
- 127.0.0.1:8083
If these examples are distributed in different computer rooms all over the country , for example :
- 127.0.0.1:8081, In Shanghai
- 127.0.0.1:8082, In Shanghai
- 127.0.0.1:8083, In Hangzhou computer room
Nacos The instance in the same machine room Divide into one colony .
in other words ,user-service It's service. , A service can contain multiple clusters , Such as Hangzhou 、 Shanghai , Each cluster can have multiple instances , Form a hierarchical model , Pictured :

When microservices access each other , You should access the same cluster instance as much as possible , Because local access is faster . When not available in this cluster , To access other clusters . for example :

In Hangzhou computer room order-service Priority should be given to accessing the same computer room user-service.
Cluster configuration
modify user-service Of application.yml file , Add cluster configuration :
spring:
cloud:
nacos:
server-addr: localhost:8848
discovery:
cluster-name: HZ # Cluster name
Restart both user-service After the instance , We can do it in nacos The console sees the following results :

Change load balancing policy
user-server:
ribbon:
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule # Load balancing rules
perhaps
@Bean
public IRule iRule(){
return new NacosRule();
}
Weight configuration
This scenario will appear in the actual deployment :
There are differences in the performance of server devices , The machine where some examples are located has good performance , Others are poor , We want better machines to take on more user requests .
But by default NacosRule It is randomly selected in the same cluster , The performance of the machine will not be considered .
therefore ,Nacos Weight configuration is provided to control access frequency , The greater the weight, the higher the access frequency .
stay nacos Console , find user-service List of instances of , Click edit , You can modify the weight :



Be careful : If the weight is changed to 0, Then the instance will never be accessed
Environmental isolation
Nacos Provides namespace To achieve environmental isolation .
- nacos There can be more than one namespace
- namespace There can be group、service etc.
- Different namespace Separate from each other , For example, different namespace Our services are not visible to each other

establish namespace
By default , all service、data、group All in the same namespace, be known as public:

We can click the page add button , Add one namespace:

then , Fill out the form :

You can see a new one on the page namespace:

Service configuration namespace
Configure the micro service namespace This can only be achieved by modifying the configuration .
for example , modify order-service Of application.yml file :
spring:
cloud:
nacos:
server-addr: localhost:8848
discovery:
cluster-name: HZ
namespace: 492a7d5d-237b-46a1-a99a-fa8e98e4b0f9 # Namespace , fill ID
restart order-service after , Access the console :


Visit at this time order-service, because namespace Different , Will lead to the loss of userservice, The console will report an error
Nacos And Eureka The difference between
Nacos There are two types of service instances l type :
Temporary instance : If the instance goes down for more than a certain time , Will be removed from the service list , The default type .
Non temporary instance : If the instance goes down , Will not be removed from the service list , It can also be called permanent instance .
Configure a service instance as a permanent instance :
spring:
cloud:
nacos:
discovery:
ephemeral: false # Set to non temporary instance
Nacos and Eureka The overall structure is similar , Service registration 、 Service pull 、 Heartbeat waiting , But there are some differences :

Nacos And eureka Common ground
- Both support service registration and service pull
- All support service providers' heartbeat for health detection
Nacos And Eureka The difference between
- Nacos Support the server to actively detect the provider status : The temporary instance adopts heartbeat mode , Active detection mode is adopted for non temporary instances
- Temporary instances with abnormal heartbeat will be rejected , Non temporary instances will not be rejected
- Nacos Support the message push mode of service list change , The service list is updated more timely
- Nacos The cluster adopts... By default AP The way , When there are non temporary instances in the cluster , use CP Pattern ;Eureka use AP The way
边栏推荐
- Color matching and related issues
- 树莓派初步使用
- 【老卫搞机】090期:键盘?主机?全功能键盘主机!
- DAST 黑盒漏洞扫描器 第五篇:漏洞扫描引擎与服务能力
- 中金证券经理的开户链接开户买股票安全吗?有谁知道啊
- Development and learning route of golang language
- Operations research says that in issue 66, Behrman also has "speech phobia"?
- Let agile return to its original source -- Some Thoughts on reading the way of agile neatness
- What are the test case design methods?
- 6.24 learning content
猜你喜欢

Learun low code OA system construction platform

从位图到布隆过滤器,C#实现

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

FPGA -VGA显示

FPGA -vga display

Let agile return to its original source -- Some Thoughts on reading the way of agile neatness

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

客户端实现client.go客户端类型定义连接

Unityeditor Editor Extension - table function

代码之外:写作是倒逼成长的最佳方式
随机推荐
入侵痕迹清理
微信小程序自动生成打卡海报
ASP.Net Core创建MVC项目上传文件(缓冲方式)
Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
Unity animation knowledge of Art
【强基计划】数学与物理竞赛中的微积分部分视频
Operations research says that in issue 66, Behrman also has "speech phobia"?
股票开户有哪些优惠活动?手机开户安全么?
Implement the queue through two stacks
分享三种在Excel表格中自动求和的方法
[strong foundation program] video of calculus in mathematics and Physics Competition
[Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!
Smartbi gives you a piece to play with Boston matrix
Cvpr2022 stereo matching of asymmetric resolution images
Operator介绍
Nacos安装指南
Leetcode 718. 最长重复子数组(暴力枚举,待解决)
Tensorrt笔记(七)Tensorrt使用问题整理
[hybrid programming JNI] details of JNA in Chapter 11
[mixed programming JNI] Part 6: operation of strings and arrays in native