以下是在Debian上搭建Go微服务架构的步骤:
安装Go环境
sudo apt update && sudo apt install golang-go
。/usr/local
,配置GOROOT
和GOPATH
环境变量。创建微服务项目
mkdir my-microservice && cd my-microservice && go mod init
。package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "pong"})
})
r.Run(":8080")
}
容器化部署
FROM golang:latest
WORKDIR /app
COPY . .
RUN go build -o my-service
EXPOSE 8080
CMD ["./my-service"]
```。
docker build -t my-service .
docker run -p 8080:8080 my-service
。服务治理组件
高级架构设计(可选)
参考来源: