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`

posted @ 2022-01-10 09:57  会飞的斧头  阅读(2658)  评论(0编辑  收藏  举报