Skip to content

Commit

Permalink
add more code for rabbitmq #3
Browse files Browse the repository at this point in the history
  • Loading branch information
thangchung committed Oct 27, 2022
1 parent 8ed9baa commit e8ea4e6
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 75 deletions.
14 changes: 14 additions & 0 deletions cmd/barista/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
app:
name: 'counter-service'
version: '1.0.0'

http:
host: '0.0.0.0'
port: 5002

rabbit_mq:
url: amqp://guest:guest@172.28.191.206:5672/

logger:
log_level: 'debug'
rollbar_env: 'counter-service'
47 changes: 47 additions & 0 deletions cmd/barista/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package config

import (
"fmt"
"log"
"os"

"github.com/ilyakaznacheev/cleanenv"
configs "github.com/thangchung/go-coffeeshop/pkg/config"
)

type (
Config struct {
configs.App `yaml:"app"`
configs.HTTP `yaml:"http"`
configs.Log `yaml:"logger"`
RabbitMQ `yaml:"rabbit_mq"`
}

RabbitMQ struct {
URL string `env-required:"true" yaml:"url" env:"RABBITMQ_URL"`
}
)

func NewConfig() (*Config, error) {
cfg := &Config{}

dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

// debug
fmt.Println(dir)

err = cleanenv.ReadConfig(dir+"/config.yml", cfg)
if err != nil {
return nil, fmt.Errorf("config error: %w", err)
}

err = cleanenv.ReadEnv(cfg)
if err != nil {
return nil, err
}

return cfg, nil
}
2 changes: 1 addition & 1 deletion cmd/barista/event/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (c *Consumer) Listen(topics []string) error {
go func() {
switch payload.Name {
case "drink_made":
fmt.Println("Got it")
fmt.Println(payload)
default:
fmt.Println("default")
}
Expand Down
21 changes: 14 additions & 7 deletions cmd/barista/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ import (
"os"
"time"

"github.com/golang/glog"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/thangchung/go-coffeeshop/cmd/barista/config"
"github.com/thangchung/go-coffeeshop/cmd/barista/event"
)

const (
RetryTimes = 5
PowOf = 2
RetryTimes = 5
BackOffSeconds = 2
)

func main() {
rabbitConn, err := connect()
cfg, err := config.NewConfig()
if err != nil {
glog.Fatal(err)
}

rabbitConn, err := connect(cfg)
if err != nil {
log.Println(err)
os.Exit(1)
Expand All @@ -38,12 +45,12 @@ func main() {
}
}

func connect() (*amqp.Connection, error) {
func connect(cfg *config.Config) (*amqp.Connection, error) {
var (
counts int64
backOff = 1 * time.Second
connection *amqp.Connection
rabbitURL = "amqp://guest:guest@172.28.177.17:5672/"
rabbitURL = cfg.RabbitMQ.URL
)

for {
Expand All @@ -64,8 +71,8 @@ func connect() (*amqp.Connection, error) {
return nil, err
}

fmt.Printf("Backing off for %d seconds...\n", int(math.Pow(float64(counts), PowOf)))
backOff = time.Duration(math.Pow(float64(counts), PowOf)) * time.Second
fmt.Printf("Backing off for %d seconds...\n", int(math.Pow(float64(counts), BackOffSeconds)))
backOff = time.Duration(math.Pow(float64(counts), BackOffSeconds)) * time.Second
time.Sleep(backOff)

continue
Expand Down
3 changes: 3 additions & 0 deletions cmd/counter/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ http:
host: '0.0.0.0'
port: 5002

rabbit_mq:
url: amqp://guest:guest@172.28.191.206:5672/

logger:
log_level: 'debug'
rollbar_env: 'counter-service'
5 changes: 5 additions & 0 deletions cmd/counter/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ type (
configs.App `yaml:"app"`
configs.HTTP `yaml:"http"`
configs.Log `yaml:"logger"`
RabbitMQ `yaml:"rabbit_mq"`
}

RabbitMQ struct {
URL string `env-required:"true" yaml:"url" env:"RABBITMQ_URL"`
}
)

Expand Down
4 changes: 0 additions & 4 deletions cmd/counter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"context"
"fmt"
"os"
"reflect"

"github.com/golang/glog"
"github.com/thangchung/go-coffeeshop/cmd/counter/config"
Expand All @@ -18,8 +16,6 @@ func main() {
glog.Fatal(err)
}

fmt.Println(reflect.TypeOf(struct{}{}))

mylog := mylogger.New(cfg.Level)

a := app.New(mylog, cfg)
Expand Down
99 changes: 58 additions & 41 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,66 @@ services:
networks:
- coffeeshop-network

# proxy:
# build:
# context: .
# dockerfile: ./docker/Dockerfile-proxy
# image: go-coffeeshop-proxy
# environment:
# APP_NAME: 'proxy-service in docker'
# GRPC_PRODUCT_HOST: 'product'
# GRPC_PRODUCT_PORT: 5001
# ports:
# - 5000:5000
# depends_on:
# - product
# - counter
# networks:
# - coffeeshop-network
proxy:
build:
context: .
dockerfile: ./docker/Dockerfile-proxy
image: go-coffeeshop-proxy
environment:
APP_NAME: 'proxy-service in docker'
GRPC_PRODUCT_HOST: 'product'
GRPC_PRODUCT_PORT: 5001
GRPC_COUNTER_HOST: 'counter'
GRPC_COUNTER_PORT: 5002
ports:
- 5000:5000
depends_on:
- product
- counter
networks:
- coffeeshop-network

product:
build:
context: .
dockerfile: ./docker/Dockerfile-product
image: go-coffeeshop-product
environment:
APP_NAME: 'product-service in docker'
ports:
- 5001:5001
networks:
- coffeeshop-network

# product:
# build:
# context: .
# dockerfile: ./docker/Dockerfile-product
# image: go-coffeeshop-product
# environment:
# APP_NAME: 'product-service in docker'
# ports:
# - 5001:5001
# networks:
# - coffeeshop-network
counter:
build:
context: .
dockerfile: ./docker/Dockerfile-counter
image: go-coffeeshop-counter
environment:
APP_NAME: 'counter-service in docker'
RABBITMQ_URL: amqp://guest:guest@rabbitmq:5672/
ports:
- 5002:5002
depends_on:
- postgres
- rabbitmq
networks:
- coffeeshop-network

# counter:
# build:
# context: .
# dockerfile: ./docker/Dockerfile-counter
# image: go-coffeeshop-counter
# environment:
# APP_NAME: 'counter-service in docker'
# ports:
# - 5002:5002
# depends_on:
# - postgres
# - rabbitmq
# networks:
# - coffeeshop-network
barista:
build:
context: .
dockerfile: ./docker/Dockerfile-barista
image: go-coffeeshop-barista
environment:
APP_NAME: 'barista-service in docker'
RABBITMQ_URL: amqp://guest:guest@rabbitmq:5672/
depends_on:
- postgres
- rabbitmq
networks:
- coffeeshop-network

networks:
coffeeshop-network:
24 changes: 24 additions & 0 deletions docker/Dockerfile-barista
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Step 1: Modules caching
FROM golang:1.19.2-alpine3.16 as modules
COPY go.mod go.sum /modules/
WORKDIR /modules
RUN go mod download

# Step 2: Builder
FROM golang:1.19.2-alpine3.16 as builder
COPY --from=modules /go/pkg /go/pkg
COPY . /app
WORKDIR /app
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -tags migrate -o /bin/app ./cmd/barista

# Step 3: Final
FROM scratch

# COPY --from=builder /app/config /config
# COPY --from=builder /app/migrations /migrations

# GOPATH for scratch images is /
COPY --from=builder /app/cmd/barista/config.yml /
COPY --from=builder /bin/app /app
CMD ["/app"]
26 changes: 26 additions & 0 deletions docker/Dockerfile-counter
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Step 1: Modules caching
FROM golang:1.19.2-alpine3.16 as modules
COPY go.mod go.sum /modules/
WORKDIR /modules
RUN go mod download

# Step 2: Builder
FROM golang:1.19.2-alpine3.16 as builder
COPY --from=modules /go/pkg /go/pkg
COPY . /app
WORKDIR /app
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -tags migrate -o /bin/app ./cmd/counter

# Step 3: Final
FROM scratch

EXPOSE 5002

# COPY --from=builder /app/config /config
# COPY --from=builder /app/migrations /migrations

# GOPATH for scratch images is /
COPY --from=builder /app/cmd/counter/config.yml /
COPY --from=builder /bin/app /app
CMD ["/app"]
Loading

0 comments on commit e8ea4e6

Please sign in to comment.