NATS——NATS初識——NATS服務器啟動——測試用例運行


NATS初識
https://github.com/nats-io

https://github.com/nats-io/nats-server

https://github.com/nats-io/nats.go

https://github.com/nats-io/nats-streaming-server

https://github.com/nats-io/stan.go

https://nats.io/download/

https://docs.nats.io/running-a-nats-service/introduction/installation

https://docs.nats.io/running-a-nats-service/clients

 

核心 nats-server 無持久化。

nats-streaming-server  streaming內置nats-server服務器,帶持久化 。

 

nats-server 的啟動與測試

1、服務啟動

下載 https://github.com/nats-io/nats-server 后, 進入目錄../nats/nats-io/nats-server-main

執行 go build, 生成可執行文件 nats-server

執行 ./nats-server

localhost:nats-server-main chong$ ./nats-server 
[9854] 2022/01/10 15:43:29.571138 [INF] Starting nats-server
[9854] 2022/01/10 15:43:29.571426 [INF]   Version:  2.7.0-beta
[9854] 2022/01/10 15:43:29.571430 [INF]   Git:      [not set]
[9854] 2022/01/10 15:43:29.571440 [INF]   Name:     NBNLIDMD3YGE7XIQ54PVN2WYRFINJ6NONBIQJYP6F4VSTHYMS4ORFHPQ
[9854] 2022/01/10 15:43:29.571460 [INF]   ID:       NBNLIDMD3YGE7XIQ54PVN2WYRFINJ6NONBIQJYP6F4VSTHYMS4ORFHPQ
[9854] 2022/01/10 15:43:29.584612 [INF] Listening for client connections on 0.0.0.0:4222
[9854] 2022/01/10 15:43:29.585148 [INF] Server is ready

2、基本測試用例

這是go版本的nats客戶端

下載 https://github.com/nats-io/nats.go 后, 進入目錄../nats/nats.go-main/examples

先啟用主題監聽     go run nats-sub/main.go -s nats://127.0.0.1:4222 -t foo

發布主題及消息     go run nats-pub/main.go -s nats://127.0.0.1:4222 foo "msg two"

 

localhost:examples chong$ go run nats-pub/main.go  -s nats://127.0.0.1:4222 foo "msg two"
Published [foo] : 'msg two'
localhost:examples chong$ 
localhost:examples chong$ go run nats-pub/main.go  -s nats://127.0.0.1:4222 foo "msg two"
Published [foo] : 'msg two'

 

localhost:examples chong$ go run nats-sub/main.go -s nats://127.0.0.1:4222 -t foo
Listening on [foo]
2022/01/10 15:51:35 [#1] Received on [foo]: 'msg two'
2022/01/10 15:51:47 [#2] Received on [foo]: 'msg two'
2022/01/10 15:51:52 Disconnected due to:EOF, will attempt reconnects for 10m
2022/01/10 15:51:53 Reconnected [nats://127.0.0.1:4222]
2022/01/10 15:52:06 [#3] Received on [foo]: 'msg one'

 

 

 

nats-streaming-serve 的啟動與測試

1、服務啟動

下載源碼 https://github.com/nats-io/nats-streaming-server 后,進入../nats/nats-streaming-server-main目錄

執行 go build , 生成可執行文件 nats-streaming-server

不帶參數的啟動命令    ./nats-streaming-server

帶參數的的啟動命令

./nats-streaming-server --store file --dir /Users/chong/Documents/code/go/nats/streaming-store-files --max_msgs 0 --max_bytes 0

localhost:nats-streaming-server-main chong$ ./nats-streaming-server --store file \
--dir /Users/chong/Documents/code/go/nats/streaming-store-files \
--max_msgs 0 --max_bytes 0 [91741] 2022/01/09 09:28:54.749153 [INF] STREAM:
Starting nats-streaming-server[test-cluster] version 0.23.2 [91741] 2022/01/09 09:28:54.749378 [INF] STREAM: ServerID: iL8tEmZBdTqud0rQbwzuro [91741] 2022/01/09 09:28:54.749382 [INF] STREAM: Go version: go1.17.3 [91741] 2022/01/09 09:28:54.749385 [INF] STREAM: Git commit: [not set] [91741] 2022/01/09 09:28:54.753269 [INF] Starting nats-server [91741] 2022/01/09 09:28:54.753288 [INF] Version: 2.6.5 [91741] 2022/01/09 09:28:54.753292 [INF] Git: [not set] [91741] 2022/01/09 09:28:54.753302 [INF] Name: NDBGMWEP6JYRPYAMU3XUZLCIRJHEZNDRLIZEUTLMOTHH [91741] 2022/01/09 09:28:54.753306 [INF] ID: NDBGMWEP6JYRPYAMU3XUZLCIRJ3HZNDRLIZEUTLMOTHH [91741] 2022/01/09 09:28:54.758760 [INF] Listening for client connections on 0.0.0.0:4222 [91741] 2022/01/09 09:28:54.759308 [INF] Server is ready [91741] 2022/01/09 09:28:54.785453 [INF] STREAM: Recovering the state... [91741] 2022/01/09 09:28:54.785841 [INF] STREAM: No recovered state [91741] 2022/01/09 09:28:54.787267 [INF] STREAM: Message store is FILE [91741] 2022/01/09 09:28:54.787276 [INF] STREAM: Store location:
/Users/chong/Documents/code/go/nats/streaming-store-files [91741] 2022/01/09 09:28:54.787338 [INF] STREAM: ---------- Store Limits ---------- [91741] 2022/01/09 09:28:54.787343 [INF] STREAM: Channels: 100 * [91741] 2022/01/09 09:28:54.787346 [INF] STREAM: --------- Channels Limits -------- [91741] 2022/01/09 09:28:54.787350 [INF] STREAM: Subscriptions: 1000 * [91741] 2022/01/09 09:28:54.787353 [INF] STREAM: Messages : unlimited [91741] 2022/01/09 09:28:54.787356 [INF] STREAM: Bytes : unlimited [91741] 2022/01/09 09:28:54.787359 [INF] STREAM: Age : unlimited * [91741] 2022/01/09 09:28:54.787362 [INF] STREAM: Inactivity : unlimited * [91741] 2022/01/09 09:28:54.787365 [INF] STREAM: ---------------------------------- [91741] 2022/01/09 09:28:54.787368 [INF] STREAM: Streaming Server is ready

2、測試用例

 下載源碼 https://github.com/nats-io/stan.go , 進入目錄 ../nats/stan.go-main/examples

發布主題及消息   go run stan-pub/main.go -s nats://127.0.0.1:4222 foo "msg nine..."

訂閱主題            go run stan-sub/main.go -s nats://127.0.0.1:4222 -t foo

發布過程如下:

localhost:examples chong$ go run stan-pub/main.go foo "msg nine..."
go: downloading github.com/nats-io/nats.go v1.13.0
go: downloading github.com/gogo/protobuf v1.3.2
Published [foo] : 'msg nine...'
localhost:examples chong$ 
localhost:examples chong$ 
localhost:examples chong$ 
localhost:examples chong$ 
localhost:examples chong$ go run stan-pub/main.go foo "msg nine..."
Published [foo] : 'msg nine...'
localhost:examples chong$ 
localhost:examples chong$ 
localhost:examples chong$ go run stan-pub/main.go foo "msg nine..."
Published [foo] : 'msg nine...'
localhost:examples chong$ 
localhost:examples chong$ pwd
/Users/chong/Documents/code/go/nats/stan.go-main/examples
localhost:examples chong$ 
localhost:examples chong$ 
localhost:examples chong$ go run stan-pub/main.go -s nats://127.0.0.1:4222 foo "msg nine..."
Published [foo] : 'msg nine...'
localhost:examples chong$ 

首次執行時,go會根據 go.mod 獲取、安裝依賴。

訂閱情況如下:

localhost:examples chong$ go run stan-sub/main.go -s nats://127.0.0.1:4222 -t foo
Connected to nats://127.0.0.1:4222 clusterID: [test-cluster] clientID: [stan-sub]
Listening on [foo], clientID=[stan-sub], qgroup=[] durable=[]
2022/01/10 16:26:43 [#1] Received: sequence:1 subject:"foo" data:"msg nine..." timestamp:1641802805067115000 
2022/01/10 16:26:43 [#2] Received: sequence:2 subject:"foo" data:"msg nine..." timestamp:1641802826574706000 
2022/01/10 16:26:43 [#3] Received: sequence:3 subject:"foo" data:"msg nine..." timestamp:1641802869268707000 
2022/01/10 16:26:43 [#4] Received: sequence:4 subject:"foo" data:"msg nine..." timestamp:1641803177556474000 

訂閱中的可選參數

go run examples/stan-sub/main.go -s nats://127.0.0.1:4222 --all -c test-cluster  -id stan-sub -t foo

go run examples/stan-sub/main.go -s nats://127.0.0.1:4222 --seq 4 -c test-cluster  -id stan-sub -t foo

go run examples/stan-sub/main.go -s nats://127.0.0.1:4222 --last -c test-cluster  -id stan-sub -t foo

    --seq <seqno>           從指定的序列號開始
    --all                   接受所有發送的數據
    --last                  從上一次最后一次發送的那條數據開始
    --since <duration>      從當前往前的時間段開始接收(例如:1s, 1hr, 具體可以參看:https://golang.org/pkg/time/#ParseDuration)
    --durable <name>       永久訂閱者的名稱
    --unsubscribe           退出時解除永久訂閱

 

 

 

附:命令備份

go env -w GO111MODULE=on #開啟go mod管理,同理off就是關閉
開啟之后,我們要import自己的包就會在go.mod路徑下進行尋址

go.mod內容如下:
#這里的go.mod是使用go mod init xx生成的
module 05-go
go 1.17
安裝gopls go get -v


運行nats的測試用例
進入測試用例目錄 nats.go-main/examples

發布主題為foo的消息
go run nats-pub/main.go -s nats://127.0.0.1:4222 foo "msg one"
訂閱主題為foo的消息
go run nats-sub/main.go -s nats://127.0.0.1:4222 -t foo

 

 

在 goland 中啟動 nats-server

1、配置

 

2、啟動

 

 

 

 

 

 

4、在命令行測試

開兩個 cmd 窗口  A   和   B, 分別執行  telnet 10.60.80.11 4222

顯示如下:

[D:\git\go\etcd]$ telnet 10.60.80.11 4222


Connecting to 10.60.80.11:4222...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
INFO {"server_id":"NDI3F7PI75QKRAY2MH2X6YAL7XVQDX6LWODFU5H5HBNM4LQSYSIF3V25",

"server_name":"NDI3F7PI75QKRAY2MH2X6YAL7XVQDX6LWODFU5H5HBNM4LQSYSIF3V25",

"version":"2.9.0-RC.3","proto":1,"go":"go1.18.4","host":"0.0.0.0","port":4222,"headers":true,

"max_payload":1048576,"client_id":4,"client_ip":"10.60.80.11"}

在A中訂閱主題    

sub topic001.* 89
+OK

在B中發布主題 ,注意這兩行分兩次輸入, 發布主題為 topic001.uu ,內容長度為5 的主題,

再輸入內容 hello, 此時在A中就顯示主閱到

pub topic001.uu 5
hello
+OK

A中顯示

sub topic001.* 89
+OK
MSG topic001.uu 89 5
hello

 

 

ps:  

為goland配置go sdk報錯問題 

低版本的goland還需要配置GOROOT,但是在配置go1.17以上的時候就一直報這個錯誤

Goland配置GOROOT報錯:The selected directory is not a valid home for Go Sdk

原因goland版本過低,當go版本大於1.17時就會報此異常。

解決方案:

編輯自己go安裝目錄下的:go\src\runtime\internal\sys\zversion.go

增加一行自己的版本:

const TheVersion = `go1.18.4`


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM