chore: import upstream snapshot with attribution
govulncheck / govulncheck (push) Has been cancelled
Lint / golangci-lint (push) Has been cancelled
Run Tests / Unit Tests (push) Has been cancelled
Run Tests / Etcd Integration Tests (push) Has been cancelled
Harness (E2E) / Harnesses (mock LLM) (push) Has been cancelled
Harness (E2E) / Provider harnesses (live LLM conformance) (push) Has been cancelled
govulncheck / govulncheck (push) Has been cancelled
Lint / golangci-lint (push) Has been cancelled
Run Tests / Unit Tests (push) Has been cancelled
Run Tests / Etcd Integration Tests (push) Has been cancelled
Harness (E2E) / Harnesses (mock LLM) (push) Has been cancelled
Harness (E2E) / Provider harnesses (live LLM conformance) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
---
|
||||
layout: default
|
||||
---
|
||||
|
||||
# Pub/Sub with NATS Broker
|
||||
|
||||
Use the NATS broker for pub/sub.
|
||||
|
||||
## In code
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"go-micro.dev/v6"
|
||||
"go-micro.dev/v6/broker"
|
||||
bnats "go-micro.dev/v6/broker/nats"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := bnats.NewNatsBroker()
|
||||
svc := micro.NewService("nats-pubsub", micro.Broker(b))
|
||||
svc.Init()
|
||||
|
||||
// subscribe
|
||||
_, _ = broker.Subscribe("events", func(e broker.Event) error {
|
||||
log.Printf("received: %s", string(e.Message().Body))
|
||||
return nil
|
||||
})
|
||||
|
||||
// publish
|
||||
_ = broker.Publish("events", &broker.Message{Body: []byte("hello")})
|
||||
|
||||
svc.Run()
|
||||
}
|
||||
```
|
||||
|
||||
## Via environment
|
||||
|
||||
Run your service with env vars set:
|
||||
|
||||
```bash
|
||||
MICRO_BROKER=nats MICRO_BROKER_ADDRESS=nats://127.0.0.1:4222 go run main.go
|
||||
```
|
||||
Reference in New Issue
Block a user