Rumah > pembangunan bahagian belakang > Golang > Seni bina modular perkhidmatan mikro Golang dengan ruang kerja go

Seni bina modular perkhidmatan mikro Golang dengan ruang kerja go

DDD
Lepaskan: 2024-10-01 18:08:02
asal
730 orang telah melayarinya

Scalable codebase infrastructure

Golang shines in backend development, concurrent operations and is a perfect suite for building scalable and performant backend applications. Due to the lack of posts revolving around It's microservice architecture with go workspaces which is an incredible tool for sharing modular code through different services, I decided to share my implementation.

Project setup

Golang microservice modular architecture with go workspace

mkdir docker

touch docker/Dockerfile.authentication

touch docker/Dockerfile.users

mkdir -p services/authentication

mkdir -p services/users

mkdir -p shared/utils

touch docker-compose.yml
Salin selepas log masuk

Following shell commands will produce the following folder tree structure

Golang microservice modular architecture with go workspace

Setting up go workspace

At the root of the project, create a go workspace simply by using a simple command go work init this will produce the go.work file

Next, initialize all the different go projects that will be able to hold dependencies, and run codebases.

cd services/authentication && go mod init github.com/LegationPro/ms/services/authentication

cd ../.. && cd services/users && go mod init github.com/LegationPro/ms/services/users

cd ../.. && cd shared && go mod init github.com/LegationPro/ms/shared
Salin selepas log masuk

After running the following commands, your project should look like this

Golang microservice modular architecture with go workspace

Next, we will populate the go workspace and tell it what is part of the workspace by running the following command

go work use ./services/authentication ./services/users ./shared

This will populate the go.work file

go 1.23.1

use (
    ./services/authentication
    ./services/users
    ./shared
)
Salin selepas log masuk

Setting it up with Docker

Let's go over the docker-compose.yml first.

Your docker-compose.yml file should look like this

services:
  authentication:
    build:
      context: .
      dockerfile: docker/Dockerfile.authentication
    volumes:
      - ./services/authentication:/app/authentication
      - ./shared:/app/shared
    ports:
      - "8081:8081"

  users:
    build:
      context: .
      dockerfile: docker/Dockerfile.users
    volumes:
      - ./services/users:/app/users
      - ./shared:/app/shared
    ports:
      - "8082:8082"
Salin selepas log masuk

We tell docker-compose to use the following services which are authentication and users.

We give is the root context, so we can access the files and folders at the root level.

Supply the dockerfile location.

Define the given volume for the container and at the end expose a port for the container to run on.

Setting up Dockerfiles

Setting up the Dockerfile is pretty simple and straight forward.

We pull the latest golang alpine image, assign a working directory, move some of the code around, adjust it to work with the go workspace structure and simply run it.

docker/Dockerfile.authentication

# Pull golang image
FROM golang:1.23-alpine

# Switch to /app as the working directory
WORKDIR /app

# Copy the authentication codebase over to our container
COPY ./services/authentication /app/authentication/

# Copy the shared codebase and libraries that are shared across our apps inside the container
COPY ./shared /app/shared

# Initialize go workspace inside of our container
RUN go work init

# Assign different codebases to go workspaces
RUN go work use ./authentication ./shared

# Simply run our service with this simple command
CMD ["go", "run", "./authentication"]
Salin selepas log masuk

Dockerfile.users

# Pull golang image
FROM golang:1.23-alpine

# Switch to /app as the working directory
WORKDIR /app

# Copy the authentication codebase over to our container
COPY ./services/users /app/users/

# Copy the shared codebase and libraries that are shared across our apps inside the container
COPY ./shared /app/shared

# Initialize go workspace inside of our container
RUN go work init

# Assign different codebases to go workspaces
RUN go work use ./users ./shared

# Simply run our service with this simple command
CMD ["go", "run", "./users"]
Salin selepas log masuk

Writing our service code

services/authentication/main.go

package main

import (
    "fmt"

    "github.com/LegationPro/ms/shared/utils"
)

func main() {
    fmt.Println(utils.SomeAuthFunc())
}
Salin selepas log masuk

services/users/main.go

package main

import (
    "fmt"

    "github.com/LegationPro/ms/shared/utils"
)

func main() {
    fmt.Println(utils.SomeUserFunc())
}
Salin selepas log masuk

shared/utils/utils.go

package utils

func SomeAuthFunc() string {
    return "Some auth func"
}

func SomeUserFunc() string {
    return "Some user func"
}
Salin selepas log masuk

The structure should look like this now

Golang microservice modular architecture with go workspace

Run the application inside a docker container

docker-compose up --build

To make sure everything works the output should be the following:

Golang microservice modular architecture with go workspace

That's it, you have a fully functional go workspace modular microservice architecture setup! ??

Source code : https://github.com/LegationPro/go-microservice-modular-docker-setup

Thank you

Thank you for reading my blog post, I hope this helps ❤️!

Atas ialah kandungan terperinci Seni bina modular perkhidmatan mikro Golang dengan ruang kerja go. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan