当前位置:网站首页>[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
边栏推荐
- go中的微服务和容器编排
- L'outil de nettoyage des données flashtext améliore directement l'efficacité de plusieurs dizaines de fois
- Unity cloth system_ Cloth component (including dynamic call related)
- 【710. 黑名单中的随机数】
- Product design in the extreme Internet Era
- 6.24 学习内容
- Electronic Society C language level 1 30, calculation of last term of arithmetic sequence
- Different subsequence problems I
- Open world mecha games phantom Galaxy
- Introduction de l'opérateur
猜你喜欢

typora设置标题自动编号

golang语言的开发学习路线

数据清洗工具flashtext,效率直接提升了几十倍数

【界面】pyqt5和Swin Transformer对人脸进行识别

CVE-2022-30190 Follina Office RCE分析【附自定义word模板POC】

Unityeditor Editor Extension - table function

用户在hander()goroutine,添加定时器功能,超时则强踢出

【LeetCode】1984. Minimum difference between highest and lowest of K scores

微信小程序自动生成打卡海报

【测试】最火的测试开发学习路线内容再次大更新,助力通关大厂测开
随机推荐
【Try to Hack】正向shell和反向shell
Is it safe to open an account and buy stocks? Who knows
From bitmap to bloom filter, C # implementation
Share three methods of automatic summation in Excel
炒股手机上开户可靠吗? 网上开户炒股安全吗
Unity4.6版本下载
go语言中的私聊功能处理
Design of master-slave replication system
【混合编程jni 】第七篇之JNI 的命令行们
Leetcode - the best time to buy or sell stocks
开放世界机甲游戏-Phantom Galaxies
Bs-gx-016 implementation of textbook management system based on SSM
leetcode - 买卖股票的最佳时机
ubuntu上安装mysql
浅谈分布式系统开发技术中的CAP定理
微信小程序自动生成打卡海报
电子协会 C语言 1级 29 、 对齐输出
Operator介绍
go中的微服务和容器编排
Extensions de l'éditeur d'unityeditor - fonctions de table