当前位置:网站首页>[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
边栏推荐
- 邮箱附件钓鱼常用技法
- Unityeditor Editor Extension - table function
- [微服务]Nacos
- leetcode - 买卖股票的最佳时机
- npm 命令提示Error: EACCES: permission denied
- Extensions de l'éditeur d'unityeditor - fonctions de table
- Bs-gx-016 implementation of textbook management system based on SSM
- Introduction de l'opérateur
- 买股票在手机上开户安全吗 网上开户炒股安全吗
- Is it safe to open an account on your mobile phone to buy stocks? Is it safe to open an account online to speculate in stocks
猜你喜欢

PHP代码审计系列(一) 基础:方法、思路、流程

通过两个stack来实现Queue

The user adds a timer function in the handler () goroutine. If it times out, it will be kicked out

WP collection plug-in tutorial no thanks for WordPress collection of rules

UnityEditor編輯器擴展-錶格功能

Solid and ambient colors

Development and learning route of golang language

Bs-gx-016 implementation of textbook management system based on SSM

Unity初学者肯定能用得上的50个小技巧
![[Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!](/img/78/ece6da9a26c9d93988dac30ce93f99.png)
[Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!
随机推荐
树莓派初步使用
低佣金免费开户渠道安全吗?
leetcode 1143. Longest Commom Subsequence 最长公共子序列(中等)
[strong foundation program] video of calculus in mathematics and Physics Competition
Three solutions for improving embedded software development environment
运筹说 第66期|贝尔曼也有“演讲恐惧症”?
Electronic Society C language level 1 29, alignment output
Unity cloth system_ Cloth component (including dynamic call related)
颜色搭配和相关问题
Nacos安装指南
[mixed programming JNI] Part 6: operation of strings and arrays in native
Reading graph augmentations to learn graph representations (lg2ar)
简单测试轻量级表达式计算器Flee
The user adds a timer function in the handler () goroutine. If it times out, it will be kicked out
【710. 黑名单中的随机数】
golang语言的开发学习路线
Restfultoolkitx of idea utility plug-in -- restful interface debugging
【混合编程jni 】第六篇之native 中字符串和数组的操作
How to write test cases and a brief introduction to go unit test tool testify
不会写免杀也能轻松过defender上线CS