当前位置:网站首页>[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
边栏推荐
- 【老卫搞机】090期:键盘?主机?全功能键盘主机!
- Unity animation knowledge of Art
- typora设置标题自动编号
- 为什么我不推荐去SAP培训机构参加培训?
- 运筹说 第66期|贝尔曼也有“演讲恐惧症”?
- [cloud native topic -51]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware redis microservice deployment process
- 【混合编程jni 】第六篇之native 中字符串和数组的操作
- 想买股票请问在券商公司的哪里开户佣金低更安全
- 低佣金免费开户渠道安全吗?
- [mixed programming JNI] Part 9: JNI summary
猜你喜欢

Reading graph augmentations to learn graph representations (lg2ar)

Learun low code OA system construction platform

【测试】最火的测试开发学习路线内容再次大更新,助力通关大厂测开

FPGA -vga display

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

运筹说 第66期|贝尔曼也有“演讲恐惧症”?

Introduction to software engineering -- Chapter 4 -- formal description technology

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

CVPR2022-不对称分辨率图像的立体匹配
![[interface] pyqt5 and swing transformer for face recognition](/img/37/b259627a8ffd82afe8e8f3029bf290.png)
[interface] pyqt5 and swing transformer for face recognition
随机推荐
12 color ring three primary colors
软件工程导论——第四章——形式化说明技术
[Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!
简单测试轻量级表达式计算器Flee
Reading graph augmentations to learn graph representations (lg2ar)
UnityEditor编辑器扩展-表格功能
数据清洗工具flashtext,效率直接提升了几十倍数
【测试】最火的测试开发学习路线内容再次大更新,助力通关大厂测开
Electronic Society C language level 1 31. Calculate line segment length
在手机开户买股票安全吗 网上开户炒股安全吗
树莓派初步使用
The client implements client Go client type definition connection
ASP. Net core create MVC project upload file (buffer mode)
电子协会 C语言 1级 31 、 计算线段长度
Is it safe to open an account and speculate in stocks on the mobile phone? Is it safe to open an account and speculate in stocks on the Internet
Learun low code OA system construction platform
Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
Raspberry pie preliminary use
客户端实现client.go客户端类型定义连接
Unityeditor Editor Extension - table function