zoukankan      html  css  js  c++  java
  • 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、基本测试用例

    下载 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

  • 相关阅读:
    自定义View之实现流行的底部菜单栏中间突起:高仿“咸鱼APP”的底部菜单
    android获取内置和外置SD卡路径
    Android Environment.getExternalStorageDirectory() 获取的是内部存储还是外部存储?
    bitmap过大无法显示图片的问题
    Android Notification的使用
    Android Device Monitor 文件管理的常见问题
    BroadcastReceiver广播相关
    Android Studio 设置代码提示和代码自动补全快捷键--Eclipse 风格
    Android Studio com.android.support:percent 导入错误
    约束布局constraint-layout导入失败的解决方案
  • 原文地址:https://www.cnblogs.com/xingchong/p/15783246.html
Copyright © 2011-2022 走看看