├── .dockerignore ├── .editorconfig ├── .gitattributes ├── .gitignore ├── Dockerfile-consumer ├── Dockerfile-sender ├── LICENSE ├── Makefile ├── README.md ├── consumer └── main.go ├── docker-compose.yml ├── go.mod ├── go.sum └── sender └── main.go /.dockerignore: -------------------------------------------------------------------------------- 1 | # Files 2 | .dockerignore 3 | .editorconfig 4 | .gitignore 5 | .gitattributes 6 | Dockerfile-* 7 | Makefile 8 | LICENSE 9 | **/*.yml 10 | **/*.md 11 | **/*_test.go 12 | 13 | # Folders 14 | .git/ 15 | .github/ 16 | build/ 17 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{go.mod,go.sum,*.go}] 11 | indent_style = tab 12 | indent_size = 4 13 | 14 | [{Makefile,Dockerfile,*.yml,*.yaml}] 15 | indent_style = tab 16 | indent_size = 2 17 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | go.sum merge=union 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # macOS 2 | **/.DS_Store 3 | 4 | # Dev build 5 | build/ 6 | tmp/ 7 | 8 | # Test 9 | *.out 10 | 11 | # Environment variables 12 | .env -------------------------------------------------------------------------------- /Dockerfile-consumer: -------------------------------------------------------------------------------- 1 | FROM golang:1.16-alpine AS builder 2 | 3 | # Move to working directory (/build). 4 | WORKDIR /build 5 | 6 | # Copy and download dependency using go mod. 7 | COPY go.mod go.sum ./ 8 | RUN go mod download 9 | 10 | # Copy the code into the container. 11 | COPY ./consumer/main.go . 12 | 13 | # Set necessary environment variables needed 14 | # for our image and build the consumer. 15 | ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64 16 | RUN go build -ldflags="-s -w" -o consumer . 17 | 18 | FROM scratch 19 | 20 | # Copy binary and config files from /build 21 | # to root folder of scratch container. 22 | COPY --from=builder ["/build/consumer", "/"] 23 | 24 | # Command to run when starting the container. 25 | ENTRYPOINT ["/consumer"] -------------------------------------------------------------------------------- /Dockerfile-sender: -------------------------------------------------------------------------------- 1 | FROM golang:1.16-alpine AS builder 2 | 3 | # Move to working directory (/build). 4 | WORKDIR /build 5 | 6 | # Copy and download dependency using go mod. 7 | COPY go.mod go.sum ./ 8 | RUN go mod download 9 | 10 | # Copy the code into the container. 11 | COPY ./sender/main.go . 12 | 13 | # Set necessary environment variables needed 14 | # for our image and build the sender. 15 | ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64 16 | RUN go build -ldflags="-s -w" -o sender . 17 | 18 | FROM scratch 19 | 20 | # Copy binary and config files from /build 21 | # to root folder of scratch container. 22 | COPY --from=builder ["/build/sender", "/"] 23 | 24 | # Command to run when starting the container. 25 | ENTRYPOINT ["/sender"] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Vic Shóstak (https://1wa.co) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: run stop 2 | 3 | run: 4 | docker-compose up -d --build 5 | 6 | stop: 7 | docker-compose down -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 📖 Tutorial: Working with RabbitMQ in Golang by examples 2 | 3 | This tutorial is more aimed at those who just want to understand how to working with a message broker in Go. 4 | 5 | 👉 The full article is published on **March 31, 2021**, on Dev.to: https://dev.to/koddr/working-with-rabbitmq-in-golang-by-examples-2dcn 6 | 7 | ## Quick start 8 | 9 | 1. Install [Docker](https://www.docker.com/get-started), Docker Compose and start their system services. 10 | 2. Run containers with the RabbitMQ, [Fiber](https://github.com/gofiber/fiber) and consumer by this command: 11 | 12 | ```bash 13 | make run 14 | ``` 15 | 16 | 3. Make HTTP request to the API endpoint: 17 | 18 | ```console 19 | curl \ 20 | --request GET \ 21 | --url 'http://localhost:3000/send?msg=test' 22 | ``` 23 | 24 | 4. Go to RabbitMQ awesome dashboard [localhost:5672](http://localhost:5672) and see `QueueService1` queue with sent messages: 25 | 26 | ![Screenshot](https://user-images.githubusercontent.com/11155743/113058619-0bec2480-91b7-11eb-9f0f-1102ea69f2fd.png) 27 | 28 | ## P.S. 29 | 30 | If you want more articles like this on this blog, then post a comment below and subscribe to me. Thanks! 😘 31 | 32 | And, of course, you can support me by donating at [LiberaPay](https://liberapay.com/koddr/donate). _Each donation will be used to write new articles and develop non-profit open-source projects for the community._ 33 | 34 | [![Support author at LiberaPay](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zq8442cqyjq2i1jdeay8.png)](https://liberapay.com/koddr/donate) 35 | 36 | ## ⚠️ License 37 | 38 | MIT © [Vic Shóstak](https://shostak.dev/) & [True web artisans](https://1wa.co/). 39 | -------------------------------------------------------------------------------- /consumer/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | 7 | "github.com/streadway/amqp" 8 | ) 9 | 10 | func main() { 11 | // Define RabbitMQ server URL. 12 | amqpServerURL := os.Getenv("AMQP_SERVER_URL") 13 | 14 | // Create a new RabbitMQ connection. 15 | connectRabbitMQ, err := amqp.Dial(amqpServerURL) 16 | if err != nil { 17 | panic(err) 18 | } 19 | defer connectRabbitMQ.Close() 20 | 21 | // Opening a channel to our RabbitMQ instance over 22 | // the connection we have already established. 23 | channelRabbitMQ, err := connectRabbitMQ.Channel() 24 | if err != nil { 25 | panic(err) 26 | } 27 | defer channelRabbitMQ.Close() 28 | 29 | // Subscribing to QueueService1 for getting messages. 30 | messages, err := channelRabbitMQ.Consume( 31 | "QueueService1", // queue name 32 | "", // consumer 33 | true, // auto-ack 34 | false, // exclusive 35 | false, // no local 36 | false, // no wait 37 | nil, // arguments 38 | ) 39 | if err != nil { 40 | log.Println(err) 41 | } 42 | 43 | // Build a welcome message. 44 | log.Println("Successfully connected to RabbitMQ") 45 | log.Println("Waiting for messages") 46 | 47 | // Make a channel to receive messages into infinite loop. 48 | forever := make(chan bool) 49 | 50 | go func() { 51 | for message := range messages { 52 | // For example, show received message in a console. 53 | log.Printf(" > Received message: %s\n", message.Body) 54 | } 55 | }() 56 | 57 | <-forever 58 | } 59 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.9" 2 | 3 | services: 4 | # Create service with RabbitMQ. 5 | message-broker: 6 | image: rabbitmq:3-management-alpine 7 | container_name: message-broker 8 | ports: 9 | - 5672:5672 # for sender and consumer connections 10 | - 15672:15672 # for serve RabbitMQ GUI 11 | volumes: 12 | - ${HOME}/dev-rabbitmq/data/:/var/lib/rabbitmq 13 | - ${HOME}/dev-rabbitmq/log/:/var/log/rabbitmq 14 | restart: always 15 | networks: 16 | - dev-network 17 | 18 | # Create service with Fiber sender. 19 | sender: 20 | container_name: sender 21 | ports: 22 | - 3000:3000 23 | build: 24 | context: . 25 | dockerfile: Dockerfile-sender 26 | environment: 27 | AMQP_SERVER_URL: amqp://guest:guest@message-broker:5672/ 28 | restart: always 29 | networks: 30 | - dev-network 31 | depends_on: 32 | - message-broker 33 | 34 | # Create service with message consumer. 35 | consumer: 36 | container_name: consumer 37 | build: 38 | context: . 39 | dockerfile: Dockerfile-consumer 40 | environment: 41 | AMQP_SERVER_URL: amqp://guest:guest@message-broker:5672/ 42 | restart: always 43 | networks: 44 | - dev-network 45 | depends_on: 46 | - sender 47 | - message-broker 48 | 49 | networks: 50 | # Create a new Docker network. 51 | dev-network: 52 | driver: bridge 53 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module tutorial-go-fiber-rabbitmq 2 | 3 | go 1.16 4 | 5 | require ( 6 | github.com/gofiber/fiber/v2 v2.52.5 7 | github.com/streadway/amqp v1.0.0 8 | ) 9 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= 2 | github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= 3 | github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo= 4 | github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= 5 | github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= 6 | github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 7 | github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM= 8 | github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= 9 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= 10 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 11 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 12 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= 13 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 14 | github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= 15 | github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 16 | github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= 17 | github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= 18 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 19 | github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo= 20 | github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= 21 | github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw= 22 | github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= 23 | github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 24 | github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= 25 | github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= 26 | github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= 27 | github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= 28 | github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 29 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 30 | golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 31 | golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= 32 | golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 33 | golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 34 | golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= 35 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 36 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 37 | golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= 38 | golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= 39 | golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= 40 | golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= 41 | golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= 42 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 43 | golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 44 | golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 45 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 46 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 47 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 48 | golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 49 | golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 50 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 51 | golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 52 | golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 53 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 54 | golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 55 | golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 56 | golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= 57 | golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 58 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 59 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 60 | golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= 61 | golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= 62 | golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= 63 | golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= 64 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 65 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 66 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 67 | golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 68 | golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= 69 | golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= 70 | golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= 71 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 72 | golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 73 | golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= 74 | golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= 75 | golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= 76 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 77 | -------------------------------------------------------------------------------- /sender/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | 7 | "github.com/gofiber/fiber/v2" 8 | "github.com/gofiber/fiber/v2/middleware/logger" 9 | "github.com/streadway/amqp" 10 | ) 11 | 12 | func main() { 13 | // Define RabbitMQ server URL. 14 | amqpServerURL := os.Getenv("AMQP_SERVER_URL") 15 | 16 | // Create a new RabbitMQ connection. 17 | connectRabbitMQ, err := amqp.Dial(amqpServerURL) 18 | if err != nil { 19 | panic(err) 20 | } 21 | defer connectRabbitMQ.Close() 22 | 23 | // Let's start by opening a channel to our RabbitMQ 24 | // instance over the connection we have already 25 | // established. 26 | channelRabbitMQ, err := connectRabbitMQ.Channel() 27 | if err != nil { 28 | panic(err) 29 | } 30 | defer channelRabbitMQ.Close() 31 | 32 | // With the instance and declare Queues that we can 33 | // publish and subscribe to. 34 | _, err = channelRabbitMQ.QueueDeclare( 35 | "QueueService1", // queue name 36 | true, // durable 37 | false, // auto delete 38 | false, // exclusive 39 | false, // no wait 40 | nil, // arguments 41 | ) 42 | if err != nil { 43 | panic(err) 44 | } 45 | 46 | // Create a new Fiber instance. 47 | app := fiber.New() 48 | 49 | // Add middleware. 50 | app.Use( 51 | logger.New(), // add simple logger 52 | ) 53 | 54 | // Add route for send message to Service 1. 55 | app.Get("/send", func(c *fiber.Ctx) error { 56 | // Create a message to publish. 57 | message := amqp.Publishing{ 58 | ContentType: "text/plain", 59 | Body: []byte(c.Query("msg")), 60 | } 61 | 62 | // Attempt to publish a message to the queue. 63 | if err := channelRabbitMQ.Publish( 64 | "", // exchange 65 | "QueueService1", // queue name 66 | false, // mandatory 67 | false, // immediate 68 | message, // message to publish 69 | ); err != nil { 70 | return err 71 | } 72 | 73 | return nil 74 | }) 75 | 76 | // Start Fiber API server. 77 | log.Fatal(app.Listen(":3000")) 78 | } 79 | --------------------------------------------------------------------------------