当前位置:网站首页>[Cloud native] Simple introduction and use of microservice Nacos
[Cloud native] Simple introduction and use of microservice Nacos
2022-07-31 05:53:00 【m0_67392811】
前言:
最近看完了Nacos相关的课程,对相关知识总结如下,希望能够加深最近的印象以及帮助到各位大佬???
如果文章有需要改进的地方还请大佬斧正???
小威先感谢各位小伙伴儿了???
文章目录
??Nacos简介
国内公司一般都推崇阿里巴巴的技术,比如注册中心,SpringCloudAlibaba也推出了一个名为Nacos的注册中心.
Nacos是构建以“服务”Modern application architecture in (例如微服务范式、云原生范式)的服务基础设施,致力于微服务的发现、管理和信息配置,能帮助开发者快速实现动态服务发现、服务配置、服务元数据及流量管理,from more agile、更容易的构建、交付和管理微服务平台.Nacos几乎支持所有主流类型的服务的发现、配置和管理.Nacos服务领域模型主要分为命名空间、集群、服务.
??Nacos实现原理
和我们之前介绍的Eureka很相似.
Nacos注册中心分为server与client,server采Java编写,为client提供注册发现服务与配置服务.而client可以用多种语言实现,clientNested with microservices,nacos提供sdk和openApi,如果没有sdk也可以根据openApi手动写服务注册与发现和配置拉取的逻辑.
服务注册的策略的是每5秒向nacos serverSend a heartbeat,心跳带上了服务名,服务ip,Server and other information.同时 nacos server也会向client 主动发起健康检查,支持tcp/http检查.
Nacos 而且支持基于 DNS 和基于 RPC 的服务发现.
原SDK、OpenAPI、or aloneAgent TODO注册 Service 后,服务消费者可以使DNS TODO或HTTP&API查找和发现服务.
如果15秒内无心跳且健康检查失败则认为实例不健康,如果30秒内健康检查失败则剔除实例.
Nacos 提供对服务的实时的健康检查,Block requests to unhealthy hosts or service instances.Nacos 支持传输层 (PING 或 TCP)和应用层.

??Nacos的使用
1.引依赖:在cloud-demo父工程的pom文件中的<dependencyManagement>中引入SpringCloudAlibaba的依赖
<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>
在user-service和order-service中的pom文件中引入nacos-discovery依赖:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
application代码
@SpringBootApplication
@EnableFeignClients(clients = UserClient.class,defaultConfiguration = DefaultFeignConfiguration.class)
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
/**
* 创建RestTemplate并注入Spring容器
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
在user-service和order-service的application.yml中添加nacos地址:
spring:
cloud:
nacos:
server-addr: localhost:8848
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/cloud_order?useSSL=false
username: root
password: 123
driver-class-name: com.mysql.jdbc.Driver
application:
name: orderservice
cloud:
nacos:
server-addr: 127.0.0.1:8848
mybatis:
type-aliases-package: cn.itcast.user.pojo
configuration:
map-underscore-to-camel-case: true
logging:
level:
cn.itcast: debug
pattern:
dateformat: MM-dd HH:mm:ss:SSS
导入结果
??Nacos分级存储
一个服务可以有多个实例,例如我们的user-service,可以有81,82,83端口
假如这些实例分布于全国各地的不同机房,例如:
- 127.0.0.1:8081,在上海机房
- 127.0.0.1:8082,在上海机房
- 127.0.0.1:8083,在杭州机房
Nacos就将同一机房内的实例 划分为一个集群.
即user-service是服务,一个服务可以包含多个集群,如杭州、上海,每个集群下可以有多个实例,形成分级模型
即Nacos分级模型:
- 一级是服务,例如提供用户功能的userservice服务
- 二级是集群,例如杭州机房集群、上海机房集群
- 三级是实例,例如杭州集群中某一台部署了userservice服务的服务器
微服务互相访问时,应该尽可能访问同集群实例,因为本地访问速度更快.当本集群内不可用时,才访问其它集群.
本文就先介绍这些,后续仍然会介绍Nacos的相关知识,如集群,负载均衡等.
大佬感觉文章写的还不错的话,期待得到大佬的支持哟???
最后再次感谢大家???
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在.深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- 10 【组件编码流程 组件自定义事件 全局事件总线】
- leetcode-每日一题剑指 Offer II 041. 滑动窗口的平均值(队列模拟)
- 05 【绑定样式 条件渲染 列表渲染】
- Install mysqldb in mac10.14
- [Cloud native] Ribbon is no longer used at the bottom layer of OpenFeign starting from the 2020.0.X version
- 08 【生命周期 组件】
- 工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
- GUCCI、LV等奢侈品巨头如何布局元宇宙的,其他品牌应该跟上吗?
- 对递归的一些感悟
- What is the difference between NFT and digital collection?
猜你喜欢

Year-end summary - the years are quiet~

leetcode-每日一题873. 最长的斐波那契子序列的长度(哈希和二分)

What is GameFi?

Error: Cannot find module ‘D:\Application\nodejs\node_modules\npm\bin\npm-cli.js‘

08 【生命周期 组件】
![[Cloud native] Ribbon is no longer used at the bottom layer of OpenFeign starting from the 2020.0.X version](/img/7e/1d27e3f1856ab8c6cbfc5221c717bb.png)
[Cloud native] Ribbon is no longer used at the bottom layer of OpenFeign starting from the 2020.0.X version

【ubuntu20.04安装MySQL以及MySQL-workbench可视化工具】

mysql password modification method in Linux (pro-test available)

元宇宙的前景及四大赛道

10 【高度塌陷与BFC】
随机推荐
一文速学-玩转MySQL获取时间、格式转换各类操作方法详解
11 【组件通信】
vulhub靶场学习日记hackme1
MySQL压缩包方式安装,傻瓜式教学
leetcode-每日一题565. 数组嵌套(标记图和并查集)
16 【打包上线 图片懒加载】
对递归的一些感悟
MySQL compressed package installation, fool teaching
vulhub靶场学习日记xxe-lab
mysql启动报错The server quit without updating PID file几种解决办法
Artifact SSMwar exploded Error deploying artifact.See server log for details
什么是 GameFi?
在kali上搭建vulhub漏洞靶场
11 【定位】
uni-app进阶之模版语法与数据绑定【day7】
GUCCI, LV and other luxury giant universe how to layout yuan, other brands should keep up with?
10 【高度塌陷与BFC】
阿里一面,说说你知道消息中间件的应用场景有哪些?
leetcode-每日一题1252. 奇数值单元格的数目(模拟优化)
Understanding SSRF, this article is enough