nats.md 6.7 KB


title: "Nats" date: 2023-07-26T13:11:48+07:00

draft: false

Nats Server 極速文檔

描述版本 nats server: 2.9.20 nats: 0.0.35

基於主題(Subject)的消息系統

規則

  • 主題名稱最好使用 [a-zA-Z0-9] 構成的字符串。可以使用 . 來級聯名稱,起到 Subject 分組(Group)的效果
  • 主題名稱不可包含空格,其中 $ 開頭的為系統主題
  • *, > 用於訂閱(Subscribe)主題時作為 wildcard 使用。*匹配單詞,> 置於結尾匹配主題分組 > time.*.east 匹配 time.us.east, time.eu.east > time.us.> 匹配 time.us.east, time.us.east.town, time.us.west > 可以使用多個 * 以及 >, eg. *.*.east.> 當前版本限制最多不超過 16 個

Core NATS 模式

Core NATS 模式的消息系統不具備存儲功能

  • Publish-Subscribe > 一對多通信
  • Request-Reply > 請求-應答模式,基於 pub-sub 模式實現
  • Queue Groups > 一個 message 每次只有一個 subscriber 能夠收到

JetStream 模式

術語

  • subject 也就是 Nats 模式下的主題
  • stream > stream 用於訂閱 subject,當創建 stream 時可指定訂閱的 subject(可多個),從此 pub 到該 subject > 的 message 都以 stream 的配置的策略存儲
  • consumer 用於客戶端消費 stream。作為 stream 的一個視圖(view),跟蹤客戶端的消費和確認進度 > 分為 push/pull 兩種消息獲取方式 > 可以是 ephemeral / durable 的

stream 參數

  • Name: 不含空格 . * > 的普通字符串
  • Storage: File/Memory
  • Subjects: 主題列表, 主題支持 wildcard
  • Replicas: 每個消息在 cluster 模式下的份數, 最大為5
  • MaxAge: 任意消息的壽命 nanoseconds
  • MaxBytes: stream 的容量(體積)
  • MaxMsgs: stream 的容量(消息個數)
  • MaxMsgSize: 消息尺寸極限
  • MaxConsumers: 最多消費者個數
  • NoAck: stream 接收消息不確認
  • Retention: LimitsPolicy/InterestPolicy/WorkQueuePolicy
  • Discard: DiscardOld / DiscardNew
  • Duplicates: 檢測重複消息的時間窗口 nanoseconds
  • Placement:
  • Mirror: 指定本 stream 是其他 stream 的 mirror
  • Sources: 指定本 stream 的源 stream 列表
  • MaxMsgsPerSubject: 每個 subject 中最大消息數
  • Description: 描述
  • Sealed
  • DenyDelete: 禁止通過 API 刪除
  • DenyPurge: 禁止通過 API 清空
  • AllowRollup
  • RePublish: 消息存儲後立即發送到指定subject
  • AllowDirect
  • MirrorDirect
  • DiscardNewPerSubject

consumer 參數

  • Durable: 設置後就為 durable 模式。不能使用空格 . * > /\ 符號,就普通字符串
  • FilterSubject:消費過濾器
  • AckPolicy: AckExplicit/AckNone/AckAll
  • AckWait: 服務傳遞消息到消費者後,如果在此時間內沒收到 ack,則重新傳遞
  • DeliverPolicy: stream 中接收消息的位置 DeliverAll / DeliverLast / DeliverNew / DeliverByStartSequence / DeliverByStartTime / DeliverLastPerSubject
  • OptStartSeq: 配合 DeliverByStartSequence 使用
  • OptStartTime: 配合 DeliverByStartTime
  • Description: 描述
  • InactiveThreshold: ephemeral 模式下,超出本設置無活動客戶端則清空 consumer
  • MaxAckPending: 未 ack 消息數量超出此值則停止傳遞
  • MaxDeliver: 未 ack 消息的最大傳遞次數
  • ReplayPolicy: ReplayOriginal / ReplayInstant
  • Replicas: 設置 consumer 狀態副本數。0表示從 stream 繼承設置
  • MemoryStorage: consumer state 保存在內存中
  • SampleFrequency: 為可觀察性設置的 ack 百分比 '30'/'30%'

KV存儲

JetStream 是存儲層,然後才是服務於 stream 的存儲層,所以包含 KV 存儲很自然

API

  • put
  • get
  • delete
  • purge
  • create
  • update
  • keys
  • watch
  • watch all
  • history

設置

  • bucket 的最大尺寸
  • 單個值的最大尺寸
  • TTL 一個值能存儲多久(redis 的 expiration)

啟動配置

配置文件格式

  • 格式為 JSON/YAML,JSON 鍵不需要引號,可以帶注釋(//或#)。utf8編碼
  • 值支持 字符串,數字,list, map。1K=1000, 1KB=1024。
  • 可以用 $ 使用變量, 變量可以是環境變量
  • 可以 include 其他配置文件
  • nats-server -c server.conf

子配置

  • Tls
    • cert_file
    • key_file
    • ca_file
    • cipher_suites
    • curve_preferences
    • insecure: NOT recommend
    • verify
    • verify_and_map
    • verify_cert_and_check_known_urls
    • pinned_certs
    • timeout
  • User
    • user: string
    • password: string
    • nkey: string
    • permissions: map
  • Authorization
    • token
    • user
    • password
    • users: [User]
    • timeout

配置項

  • host: 0.0.0.0
  • port: 4222
  • listen: :
  • client_advertise: 通告客戶端連接的 cluster 地址, host:port 格式
  • tls: Tls
  • ### profiling
  • prof_port: 6543
  • ### log
  • debug: false
  • trace: true
  • logtime: false
  • logfile_size_limit: 1G
  • log_file: "/tmp/nats-server.log"
  • gateway
    • name
    • reject_unknown_cluster
    • gateways: [gateway]
      • name
      • url
      • urls [url]
      • tls:Tls
    • host
    • port
    • listen
    • tls
    • advertise
    • connect_retries
    • authorization: Authorization
  • leafnodes
    • host
    • port
    • listen
    • tls: Tls
    • advertise
    • no_advertise
    • authorization: Authorization
    • remotes: []
      • url
      • urls
      • account: Account
      • credentials
      • tls
      • ws_compression
      • ws_no_masking
    • reconnect
    • authorization
      • user
      • password
      • account: Account
      • timeout
      • users: [user]
        • user
        • password
        • account: Account
  • cluster
    • host
    • port
    • name
    • listen
    • tls: Tls
    • advertise/cluster_advertise
    • no_advertise
    • routes: [url]
    • connect_retries
    • authorization: Authorization
  • ### timeouts
  • ping_interval: "2m"
  • ping_max: 2
  • write_deadline: "10s"
  • ### limites
  • max_connections: '64k'
  • max_control_line: 4KB
  • max_payload
  • max_pending
  • max_subscriptions
  • jetstream:
    • store_dir
    • max_mem
    • max_file
    • max_memory_store
    • max_file_store
    • cipher
    • key
    • max_outstanding_catchup
  • ### centralized auth
  • authorization
  • accounts
  • no_auth_user
  • ### decentralized auth
  • operator
  • resolver
  • resolver_tls
  • resolver_preload
  • ### runtime
  • disable_sublist_cache
  • lame_duck_duration
  • lame_duck_grace_period
  • ### monitor
  • server_name
  • server_tags
  • trace
  • trace_verbose
  • debug
  • logtime
  • log_file
  • log_size_limit
  • max_traced_msg_len
  • syslog
  • remote_syslog
  • http_port
  • http
  • https_port
  • http_base_path
  • https
  • system_account
  • pid_file
  • port_file_dir
  • connect_error_reports
  • reconnect_error_reports
  • ### mqtt
  • mqtt
    • host
    • port
    • listen
    • tls: Tls
    • authorization: Authorization
    • act_wait: "1m"
    • max_ack_pending: 100
  • websocket
    • host
    • port
    • advertise
    • tls: Tls
    • no_tls: true
    • same_origin: true
    • allowed_origins: [url]
    • compression: true
    • handshake_timeout: "2s"
    • jwt_cookie: 'my_jwt_cookie_name'
    • no_auth_user: ""
    • authorization: Authorization
  • Config file reload

    nats-server --signal reload