本文整理汇总了Golang中github.com/apcera/nats.Connect函数的典型用法代码示例。如果您正苦于以下问题:Golang Connect函数的具体用法?Golang Connect怎么用?Golang Connect使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Connect函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: ExampleConnect
// Shows different ways to create a Conn
func ExampleConnect() {
nats.Connect(nats.DefaultURL)
nats.Connect("nats://derek:[email protected]:421")
opts := nats.Options{
AllowReconnect: true,
MaxReconnect: 10,
ReconnectWait: 5 * time.Second,
Timeout: 1 * time.Second,
}
nc, _ := opts.Connect()
nc.Close()
}
开发者ID:MarkyV,项目名称:nats,代码行数:16,代码来源:example_test.go
示例2: NewGnatsd
func NewGnatsd(numberOfMessages int, testLatency bool) *Gnatsd {
conn, _ := nats.Connect(nats.DefaultURL)
// We want to be alerted if we get disconnected, this will
// be due to Slow Consumer.
conn.Opts.AllowReconnect = false
// Report async errors.
conn.Opts.AsyncErrorCB = func(nc *nats.Conn, sub *nats.Subscription, err error) {
panic(fmt.Sprintf("NATS: Received an async error! %v\n", err))
}
// Report a disconnect scenario.
conn.Opts.DisconnectedCB = func(nc *nats.Conn) {
fmt.Printf("Getting behind! %d\n", nc.OutMsgs-nc.InMsgs)
panic("NATS: Got disconnected!")
}
var handler benchmark.MessageHandler
if testLatency {
handler = &benchmark.LatencyMessageHandler{
NumberOfMessages: numberOfMessages,
Latencies: []float32{},
}
} else {
handler = &benchmark.ThroughputMessageHandler{NumberOfMessages: numberOfMessages}
}
return &Gnatsd{
handler: handler,
subject: "test",
conn: conn,
testLatency: testLatency,
}
}
开发者ID:hitomi333,项目名称:mq-benchmarking,代码行数:35,代码来源:gnatsd.go
示例3: ExampleConn_PublishMsg
func ExampleConn_PublishMsg() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
msg := &nats.Msg{Subject: "foo", Reply: "bar", Data: []byte("Hello World!")}
nc.PublishMsg(msg)
}
开发者ID:MarkyV,项目名称:nats,代码行数:7,代码来源:example_test.go
示例4: Dial
func (n *ntport) Dial(addr string, opts ...transport.DialOption) (transport.Client, error) {
cAddr := nats.DefaultURL
if len(n.addrs) > 0 && strings.HasPrefix(n.addrs[0], "nats://") {
cAddr = n.addrs[0]
}
c, err := nats.Connect(cAddr)
if err != nil {
return nil, err
}
id := nats.NewInbox()
sub, err := c.SubscribeSync(id)
if err != nil {
return nil, err
}
return &ntportClient{
conn: c,
addr: addr,
id: id,
sub: sub,
}, nil
}
开发者ID:kgrvamsi,项目名称:go-micro,代码行数:25,代码来源:nats.go
示例5: ExampleConn_Subscribe
// This Example shows an asynchronous subscriber.
func ExampleConn_Subscribe() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
nc.Subscribe("foo", func(m *nats.Msg) {
fmt.Printf("Received a message: %s\n", string(m.Data))
})
}
开发者ID:MarkyV,项目名称:nats,代码行数:9,代码来源:example_test.go
示例6: ExampleSubscription_Unsubscribe
func ExampleSubscription_Unsubscribe() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
sub, _ := nc.SubscribeSync("foo")
// ...
sub.Unsubscribe()
}
开发者ID:MarkyV,项目名称:nats,代码行数:8,代码来源:example_test.go
示例7: ExampleConn_Request
func ExampleConn_Request() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
nc.Subscribe("foo", func(m *nats.Msg) {
nc.Publish(m.Reply, []byte("I will help you"))
})
nc.Request("foo", []byte("help"), 50*time.Millisecond)
}
开发者ID:MarkyV,项目名称:nats,代码行数:9,代码来源:example_test.go
示例8: main
func main() {
nc, _ := nats.Connect("nats://yourhost:4222")
defer nc.Close()
nc.QueueSubscribe(TOPIC, QUEUE, func(m *nats.Msg) {
fmt.Println(string(m.Data))
open.Start(string(m.Data))
})
select {}
}
开发者ID:anykao,项目名称:p,代码行数:9,代码来源:bopen.go
示例9: Open
func (b Bopen) Open(url string) {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
if b.Topic != "" {
nc.Publish(b.Topic, []byte(url))
} else {
nc.Publish("url", []byte(url))
}
}
开发者ID:anykao,项目名称:p,代码行数:9,代码来源:bopen_linux.go
示例10: ExampleConn_QueueSubscribe
func ExampleConn_QueueSubscribe() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
received := 0
nc.QueueSubscribe("foo", "worker_group", func(_ *nats.Msg) {
received += 1
})
}
开发者ID:MarkyV,项目名称:nats,代码行数:10,代码来源:example_test.go
示例11: Init
func (nc *NatsClient) Init(server, service string) {
var err error
nc.natsConn, err = nats.Connect(server)
if err != nil {
panic(err)
}
nc.natsEnConn, err = nats.NewEncodedConn(nc.natsConn, nats.JSON_ENCODER)
if err != nil {
panic(err)
}
//defer ec.Close()
}
开发者ID:sbinq,项目名称:goatt,代码行数:12,代码来源:client_nats.go
示例12: ExampleSubscription_NextMsg
func ExampleSubscription_NextMsg() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
sub, _ := nc.SubscribeSync("foo")
m, err := sub.NextMsg(1 * time.Second)
if err == nil {
fmt.Printf("Received a message: %s\n", string(m.Data))
} else {
fmt.Println("NextMsg timed out.")
}
}
开发者ID:MarkyV,项目名称:nats,代码行数:12,代码来源:example_test.go
示例13: ExampleConn_Flush
func ExampleConn_Flush() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
msg := &nats.Msg{Subject: "foo", Reply: "bar", Data: []byte("Hello World!")}
for i := 0; i < 1000; i++ {
nc.PublishMsg(msg)
}
err := nc.Flush()
if err == nil {
// Everything has been processed by the server for nc *Conn.
}
}
开发者ID:MarkyV,项目名称:nats,代码行数:13,代码来源:example_test.go
示例14: ExampleEncodedConn_Publish
// EncodedConn can publish virtually anything just
// by passing it in. The encoder will be used to properly
// encode the raw Go type
func ExampleEncodedConn_Publish() {
nc, _ := nats.Connect(nats.DefaultURL)
c, _ := nats.NewEncodedConn(nc, "json")
defer c.Close()
type person struct {
Name string
Address string
Age int
}
me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
c.Publish("hello", me)
}
开发者ID:MarkyV,项目名称:nats,代码行数:17,代码来源:example_test.go
示例15: ExampleEncodedConn_BindSendChan
// BindSendChan() allows binding of a Go channel to a nats
// subject for publish operations. The Encoder attached to the
// EncodedConn will be used for marshalling.
func ExampleEncodedConn_BindSendChan() {
nc, _ := nats.Connect(nats.DefaultURL)
c, _ := nats.NewEncodedConn(nc, "json")
defer c.Close()
type person struct {
Name string
Address string
Age int
}
ch := make(chan *person)
c.BindSendChan("hello", ch)
me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
ch <- me
}
开发者ID:hsinhoyeh,项目名称:nats,代码行数:20,代码来源:example_test.go
示例16: Listen
func (n *ntport) Listen(addr string) (transport.Listener, error) {
cAddr := nats.DefaultURL
if len(n.addrs) > 0 && strings.HasPrefix(n.addrs[0], "nats://") {
cAddr = n.addrs[0]
}
c, err := nats.Connect(cAddr)
if err != nil {
return nil, err
}
return &ntportListener{
addr: nats.NewInbox(),
conn: c,
exit: make(chan bool, 1),
}, nil
}
开发者ID:kgrvamsi,项目名称:go-micro,代码行数:18,代码来源:nats.go
示例17: ExampleSubscription_AutoUnsubscribe
func ExampleSubscription_AutoUnsubscribe() {
nc, _ := nats.Connect(nats.DefaultURL)
defer nc.Close()
received, wanted, total := 0, 10, 100
sub, _ := nc.Subscribe("foo", func(_ *nats.Msg) {
received += 1
})
sub.AutoUnsubscribe(wanted)
for i := 0; i < total; i++ {
nc.Publish("foo", []byte("Hello"))
}
nc.Flush()
fmt.Printf("Received = %d", received)
}
开发者ID:MarkyV,项目名称:nats,代码行数:18,代码来源:example_test.go
示例18: NewPeer
// NewPeer creates and returns a new Peer for communicating with NATS.
func NewPeer(host string) (*Peer, error) {
conn, err := nats.Connect(fmt.Sprintf("nats://%s", host))
if err != nil {
return nil, err
}
// We want to be alerted if we get disconnected, this will be due to Slow
// Consumer.
conn.Opts.AllowReconnect = false
return &Peer{
conn: conn,
messages: make(chan []byte, 10000),
send: make(chan []byte),
errors: make(chan error, 1),
done: make(chan bool),
}, nil
}
开发者ID:bunin,项目名称:Flotilla,代码行数:19,代码来源:nats.go
示例19: init
func init() {
cfgPtr := flag.String("config", "config/director.json", "Path to the config file")
flag.Parse()
f, err := os.Open(*cfgPtr)
if err != nil {
fmt.Println("Cannot open the config file:", err)
os.Exit(1)
}
err = json.NewDecoder(f).Decode(&cfg)
if err != nil {
fmt.Println("Cannot decode the config file:", err)
os.Exit(1)
}
db_dsn := cfg.DB["Username"] + ":" + cfg.DB["Password"] + "@tcp(" + cfg.DB["Host"] + ":" + cfg.DB["Port"] + ")/" + cfg.DB["Database"]
db, err = sql.Open("mysql", db_dsn)
if err != nil {
fmt.Println("Cannot connect to the Policy database:", err)
os.Exit(1)
}
if cfg.Duration != 0 {
duration = cfg.Duration
}
natsc, err = nats.Connect(cfg.Nats)
if err != nil {
fmt.Println("Cannot connect to the gnatsd:", err)
os.Exit(1)
}
if cfg.Log != "" {
logf, err := os.Open(cfg.Log)
if err != nil {
fmt.Println("Cannot open the log file:", err)
os.Exit(1)
}
log.SetOutput(logf)
}
}
开发者ID:cloudfoundry-community,项目名称:cfscaler,代码行数:42,代码来源:main.go
示例20: Connect
// Connect to rabbitmq
func (b *Broker) Connect(uri string) error {
b.natsURL = uri
log.Debugf("Dialing [%s]", uri)
// dial the server
conn, err := nats.Connect(b.natsURL)
if err != nil {
return err
}
// create the encoded connection
b.connection, err = nats.NewEncodedConn(conn, "json")
if err != nil {
return err
}
log.Debug("Connected to gnatsd")
return nil
}
开发者ID:adfin,项目名称:gocelery,代码行数:21,代码来源:nats.go
注:本文中的github.com/apcera/nats.Connect函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论