当前位置:网站首页>Nacos作为服务注册中心
Nacos作为服务注册中心
2022-04-23 18:36:00 【华为云】
@toc
1、Nacos简介
1.1 Nacos是什么?
一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
Nacos就是服务注册中心+配置中心的组合,等价于
1.2 Nacos能做什么?
- 替代Eureka做服务注册中心
- 替代Config做服务配置中心
项目地址:https://github.com/alibaba/Nacos
官网文档:https://nacos.io/zh-cn/index.html
1.3 各种注册中心比较
据说 Nacos 在阿里巴巴内部有超过 10 万的实例运行,已经过了类似双十一等各种大型流量的考验
2、安装并运行Nacos
前提条件:本地Java8+Maven环境已经配置成功。
先从官网下载Nacos:https://github.com/alibaba/nacos/releases
解压安装包,直接运行bin目录下的startup.cmd
命令运行成功后直接访问http://localhost:8848/nacos,默认账号密码都是nacos
3、Nacos作为服务注册中心演示
3.1 基于Nacos的服务提供者
3.1.1 新建cloudalibaba-provider-payment9001
3.1.2 pom
父pom
本模块的pom:
<dependencies> <!--SpringCloud ailibaba nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- SpringBoot整合Web组件 --> <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> <!--日常通用jar包配置--> <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 #配置Nacos地址management: endpoints: web: exposure: include: '*'
3.1.4 主启动类
@EnableDiscoveryClient@SpringBootApplicationpublic class PaymentMain9001{ public static void main(String[] args) { SpringApplication.run(PaymentMain9001.class, args); }}
3.1.5 业务类
@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 测试
启动本模块之后查看Nacos控制台
访问:http://localhost:9001/payment/nacos/1
测试是没有问题的。
3.1.7 再搭建cloudalibabaprovider-payment9002模块
除了端口号外,其他几乎与cloudalibabaprovider-payment9001模块一致,搭建过程不在赘述。
3.2 基于Nacos的服务消费者
3.2.1 新建Module
新建cloudalibaba-consumer-nacos-order83模块。
3.2.2 pom
<dependencies> <!--SpringCloud ailibaba nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- 引入自己定义的api通用包,可以使用Payment支付Entity --> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <!-- SpringBoot整合Web组件 --> <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> <!--日常通用jar包配置--> <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默认是支持负载均衡的,因为我们查看它的依赖文件可以发现,它默认引入了Ribbon负载均衡组件。
3.2.3 yml
server: port: 83spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: localhost:8848#消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)service-url: nacos-user-service: http://nacos-payment-provider
3.2.4 主启动类
@EnableDiscoveryClient@SpringBootApplicationpublic class OrderNacosMain83{ public static void main(String[] args) { SpringApplication.run(OrderNacosMain83.class,args); }}
3.2.5 业务类
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配置类
@Configurationpublic class ApplicationContextBean{ @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); }}
3.2.6 测试
启动该服务之后查看Nacos控制台
访问:http://localhost:83/consumer/payment/nacos/13
第一次:
第二次:
多刷新几次就可以发现,提供服务的端口号一直在9001和9002之间切换,因为我们默认负载均衡算法用的是轮询算法。
3.3 服务注册中心对比
3.3.1 Nacos生态图
可以看到,阿里巴巴的野心还是很大的。
3.3.2 Nacos和CAP
3.3.3 Nacos支持AP和CP模式的切换
==C是所有节点在同一时间看到的数据是一致的;而A的定义是所有的请求都会收到响应。==
何时选择使用何种模式?
一般来说,如果不需要存储服务级别的信息且服务实例是通过nacos-client注册,并能够保持心跳上报,那么就可以选择AP模式。当前主流的服务如 Spring cloud 和 Dubbo 服务,都适用于AP模式,AP模式为了服务的可能性而减弱了一致性,因此AP模式下只支持注册临时实例。
如果需要在服务级别编辑或者存储配置信息,那么 CP 是必须,K8S服务和DNS服务则适用于CP模式。
CP模式下则支持注册持久化实例,此时则是以 Raft 协议为集群运行模式,该模式下注册实例之前必须先注册服务,如果服务不存在,则会返回错误。
curl -X PUT '$NACOS_SERVER:8848/nacos/v1/ns/operator/switches?entry=serverMode&value=CP'
到此,Nacos作为服务注册中心就介绍完了,作为配置中心请查看下一篇文章。
版权声明
本文为[华为云]所创,转载请带上原文链接,感谢
https://bbs.huaweicloud.com/blogs/349225
边栏推荐
- C language simulates entering and leaving the stack, first in first out, first in first out, shared memory
- 使用晨曦记账本,分析某个时间段每个账户收支结余
- 昇腾 AI 开发者创享日全国巡回首站在西安成功举行
- MATLAB小技巧(6)七种滤波方法比较
- Const keyword, variable and function are decorated with const
- Mysql database backup command -- mysqldump
- Software test summary
- Cygwin64 right click to add menu, and open cygwin64 here
- After CANopen starts PDO timing transmission, the heartbeat frame time is wrong, PDO is delayed, and CANopen time axis is disordered
- QT add external font ttf
猜你喜欢
Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
iptables -L执行缓慢
Excel intercept text
Spark performance optimization guide
listener.log
ctfshow-web361(SSTI)
Ctfshow - web362 (ssti)
ESP32 LVGL8. 1 - label (style 14)
MATLAB小技巧(6)七种滤波方法比较
[mathematical modeling] - analytic hierarchy process (AHP)
随机推荐
Error reported when running tensorboard: valueerror: duplicate plugins for name projector, solution
CISSP certified daily knowledge points (April 12, 2022)
Daily network security certification test questions (April 13, 2022)
K210 serial communication
机器学习理论之(7):核函数 Kernels —— 一种帮助 SVM 实现非线性化决策边界的方式
Connection mode of QT signal and slot connect() and the return value of emit
Resolves the interface method that allows annotation requests to be written in postman
According to the result set queried by SQL statement, it is encapsulated as JSON
CISSP certified daily knowledge points (April 14, 2022)
STM32 learning record 0008 - GPIO things 1
硬核解析Promise對象(這七個必會的常用API和七個關鍵問題你都了解嗎?)
Query the logistics update quantity according to the express order number
logstash 7. There is a time problem in X. the difference between @ timestamp and local time is 8 hours
ctfshow-web362(SSTI)
QT excel operation summary
软件测试总结
CANopen STM32 transplantation
Feign requests the log to be printed uniformly
STM32: LCD display
Use of regular expressions in QT