nats-by-example icon indicating copy to clipboard operation
nats-by-example copied to clipboard

Create stream in docker compose config file

Open kowiste opened this issue 2 years ago • 2 comments

Really nice project you have here.

I have a problem trying to create a stream in the configuration file, it would be nice if you could add a example for this.

Here is what I'm doing now, with the container up I connect to nats and cant see mistream , but after the addstream line test stream is display.

How I can configure streams in the config file?

docker-compose.yml

version: '3.5'
services:
  nats:
    image: nats:2.9.15-alpine3.17
    container_name: nats
    restart: unless-stopped
    command: --js
    ports:
      - "4222:4222"
      - "8222:8222"
    networks:
      - web
    volumes:
      - ./conf/nats-server.conf:/etc/nats/nats-server.conf
networks:
  web:
    external: true

nats-server.conf

port: 4222
jetstream: enabled
monitor_port: 8222

jetstream {
  store_dir: "/data/jetstream"
  store_type: "file"
  streams {
    name: "mistream"
    subjects: ["mistream.*"]
    retention {
      limit: 100        
      max_bytes: 1G    
    }
    max_msg_size: 500KB
  }
}

cluster {
  name: "my_cluster"

  port: 6222
  authorization {
    user: ruser
    password: T0pS3cr3t
    timeout: 2
  }
  routes = []
}

main.go

func main() {
	nc, err := nats.Connect("nats://localhost:4222")
	if err != nil {
		fmt.Println("Connection error:", err)
		return
	}
	defer nc.Close()

	js, err := nc.JetStream()
	if err != nil {
		fmt.Println("Error Jetstream:", err)
		return
	}
	c := <-js.StreamNames()
	println(c)
	js.AddStream(&nats.StreamConfig{
		Name: "test",
	})
	c = <-js.StreamNames()
	println(c)
}

kowiste avatar Mar 25 '23 08:03 kowiste

Hi @Kowiste, NATS does not support defining streams in the configuration file. The options are programmatically through the client libraries (e.g. Go), the CLI, a Terraform provider, or nack (a k8s controller).

bruth avatar Oct 17 '23 13:10 bruth

I feel that is an oversight, because you're requiring the use of extra tooling when a simple configuration file can drive a single instance or cluster can have everything it needs in one file.

MrHamel avatar Sep 14 '24 06:09 MrHamel