当前位置:网站首页>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
边栏推荐
- Excel intercept text
- ESP32 LVGL8. 1 - label (style 14)
- Daily network security certification test questions (April 12, 2022)
- CISSP certified daily knowledge points (April 14, 2022)
- Ctfshow - web362 (ssti)
- ctfshow-web362(SSTI)
- Analysez l'objet promise avec le noyau dur (Connaissez - vous les sept API communes obligatoires et les sept questions clés?)
- listener.log
- Nacos作为服务注册中心
- Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
猜你喜欢
With the use of qchart, the final UI interface can be realized. The control of qweight can be added and promoted to a user-defined class. Only the class needs to be promoted to realize the coordinate
Analysez l'objet promise avec le noyau dur (Connaissez - vous les sept API communes obligatoires et les sept questions clés?)
ctfshow-web361(SSTI)
listener. log
ESP32 LVGL8. 1 - arc (arc 19)
Quantexa CDI(场景决策智能)Syneo平台介绍
STM32 learning record 0008 - GPIO things 1
【数学建模】—— 层次分析法(AHP)
视频边框背景如何虚化,简单操作几步实现
ctfshow-web362(SSTI)
随机推荐
Stm32mp157 wm8960 audio driver debugging notes
Quantexa CDI(场景决策智能)Syneo平台介绍
【科普】CRC校验(一)什么是CRC校验?
If condition judgment in shell language
Daily network security certification test questions (April 14, 2022)
Promote QT default control to custom control
QT tablewidget insert qcombobox drop-down box
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
Analysez l'objet promise avec le noyau dur (Connaissez - vous les sept API communes obligatoires et les sept questions clés?)
Multifunctional toolbox wechat applet source code
Daily CISSP certification common mistakes (April 14, 2022)
Nacos作为服务配置中心实战
迁移学习进阶
Daily CISSP certification common mistakes (April 15, 2022)
Function recursion and solving interesting problems
机器学习理论基础篇--关于机器学习的一些术语
使用晨曦记账本,分析某个时间段每个账户收支结余
WIN1 remote "this may be due to credssp encryption Oracle correction" solution
Ionic 从创建到打包指令集顺序
Gson fastjason Jackson of object to JSON difference modifies the field name