diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 8dc2837..8215ab6 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -105,7 +105,7 @@ jobs: - name: Docker build run: | docker buildx build --load \ - -f product/Dockerfile \ + -f services/product/Dockerfile \ -t ${{ vars.REGISTRY }}/$NAME:latest \ . @@ -130,7 +130,7 @@ jobs: - name: Docker build run: | docker buildx build --load \ - -f admin/Dockerfile \ + -f services/admin/Dockerfile \ -t ${{ vars.REGISTRY }}/$NAME:latest \ . @@ -155,7 +155,7 @@ jobs: - name: Docker build run: | docker buildx build --load \ - -f user/Dockerfile \ + -f services/user/Dockerfile \ -t ${{ vars.REGISTRY }}/$NAME:latest \ . diff --git a/README.md b/README.md index f42103e..98e7505 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,6 @@ protoc -I. -I../pkg/third_party --descriptor_set_out=../bff/etc/order.pb --inclu - 增删 **RPC** - 改 **google.api.http** 路由 - - ## 如何加一个新服务 以加 `order` 为例(对标现有 `product`)。 diff --git a/bff/Dockerfile b/bff/Dockerfile index af97aab..ba251d3 100644 --- a/bff/Dockerfile +++ b/bff/Dockerfile @@ -1,5 +1,3 @@ -# syntax=docker/dockerfile:1 - FROM golang:1.26.5-alpine AS builder WORKDIR /src @@ -10,16 +8,18 @@ ENV GOPROXY=https://goproxy.cn,direct \ GOARCH=amd64 COPY pkg ./pkg -COPY bff/go.mod bff/go.sum ./bff/ +COPY go.mod go.sum ./ -WORKDIR /src/bff RUN --mount=type=cache,target=/go/pkg/mod \ go mod download -COPY bff/ ./ +COPY bff/ ./bff/ + +WORKDIR /src/bff + RUN --mount=type=cache,target=/go/pkg/mod \ --mount=type=cache,target=/root/.cache/go-build \ - go build -ldflags="-s -w" -o /out/bff . + go build -ldflags="-s -w" -o /out/main ./ FROM alpine:3.22 @@ -31,9 +31,14 @@ RUN apk add --no-cache tzdata \ && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo "Asia/Shanghai" > /etc/timezone -COPY --from=builder /out/bff . -COPY bff/etc/*.pb ./etc/ +COPY --from=builder /out/main . +COPY rpc/user/*.pb \ + rpc/order/*.pb \ + rpc/product/*.pb \ + rpc/admin/*.pb \ + rpc/lonelog/*.pb \ + ./etc/ EXPOSE 10000 -CMD ["./bff", "-f", "etc/bff.yaml"] +CMD ["./main", "-f", "etc/bff.yaml"] diff --git a/services/admin/.gitignore b/services/admin/.gitignore index f189f79..fe82c05 100644 --- a/services/admin/.gitignore +++ b/services/admin/.gitignore @@ -1,2 +1,3 @@ run.toml -tmp \ No newline at end of file +tmp +etc/admin.yaml \ No newline at end of file diff --git a/services/admin/Dockerfile b/services/admin/Dockerfile new file mode 100644 index 0000000..e598cab --- /dev/null +++ b/services/admin/Dockerfile @@ -0,0 +1,37 @@ +FROM golang:1.26.5-alpine AS builder + +WORKDIR /src + +ENV GOPROXY=https://goproxy.cn,direct \ + CGO_ENABLED=0 \ + GOOS=linux \ + GOARCH=amd64 + +COPY pkg ./pkg +COPY go.mod go.sum ./ + +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download + +COPY rpc/admin/ ./rpc/admin/ +COPY services/admin/ ./services/admin/ + +WORKDIR /src/services/admin + +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/go-build \ + go build -ldflags="-s -w" -o /out/main ./ + +FROM alpine:3.22 + +WORKDIR /app + +ENV TZ=Asia/Shanghai + +RUN apk add --no-cache tzdata \ + && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone + +COPY --from=builder /out/main . + +CMD ["./main", "-f", "etc/admin.yaml"] diff --git a/services/admin/etc/admin.yaml b/services/admin/etc/admin.yaml index 7a4a8d2..2152980 100644 --- a/services/admin/etc/admin.yaml +++ b/services/admin/etc/admin.yaml @@ -1,7 +1,7 @@ Nacos: Hosts: - - host.docker.internal:8848 + - rnacos:8848 NamespaceId: test Group: LONE_SERVICES RegisterIP: admin - ConfigID: develop-admin \ No newline at end of file + ConfigID: admin \ No newline at end of file diff --git a/services/lonelog/.gitignore b/services/lonelog/.gitignore index f189f79..925c4d2 100644 --- a/services/lonelog/.gitignore +++ b/services/lonelog/.gitignore @@ -1,2 +1,3 @@ run.toml -tmp \ No newline at end of file +tmp +etc/lonelog.yaml \ No newline at end of file diff --git a/services/lonelog/Dockerfile b/services/lonelog/Dockerfile new file mode 100644 index 0000000..33f64ce --- /dev/null +++ b/services/lonelog/Dockerfile @@ -0,0 +1,37 @@ +FROM golang:1.26.5-alpine AS builder + +WORKDIR /src + +ENV GOPROXY=https://goproxy.cn,direct \ + CGO_ENABLED=0 \ + GOOS=linux \ + GOARCH=amd64 + +COPY pkg ./pkg +COPY go.mod go.sum ./ + +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download + +COPY rpc/lonelog/ ./rpc/lonelog/ +COPY services/lonelog/ ./services/lonelog/ + +WORKDIR /src/services/lonelog + +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/go-build \ + go build -ldflags="-s -w" -o /out/main ./ + +FROM alpine:3.22 + +WORKDIR /app + +ENV TZ=Asia/Shanghai + +RUN apk add --no-cache tzdata \ + && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone + +COPY --from=builder /out/main . + +CMD ["./main", "-f", "etc/lonelog.yaml"] diff --git a/services/lonelog/etc/lonelog.yaml b/services/lonelog/etc/lonelog.yaml index 1ab306a..ae3b4f1 100644 --- a/services/lonelog/etc/lonelog.yaml +++ b/services/lonelog/etc/lonelog.yaml @@ -1,6 +1,6 @@ Nacos: Hosts: - - host.docker.internal:8848 + - rnacos:8848 NamespaceId: test Group: LONE_SERVICES RegisterIP: log diff --git a/services/order/.gitignore b/services/order/.gitignore new file mode 100644 index 0000000..aa5820a --- /dev/null +++ b/services/order/.gitignore @@ -0,0 +1,2 @@ +run.toml +etc/order.yaml \ No newline at end of file diff --git a/services/order/Dockerfile b/services/order/Dockerfile new file mode 100644 index 0000000..b5d71ce --- /dev/null +++ b/services/order/Dockerfile @@ -0,0 +1,37 @@ +FROM golang:1.26.5-alpine AS builder + +WORKDIR /src + +ENV GOPROXY=https://goproxy.cn,direct \ + CGO_ENABLED=0 \ + GOOS=linux \ + GOARCH=amd64 + +COPY pkg ./pkg +COPY go.mod go.sum ./ + +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download + +COPY rpc/order/ ./rpc/order/ +COPY services/order/ ./services/order/ + +WORKDIR /src/services/order + +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/go-build \ + go build -ldflags="-s -w" -o /out/main ./ + +FROM alpine:3.22 + +WORKDIR /app + +ENV TZ=Asia/Shanghai + +RUN apk add --no-cache tzdata \ + && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone + +COPY --from=builder /out/main . + +CMD ["./main", "-f", "etc/order.yaml"] diff --git a/services/order/etc/order.yaml b/services/order/etc/order.yaml index 143039f..2e7b225 100644 --- a/services/order/etc/order.yaml +++ b/services/order/etc/order.yaml @@ -1,6 +1,6 @@ Nacos: Hosts: - - host.docker.internal:8848 + - rnacos:8848 NamespaceId: test Group: LONE_SERVICES RegisterIP: order diff --git a/services/product/.gitignore b/services/product/.gitignore index 1c8f68c..13221b4 100644 --- a/services/product/.gitignore +++ b/services/product/.gitignore @@ -1 +1,2 @@ -etc/product.yaml \ No newline at end of file +etc/product.yaml +run.toml \ No newline at end of file diff --git a/services/product/Dockerfile b/services/product/Dockerfile index af79d54..9a59160 100644 --- a/services/product/Dockerfile +++ b/services/product/Dockerfile @@ -1,5 +1,3 @@ -# syntax=docker/dockerfile:1 - FROM golang:1.26.5-alpine AS builder WORKDIR /src @@ -10,16 +8,19 @@ ENV GOPROXY=https://goproxy.cn,direct \ GOARCH=amd64 COPY pkg ./pkg -COPY product/go.mod product/go.sum ./product/ +COPY go.mod go.sum ./ -WORKDIR /src/product RUN --mount=type=cache,target=/go/pkg/mod \ go mod download -COPY product/ ./ +COPY rpc/product/ ./rpc/product/ +COPY services/product/ ./services/product/ + +WORKDIR /src/services/product + RUN --mount=type=cache,target=/go/pkg/mod \ --mount=type=cache,target=/root/.cache/go-build \ - go build -ldflags="-s -w" -o /out/product . + go build -ldflags="-s -w" -o /out/main ./ FROM alpine:3.22 @@ -31,8 +32,8 @@ RUN apk add --no-cache tzdata \ && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo "Asia/Shanghai" > /etc/timezone -COPY --from=builder /out/product . +COPY --from=builder /out/main . EXPOSE 10100 -CMD ["./product", "-f", "etc/product.yaml"] +CMD ["./main", "-f", "etc/product.yaml"] diff --git a/services/product/internal/config/config.go b/services/product/internal/config/config.go index 3de9421..be26101 100644 --- a/services/product/internal/config/config.go +++ b/services/product/internal/config/config.go @@ -3,11 +3,8 @@ package config import "github.com/zeromicro/go-zero/zrpc" type Config struct { - zrpc.RpcServerConf - Nacos NacosConf - Mysql MysqlConf - BizRedis RedisConf - AppLog LogConf + Nacos NacosConf + zrpc.RpcClientConf } type NacosConf struct { @@ -15,37 +12,5 @@ type NacosConf struct { NamespaceId string `json:",optional"` Group string `json:",optional"` RegisterIP string `json:",optional"` -} - -type MysqlConf struct { - Host string - Port int - User string - Password string - Database string - Charset string `json:",default=utf8mb4"` - Prefix string `json:",optional"` - - ReadHost string `json:",optional"` - ReadPort int `json:",optional"` - ReadUser string `json:",optional"` - ReadPassword string `json:",optional"` - ReadDatabase string `json:",optional"` -} - -type RedisConf struct { - Host string - Port int `json:",default=6379"` - Password string `json:",optional"` - DB int `json:",optional"` -} - -type LogConf struct { - Path string `json:",default=logs"` - InfoFile string `json:",default=info.log"` - ErrorFile string `json:",default=error.log"` - FatalFile string `json:",default=fatal.log"` - MaxSize int `json:",default=100"` - MaxBackups int `json:",default=10"` - MaxAge int `json:",default=30"` + ConfigID string `json:",optional"` } diff --git a/services/product/internal/svc/servicecontext.go b/services/product/internal/svc/servicecontext.go index 7bb1f7d..66b0bba 100644 --- a/services/product/internal/svc/servicecontext.go +++ b/services/product/internal/svc/servicecontext.go @@ -1,6 +1,7 @@ package svc import ( + "lone-services/pkg/utils" "lone-services/services/product/internal/config" "gorm.io/gorm" @@ -16,6 +17,6 @@ func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext { return &ServiceContext{ Config: c, DB: db, - Prefix: c.Mysql.Prefix, + Prefix: utils.GetConfigString("mysql.prefix"), } } diff --git a/services/product/product.go b/services/product/product.go index 0734418..4f01746 100644 --- a/services/product/product.go +++ b/services/product/product.go @@ -2,12 +2,11 @@ package main import ( "flag" - "fmt" "lone-services/pkg/discovery" - "lone-services/pkg/log" "lone-services/pkg/modelbase" "lone-services/pkg/mysql" "lone-services/pkg/redis" + "lone-services/pkg/utils" "lone-services/pkg/validate" product "lone-services/rpc/product/pb" "lone-services/services/product/internal/config" @@ -28,24 +27,35 @@ import ( var configFile = flag.String("f", "etc/product.yaml", "the config file") func main() { - flag.Parse() - var c config.Config conf.MustLoad(*configFile, &c) - if err := log.Init(log.Config{ - Path: c.AppLog.Path, - InfoFile: c.AppLog.InfoFile, - ErrorFile: c.AppLog.ErrorFile, - FatalFile: c.AppLog.FatalFile, - MaxSize: c.AppLog.MaxSize, - MaxBackups: c.AppLog.MaxBackups, - MaxAge: c.AppLog.MaxAge, - }); err != nil { - fmt.Fprintf(os.Stderr, "log init: %v\n", err) - os.Exit(1) + nacosParam := utils.NacosConfig{ + Hosts: c.Nacos.Hosts, + NamespaceId: c.Nacos.NamespaceId, + Group: c.Nacos.Group, + ConfigID: c.Nacos.ConfigID, } + utils.InitConfig(nacosParam) + + logConf := logx.LogConf{ + ServiceName: utils.GetConfigString("log.serviceName"), + Mode: utils.GetConfigString("log.mode"), + Encoding: utils.GetConfigString("log.encoding"), + Level: utils.GetConfigString("log.level"), + Path: utils.GetConfigString("log.path"), + KeepDays: utils.GetConfigInt("log.keepDays"), + MaxSize: utils.GetConfigInt("log.maxSize"), + MaxBackups: utils.GetConfigInt("log.maxBackups"), + Compress: utils.GetConfigBool("log.compress"), + } + logx.SetUp(logConf) + logx.AddWriter(logx.NewWriter(os.Stdout)) + + listenOn := utils.GetConfigString("base.listenOn") + mode := utils.GetConfigString("base.mode") + serviceName := utils.GetConfigString("base.name") if err := discovery.Init(discovery.Config{ Hosts: c.Nacos.Hosts, @@ -53,22 +63,25 @@ func main() { Group: c.Nacos.Group, }); err != nil { logx.Errorf("nacos init: %v", err) + os.Exit(1) } - registerIP := c.Nacos.RegisterIP - port, err := listenPort(c.ListenOn) + port, err := listenPort(listenOn) if err != nil { logx.Errorf("parse ListenOn: %v", err) + os.Exit(1) } if err := discovery.Register(discovery.Instance{ - ServiceName: c.Name, - IP: registerIP, + ServiceName: serviceName, + IP: c.Nacos.RegisterIP, Port: port, Group: c.Nacos.Group, }); err != nil { logx.Errorf("nacos register: %v", err) + os.Exit(1) } + logx.Infof("服务注册成功: %s:%d", c.Nacos.RegisterIP, port) defer func() { if err := discovery.Deregister(); err != nil { @@ -77,50 +90,55 @@ func main() { }() db, err := mysql.New(mysql.Config{ - Host: c.Mysql.Host, - Port: c.Mysql.Port, - User: c.Mysql.User, - Password: c.Mysql.Password, - Database: c.Mysql.Database, - Charset: c.Mysql.Charset, - Prefix: c.Mysql.Prefix, - ReadHost: c.Mysql.ReadHost, - ReadPort: c.Mysql.ReadPort, - ReadUser: c.Mysql.ReadUser, - ReadPassword: c.Mysql.ReadPassword, - ReadDatabase: c.Mysql.ReadDatabase, + Host: utils.GetConfigString("mysql.host"), + Port: utils.GetConfigInt("mysql.port"), + User: utils.GetConfigString("mysql.user"), + Password: utils.GetConfigString("mysql.password"), + Database: utils.GetConfigString("mysql.database"), + Charset: utils.GetConfigString("mysql.charset"), + Prefix: utils.GetConfigString("mysql.prefix"), + ReadHost: utils.GetConfigString("mysql_read.host"), + ReadPort: utils.GetConfigInt("mysql_read.port"), + ReadUser: utils.GetConfigString("mysql_read.user"), + ReadPassword: utils.GetConfigString("mysql_read.password"), + ReadDatabase: utils.GetConfigString("mysql_read.database"), }) if err != nil { logx.Errorf("mysql init: %v", err) + os.Exit(1) } if err := redis.Init(redis.Config{ - Host: c.BizRedis.Host, - Port: c.BizRedis.Port, - Password: c.BizRedis.Password, - DB: c.BizRedis.DB, + Host: utils.GetConfigString("redis.host"), + Port: utils.GetConfigInt("redis.port"), + Password: utils.GetConfigString("redis.password"), + DB: utils.GetConfigInt("redis.db"), }); err != nil { logx.Errorf("redis init: %v", err) + os.Exit(1) } - modelbase.Init(db, modelbase.Config{Prefix: c.Mysql.Prefix}) + debug := utils.GetConfigBool("mysql.debug") + modelbase.Init(db, modelbase.Config{Prefix: utils.GetConfigString("mysql.prefix"), Debug: debug}) + + rpcConf := zrpc.RpcServerConf{ + ListenOn: listenOn, + } + rpcConf.Mode = mode ctx := svc.NewServiceContext(c, db) - s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { + s := zrpc.MustNewServer(rpcConf, func(grpcServer *grpc.Server) { product.RegisterProductServer(grpcServer, server.NewProductServer(ctx)) - - if c.Mode == service.DevMode || c.Mode == service.TestMode { + if mode == service.DevMode || mode == service.TestMode { reflection.Register(grpcServer) } }) defer s.Stop() - logx.AddWriter(logx.NewWriter(os.Stdout)) - s.AddUnaryInterceptors(validate.UnaryServerInterceptor(validate.MustNew())) - logx.Infof("Starting rpc server at %s...", c.ListenOn) + logx.Infof("Starting rpc server at %s...", listenOn) s.Start() } diff --git a/services/user/.gitignore b/services/user/.gitignore index 723b7fa..9ec6736 100644 --- a/services/user/.gitignore +++ b/services/user/.gitignore @@ -1 +1,2 @@ -run.toml \ No newline at end of file +run.toml +etc/user.yaml \ No newline at end of file diff --git a/services/user/Dockerfile b/services/user/Dockerfile new file mode 100644 index 0000000..e704282 --- /dev/null +++ b/services/user/Dockerfile @@ -0,0 +1,37 @@ +FROM golang:1.26.5-alpine AS builder + +WORKDIR /src + +ENV GOPROXY=https://goproxy.cn,direct \ + CGO_ENABLED=0 \ + GOOS=linux \ + GOARCH=amd64 + +COPY pkg ./pkg +COPY go.mod go.sum ./ + +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download + +COPY rpc/user/ ./rpc/user/ +COPY services/user/ ./services/user/ + +WORKDIR /src/services/user + +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/go-build \ + go build -ldflags="-s -w" -o /out/main ./ + +FROM alpine:3.22 + +WORKDIR /app + +ENV TZ=Asia/Shanghai + +RUN apk add --no-cache tzdata \ + && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone + +COPY --from=builder /out/main . + +CMD ["./main", "-f", "etc/user.yaml"] diff --git a/services/user/etc/user.yaml b/services/user/etc/user.yaml index aeae5fa..dd536ee 100644 --- a/services/user/etc/user.yaml +++ b/services/user/etc/user.yaml @@ -1,6 +1,6 @@ Nacos: Hosts: - - nacos:8848 + - rnacos:8848 NamespaceId: test Group: LONE_SERVICES RegisterIP: user