NATS 中文文档
  • 引言
  • 发布日志
  • NATS 2.0
  • 对比 NATS
  • FAQ
  • NATS Concepts
    • What is NATS
    • Subject-Based Messaging
    • Publish-Subscribe
    • Request-Reply
    • Queue Groups
    • Acknowledgements
    • Sequence Numbers
  • Developing With NATS
    • Introduction
    • Connecting
      • Connecting to the Default Server
      • Connecting to a Specific Server
      • Connecting to a Cluster
      • Connection Name
      • Setting a Connect Timeout
      • Ping/Pong Protocol
      • Turning Off Echo'd Messages
      • Miscellaneous functionalities
    • Automatic Reconnections
      • Disabling Reconnect
      • Set the Number of Reconnect Attempts
      • Avoiding the Thundering Herd
      • Pausing Between Reconnect Attempts
      • Listening for Reconnect Events
      • Buffering Messages During Reconnect Attempts
    • Securing Connections
      • Authenticating with a User and Password
      • Authenticating with a Token
      • Authenticating with an NKey
      • Authenticating with a Credentials File
      • Encrypting Connections with TLS
    • Receiving Messages
      • Synchronous Subscriptions
      • Asynchronous Subscriptions
      • Unsubscribing
      • Unsubscribing After N Messages
      • Replying to a Message
      • Wildcard Subscriptions
      • Queue Subscriptions
      • Draining Messages Before Disconnect
      • Structured Data
    • Sending Messages
      • Including a Reply Subject
      • Request-Reply Semantics
      • Caches, Flush and Ping
      • Sending Structured Data
    • Monitoring the Connection
      • Listen for Connection Events
      • Slow Consumers
    • Tutorials
      • Explore NATS Pub/Sub
      • Explore NATS Request/Reply
      • Explore NATS Queueing
      • Advanced Connect and Custom Dialer in Go
  • NATS Server
    • Installing
    • Running
      • Windows Service
    • Clients
    • Flags
    • Configuration
      • Securing NATS
        • Enabling TLS
        • Authentication
          • Tokens
          • Username/Password
          • TLS Authentication
          • NKeys
          • Authentication Timeout
        • Authorization
        • Multi Tenancy using Accounts
        • Decentralized JWT Authentication/Authorization
          • Account lookup using Resolver
          • Memory Resolver Tutorial
          • Mixed Authentication/Authorization Setup
      • Clustering
        • Configuration
        • TLS Authentication
      • Super-cluster with Gateways
        • Configuration
      • Leaf Nodes
        • Configuration
      • Logging
      • Monitoring
      • System Events
        • System Events & Decentralized JWT Tutorial
    • Managing A NATS Server
      • Upgrading a Cluster
      • Slow Consumers
      • Signals
    • NATS and Docker
      • Tutorial
      • Docker Swarm
      • Python and NGS Running in Docker
  • NATS Tools
    • Introduction
    • mkpasswd
    • nk
    • nsc
      • Basics
      • Streams
      • Services
      • Signing Keys
      • Revocation
      • Managed Operators
    • nats-account-server
      • Basics
      • Inspecting JWTs
      • Directory Store
      • Update Notifications
    • nats-top
      • Tutorial
    • nats-bench
  • NATS Streaming Concepts
    • Introduction
    • Relation to NATS
    • Client Connections
    • Channels
      • Message Log
      • Subscriptions
        • Regular
        • Durable
        • Queue Group
        • Redelivery
    • Store Interface
    • Store Encryption
    • Clustering
      • Supported Stores
      • Configuration
      • Auto Configuration
      • Containers
    • Fault Tolerance
      • Active Server
      • Standby Servers
      • Shared State
      • Failover
    • Partitioning
    • Monitoring
      • Endpoints
  • Developing With NATS Streaming
    • Introduction
    • Connecting to NATS Streaming
    • Publishing to a Channel
    • Receiving Messages from a Channel
    • Durable Subscriptions
    • Queue Subscriptions
    • Acknowledgements
    • The Streaming Protocol
  • NATS Streaming Server
    • Important Changes
    • Installing
    • Running
    • Configuring
      • Command Line Arguments
      • Configuration File
      • Store Limits
      • 持久化
        • 文件存储
        • SQL 存储
      • Securing
    • Process Signaling
    • Windows Service
    • Embedding NATS Streaming Server
    • Docker Swarm
  • NATS Protocol
    • Protocol Demo
    • Client Protocol
      • Developing a Client
    • NATS Cluster Protocol
  • 在 Kubernetes中使用NATS
    • 序言
    • 安装 NATS 和 NATS Streaming
    • 创建一个 Kubernetes 集群
    • 容错(Fault Tolerance)模式下的NATS Streaming 集群
    • NATS 和 Prometheus Operator
    • NATS 集群和证书管理
    • 使用 cfssl 来提高 NATS 集群的安全性
    • 使用负载均衡器(Load Balancer) 为NATS提供外部访问
    • 使用Helm在Digital Ocean 创建一个NATS 超级集群
    • 使用Helm从0到 K8s到 子节点
由 GitBook 提供支持
在本页
  • Prerequisites
  • 1. Start the NATS server
  • 2. Start a shell or command prompt session
  • 3. CD to the Go client examples directory
  • 4. Run the client subscriber program
  • 5. Start another shell or command prompt session
  • 6. CD to the examples directory
  • 7. Publish a NATS message
  • 8. Verify message publication and receipt
  • 9. Publish another message
  • 10. Start another shell or command prompt session
  • 11. CD to the examples directory
  • 12. Start a second client subscriber program
  • 13. Publish another message using the publisher client
  • 14. Start another shell or command prompt session
  • 15. CD to the examples directory
  • 16. Subscribe to a different subject
  • 17. Update the last subscriber to use a wildcard
  • 18. Publish another message

这有帮助吗?

  1. Developing With NATS
  2. Tutorials

Explore NATS Pub/Sub

上一页Tutorials下一页Explore NATS Request/Reply

最后更新于4年前

这有帮助吗?

NATS is a messaging system . Subscribers listening on a subject receive messages published on that subject. If the subscriber is not actively listening on the subject, the message is not received. Subscribers can use the wildcard tokens such as * and > to match a single token or to match the tail of a subject.

Prerequisites

Go and the NATS server should be installed. Optionally you can use the demo server located at nats://demo.nats.io

1. Start the NATS server

% nats-server

When the server starts successfully, you will see the following messages:

[1] 2019/31/05 15:18:22.301550 [INF] Starting nats-server version 2.0.0
[1] 2019/31/05 15:18:22.301762 [INF] Listening for client connections on 0.0.0.0:4222
[1] 2019/31/05 15:18:22.301769 [INF] nats-server is ready

The NATS server listens for client connections on TCP Port 4222.

2. Start a shell or command prompt session

You will use this session to run an example NATS client subscriber program.

3. CD to the Go client examples directory

% cd $GOPATH/src/github.com/nats-io/nats.go/examples

4. Run the client subscriber program

% go run nats-sub/main.go <subject>

Where <subject> is a subject to listen on. A valid subject is a string that is unique in the system.

For example:

% go run nats-sub/main.go msg.test

You should see the message: Listening on [msg.test]

5. Start another shell or command prompt session

You will use this session to run a NATS publisher client.

6. CD to the examples directory

% cd $GOPATH/src/github.com/nats-io/nats.go/examples

7. Publish a NATS message

% go run nats-pub/main.go <subject> <message>

Where <subject> is the subject name and <message> is the text to publish.

For example:

% go run nats-pub/main.go msg.test hello

or

% go run nats-pub/main.go msg.test "NATS MESSAGE"

8. Verify message publication and receipt

You should see that the publisher sends the message and prints: Published [msg.test] : 'NATS MESSAGE'

And that the subscriber receives the message and prints: [#1] Received on [msg.test]: 'NATS MESSAGE'

Note that if the receiver does not get the message, check that you are using the same subject name for the publisher and the subscriber.

9. Publish another message

% go run nats-pub/main.go msg.test "NATS MESSAGE 2"

You should see that the subscriber receives message 2. Note that the message count is incremented each time your subscribing client receives a message on that subject:

10. Start another shell or command prompt session

You will use this session to run a second NATS subscriber.

11. CD to the examples directory

% cd $GOPATH/src/github.com/nats-io/nats.go/examples

12. Start a second client subscriber program

% go run nats-sub/main.go msg.test

13. Publish another message using the publisher client

% go run nats-pub/main.go msg.test "NATS MESSAGE 3"

Verify that both subscribing clients receive the message.

14. Start another shell or command prompt session

You will use this session to run a third NATS subscriber.

15. CD to the examples directory

% cd $GOPATH/src/github.com/nats-io/nats.go/examples

16. Subscribe to a different subject

% go run nats-sub/main.go msg.test.new

All the but last subscriber receives the message. Why? Because that subscriber is not listening on the message subject used by the publisher.

17. Update the last subscriber to use a wildcard

NATS supports the use of wildcard characters for message subscribers only. You cannot publish a message using a wildcard subject.

Change the last subscriber the listen on msg.* and run it:

% go run nats-sub/main.go msg.*

18. Publish another message

This time, all three subscribing clients should receive the message.

publish subscribe
based on subjects