43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package nats_client
|
|
|
|
import (
|
|
"fmt"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule-agent/internal/model/nats_msg_model"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule-agent/internal/nats_service/definition"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule-agent/pkg/log"
|
|
"github.com/nats-io/nats.go"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestPublish(t *testing.T) {
|
|
Publish("hello", []byte("萨达咖啡机阿拉山口戴假发立卡就撒到啦开发机"))
|
|
}
|
|
|
|
func TestSubscribe(t *testing.T) {
|
|
// Connect Options.
|
|
opts := []nats.Option{nats.Name("NATS Sample Publisher")}
|
|
opts = append(opts, nats.UserInfo(NatsConfig.User, NatsConfig.Password))
|
|
|
|
nc, _ := nats.Connect(NatsConfig.Url, opts...)
|
|
|
|
nc.Subscribe(definition.ToScheduleSubject, func(msg *nats.Msg) {
|
|
fmt.Println("接收到数据:" + string(msg.Data))
|
|
model, err := nats_msg_model.UnmarshalMsgModel(msg.Data)
|
|
if err != nil {
|
|
fmt.Println("反序列化失败:" + err.Error())
|
|
}
|
|
|
|
fmt.Println(model.Func)
|
|
|
|
})
|
|
|
|
nc.Flush()
|
|
if err := nc.LastError(); err != nil {
|
|
log.Error(err)
|
|
}
|
|
|
|
time.Sleep(time.Second * 500)
|
|
|
|
}
|