当前位置:网站首页>Nacos as service registry
Nacos as service registry
2022-04-23 18:39:00 【Hua Weiyun】
@toc
1、Nacos brief introduction
1.1 Nacos What is it? ?
A dynamic service discovery that is easier to build cloud native applications 、 Configuration management and service management platform .
Nacos Service registry + A combination of configuration centers , Equivalent to
1.2 Nacos What can be done ?
- replace Eureka Be a service registry
- replace Config Be a service configuration center
Project address :https://github.com/alibaba/Nacos
Official document :https://nacos.io/zh-cn/index.html
1.3 Various registries are more
It is said that Nacos There are more than 10 The instance of Wan runs , It has passed the test of all kinds of large traffic such as double 11
2、 Install and run Nacos
Prerequisite : Local Java8+Maven The environment has been configured successfully .
Download it from the official website first Nacos:https://github.com/alibaba/nacos/releases
Unzip the installation package , Direct operation bin In the catalog startup.cmd
After the command runs successfully, you can directly access http://localhost:8848/nacos, The default account passwords are nacos
3、Nacos Demonstrate as a service registry
Official document :https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_nacos_discovery
3.1 be based on Nacos Service provider
3.1.1 newly build cloudalibaba-provider-payment9001
3.1.2 pom
Father pom
Of this module pom:
<dependencies> <!--SpringCloud ailibaba nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- SpringBoot Integrate Web Components --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Everyday use jar Package configuration --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
3.1.3 yml
server: port: 9001spring: application: name: nacos-payment-provider cloud: nacos: discovery: server-addr: localhost:8848 # To configure Nacos Address management: endpoints: web: exposure: include: '*'
3.1.4 Main startup class
@EnableDiscoveryClient@SpringBootApplicationpublic class PaymentMain9001{ public static void main(String[] args) { SpringApplication.run(PaymentMain9001.class, args); }}
3.1.5 Business class
@RestControllerpublic class PaymentController{ @Value("${server.port}") private String serverPort; @GetMapping(value = "/payment/nacos/{id}") public String getPayment(@PathVariable("id") Integer id) { return "nacos registry, serverPort: "+ serverPort+"\t id"+id; }}
3.1.6 test
After starting this module, check Nacos Console
visit :http://localhost:9001/payment/nacos/1
There is no problem with the test .
3.1.7 To set up cloudalibabaprovider-payment9002 modular
In addition to the port number , Others are almost the same as cloudalibabaprovider-payment9001 Modules are consistent , The construction process is not repeated .
3.2 be based on Nacos Service consumers
3.2.1 newly build Module
newly build cloudalibaba-consumer-nacos-order83 modular .
3.2.2 pom
<dependencies> <!--SpringCloud ailibaba nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- Introduce your own definition of api General package , have access to Payment payment Entity --> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <!-- SpringBoot Integrate Web Components --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Everyday use jar Package configuration --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
nacos Load balancing is supported by default , Because when we look at its dependent files, we can find , It introduces... By default Ribbon Load balancing component .
3.2.3 yml
server: port: 83spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: localhost:8848# The name of the micro service the consumer is going to visit ( Successfully registered nacos Micro service providers of )service-url: nacos-user-service: http://nacos-payment-provider
3.2.4 Main startup class
@EnableDiscoveryClient@SpringBootApplicationpublic class OrderNacosMain83{ public static void main(String[] args) { SpringApplication.run(OrderNacosMain83.class,args); }}
3.2.5 Business class
OrderNacosController
@RestControllerpublic class PaymentController{ @Value("${server.port}") private String serverPort; @GetMapping(value = "/payment/{id}") public String getPayment(@PathVariable("id") Integer id) { return "nacos registry, serverPort: "+ serverPort+"\t id"+id; }}
ApplicationContextBean Configuration class
@Configurationpublic class ApplicationContextBean{ @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); }}
3.2.6 test
After starting the service, view Nacos Console
visit :http://localhost:83/consumer/payment/nacos/13
for the first time :
The second time :
Refresh a few more times to find , The service port number has been 9001 and 9002 Switch between , Because our default load balancing algorithm is the polling algorithm .
3.3 Service registry comparison
3.3.1 Nacos Ecological map
You can see , Alibaba's ambition is still great .
3.3.2 Nacos and CAP
3.3.3 Nacos Support AP and CP Mode switching
==C All nodes see the same data at the same time ; and A The definition is that all requests will receive a response .==
When to choose which mode to use ?
Generally speaking , If the information is stored at the service level and is not required nacos-client register , And can keep the heartbeat , Then you can choose AP Pattern . Current mainstream services such as Spring cloud and Dubbo service , All apply to AP Pattern ,AP Patterns weaken consistency for the possibility of services , therefore AP Only temporary instance registration is supported in mode .
If you need to edit or store configuration information at the service level , that CP It is necessary ,K8S Service and DNS Services apply to CP Pattern .
CP Mode, it supports the registration of persistent instances , In this case Raft The protocol is cluster operation mode , In this mode, the service must be registered before the instance is registered , If the service doesn't exist , It will return an error .
curl -X PUT '$NACOS_SERVER:8848/nacos/v1/ns/operator/switches?entry=serverMode&value=CP'
Here we are ,Nacos As a service registration center, I'm finished , As a configuration center, please check out the next article .
版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231836021792.html
边栏推荐
- 深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
- Using transmittablethreadlocal to realize parameter cross thread transmission
- 教你用简单几个步骤快速重命名文件夹名
- Interpretation and compilation of JVM
- Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
- 14个py小游戏源代码分享第二弹
- ESP32 LVGL8. 1 - label (style 14)
- ESP32 LVGL8. 1 - anim animation (anim 16)
- Quantexa CDI(场景决策智能)Syneo平台介绍
- Ionic instruction set order from creation to packaging
猜你喜欢
iptables初探
Query the logistics update quantity according to the express order number
ctfshow-web361(SSTI)
ESP32 LVGL8. 1 - label (style 14)
STM32学习记录0008——GPIO那些事1
[mathematical modeling] - analytic hierarchy process (AHP)
昇腾 AI 开发者创享日全国巡回首站在西安成功举行
Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
How to restore MySQL database after win10 system is reinstalled (mysql-8.0.26-winx64. Zip)
机器学习实战 -朴素贝叶斯
随机推荐
解决:cnpm : 無法加載文件 ...\cnpm.ps1,因為在此系統上禁止運行脚本
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
In shell programming, the shell file with relative path is referenced
Matlab tips (6) comparison of seven filtering methods
数据库上机实验四(数据完整性与存储过程)
K210串口通信
Iptables - L executes slowly
Custom prompt box MessageBox in QT
ctfshow-web362(SSTI)
Ucosiii transplantation and use, reference punctual atom
14 py games source code share the second bullet
iptables初探
MVVM模型
机器学习理论基础篇--关于机器学习的一些术语
CISSP certified daily knowledge points (April 12, 2022)
串口调试工具cutecom和minicom
22年字节跳动飞书人力套件三面面经
Daily network security certification test questions (April 13, 2022)
Machine learning practice - naive Bayes
Use of regular expressions in QT