没有合适的资源?快使用搜索试试~ 我知道了~
首页NATS实战指南:从入门到精通
NATS实战指南:从入门到精通
需积分: 9 4 下载量 97 浏览量
更新于2024-07-18
收藏 4.76MB PDF 举报
"《Practical NATS -- 2018》是一本由Waldemar Quevedo编著的实用指南,旨在帮助读者从入门到专业地理解和掌握NATS这一强大的实时消息传递系统。该书的核心内容围绕NATS在构建应用中的实际应用,特别是作为组件间通信的控制平面。 在本书中,作者首先会引导读者了解NATS的基本原理,深入剖析其工作原理,包括客户端提供的基本构建块如何实现高级通信模式。读者可以期待学习到如何利用NATS创建具有高可扩展性和实时性的应用程序,这些应用程序通过NATS的高效、轻量级架构进行组件间的协调和通信。 此外,书中详细讲解了如何安装、配置和管理NATS服务器,以及在实际操作中可能遇到的常见问题及其解决方法。这包括设置服务器的最佳实践、网络配置、负载均衡、安全性配置,以及如何处理如连接问题、性能瓶颈等故障场景的诊断和调试技巧。 作为一本实用教程,读者不仅能够获得理论知识,还能通过具体的实例和案例研究,将所学知识应用到实际项目中。书中的ISBN号码(pbk: 978-1-4842-3569-0, electronic: 978-1-4842-3570-6)和Library of Congress Control Number(2018946546)提供了进一步获取和引用的信息,而版权信息强调了所有权利保留,确保了知识的完整性和原创性。 《Practical NATS -- 2018》是一本非常适合IT专业人士和开发者,尤其是对实时消息传递系统感兴趣的读者深入学习和提升NATS技能的权威参考书籍。无论你是初学者还是希望进一步深化理解的专家,都能从中受益匪浅。"
资源详情
资源推荐
1
© Waldemar Quevedo 2018
W. Quevedo, Practical NATS, https://doi.org/10.1007/978-1-4842-3570-6_1
CHAPTER 1
Introduction toNATS
NATS is a high-performance messaging system created by Derek Collison
in 2010. It was originally built to serve as the message bus for Cloud
Foundry, handling internal communication among components of the
system. With the rise of microservices and cloud native paradigms, NATS
has increased in popularity, becoming a mainstream piece of modern
cloud architectures. The nats-io/gnatsd repository in GitHub now has
over 3K stars and there is a growing ecosystem of tools and projects that us
NATS as part of their architecture, as many have found it useful to address
concerns such as:
• Service discovery
• Low latency communication
• Load balancing
• Notifications and events handling
In this chapter, you will learn:
• What NATS is and when to consider using messaging
• NATS’ features and design
• A brief history of the project
2
Using NATS forMessaging
NATS is at its core a Publish/Subscribe (PubSub) system. The PubSub
messaging model allows clients in a system to communicate without having
to deal with the precise endpoints of where the services are located in the
network, delegating this responsibility instead to the messaging system
(see Figure1-1). Clients become subscribers (or consumers) by registering
interest into a subject, then whenever a publisher (or producer) client
emits a message on that subject, the messaging system will deliver it to the
available subscribers that were interested in that topic.
Figure 1-1. Publish/subscribe messaging
In NATS, this message delivery is brokered by the NATS Server (gnatsd).
Clients establish a TCP connection to it and follow the NATS protocol,
which was designed for simple and efficient PubSub messaging. Listing 1-1
provides a basic example of using telnet to interact with the NATS Server,
using the demo.nats.io endpoint which publicly available for testing.
Chapter 1 IntroduCtIon tonatS
3
Listing 1-1. Hello World, NATS Style
telnet demo.nats.io 4222
INFO {"server_id":"EiRJABZmVpWQDpriVqbbtw",...,
"max_payload":1048576}
SUB greetings 1
+OK
PUB greetings 12
Hello World!
+OK
MSG greetings 1 12
Hello World!
The previous (albeit simple) example already shows quite a bit of
the basic, yet powerful, feature set that NATS provides. We are making
a subscription on the greetings subject (SUB), then publishing (PUB)
and receiving a message (MSG) on that same subject. Here the message
published is an opaque blob of data (in this case just an array of bytes
with the Hello World! Characters, although we could have used any
type of encoding as part of the payload). The NATS protocol is fairly
straightforward with just a few number of commands, and this helps in
making the implementation of the clients less complex.
Simplicity is a recurrent theme in NATS, as the project from its
foundations had as a goal to be a lightweight messaging system and do
less overall. Unlike other messaging systems, the NATS Server will only
be keeping limited state for the client and only as as long as it has an
established connection to the server. As soon as the client disconnects,
the server will clear up any state related to the client, and it will not persist
any messages and deliver them to the client in case it later reconnects,
considering it a fresh new session with the server instead.
Chapter 1 IntroduCtIon tonatS
4
Another distinguishing factor of NATS is its great performance. NATS
excels at enabling low-latency communication among services, and
the way that the request response mechanism works in the clients was
designed to address this specific use case in mind. Derek originally came
up with the idea from a lesson learned during his time at Google, where
making a request would involve a large number of machines responding
back, but the client making the request would care only about a single
response, namely the fastest one.
Thorough benchmarks done by Tyler Treat, who has spent significant
time documenting the trade-offs taken by multiple messaging systems on
his blog at bravenewgeek.com, demonstrate that NATS shows “predictable,
tighter tail latencies”
1
in request/response round-trip benchmarks,
especially when dealing with smaller messages. By default, the maximum
payload size for a single NATS message is 1MB (although this can be tuned
in the server).
Messaging Over theREST
Although these days HTTP-based REST APIs are very popular and best
practices are well known, using messaging-based approaches instead for
communicating offer a number of benefits when dealing with complex
distributed systems. As previously mentioned, the PubSub model helps
us decouple the services and instead just focus on communicating and
sending the messages.
Consider for example what is involved in making an HTTP request.
First, it is needed to look up an available endpoint (e.g., via DNS) of the
service to which the client can connect and make the request. At this point
it may still be possible that the endpoint is actually unhealthy, so it would
1
“Benchmarking Message Queue Latency”
https://bravenewgeek.com/benchmarking-message-queue-latency/
Chapter 1 IntroduCtIon tonatS
5
be needed to retry and attempt to connect to another endpoint of the
service. After successfully establishing a connection, the client will make
the request and wait for the response back synchronously, then finally
close the connection. In many programming languages, developers need
to carefully manage the resources involved throughout. In Go, for
example, it is a common programming error to leak sockets when using
the net/http package, thus requiring careful code review or help via static
analysis methods.
In comparison, a NATS-based request involves much less overhead,
both in terms of the protocol and what is needed to keep in mind
when making it. There is no need to have an established point-to-point
connection against the service to which we are making the request;
instead, the client ought to be connected to an available NATS Server
already and just publish the message, then wait for the message containing
the reply to be delivered asynchronously.
Do Not Assume theAudience
When using NATS, we are advised to never assume the audience of who is
going to be consuming the message, as there can be multiple consumers
for the same message for various independent reasons. NATS has support
for wildcard subscriptions on a subject, and with enough permissions, it
is possible to audit or trace every single message being sent through the
wire without affecting how other parts of the systems are communicating.
In Figure1-2, there can be a number of “worker” clients subscribed to
a subject that can reply to published requests, but all these requests are
being logged by an “audit” client that does not reply.
Chapter 1 IntroduCtIon tonatS
剩余270页未读,继续阅读
白鸽-小蛋
- 粉丝: 10
- 资源: 166
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 新型矿用本安直流稳压电源设计:双重保护电路
- 煤矿掘进工作面安全因素研究:结构方程模型
- 利用同位素位移探测原子内部新型力
- 钻锚机钻臂动力学仿真分析与优化
- 钻孔成像技术在巷道松动圈检测与支护设计中的应用
- 极化与非极化ep碰撞中J/ψ的Sivers与cos2φ效应:理论分析与COMPASS验证
- 新疆矿区1200m深孔钻探关键技术与实践
- 建筑行业事故预防:综合动态事故致因理论的应用
- 北斗卫星监测系统在电网塔形实时监控中的应用
- 煤层气羽状水平井数值模拟:交替隐式算法的应用
- 开放字符串T对偶与双空间坐标变换
- 煤矿瓦斯抽采半径测定新方法——瓦斯储量法
- 大倾角大采高工作面设备稳定与安全控制关键技术
- 超标违规背景下的热波动影响分析
- 中国煤矿选煤设计进展与挑战:历史、现状与未来发展
- 反演技术与RBF神经网络在移动机器人控制中的应用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功