feat: Dockerfile remove config
This commit is contained in:
@@ -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 \
|
||||
.
|
||||
|
||||
|
||||
@@ -29,8 +29,6 @@ protoc -I. -I../pkg/third_party --descriptor_set_out=../bff/etc/order.pb --inclu
|
||||
- 增删 **RPC**
|
||||
- 改 **google.api.http** 路由
|
||||
|
||||
|
||||
|
||||
## 如何加一个新服务
|
||||
|
||||
以加 `order` 为例(对标现有 `product`)。
|
||||
|
||||
+14
-9
@@ -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"]
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
run.toml
|
||||
tmp
|
||||
etc/admin.yaml
|
||||
@@ -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"]
|
||||
@@ -1,7 +1,7 @@
|
||||
Nacos:
|
||||
Hosts:
|
||||
- host.docker.internal:8848
|
||||
- rnacos:8848
|
||||
NamespaceId: test
|
||||
Group: LONE_SERVICES
|
||||
RegisterIP: admin
|
||||
ConfigID: develop-admin
|
||||
ConfigID: admin
|
||||
@@ -1,2 +1,3 @@
|
||||
run.toml
|
||||
tmp
|
||||
etc/lonelog.yaml
|
||||
@@ -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"]
|
||||
@@ -1,6 +1,6 @@
|
||||
Nacos:
|
||||
Hosts:
|
||||
- host.docker.internal:8848
|
||||
- rnacos:8848
|
||||
NamespaceId: test
|
||||
Group: LONE_SERVICES
|
||||
RegisterIP: log
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
run.toml
|
||||
etc/order.yaml
|
||||
@@ -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"]
|
||||
@@ -1,6 +1,6 @@
|
||||
Nacos:
|
||||
Hosts:
|
||||
- host.docker.internal:8848
|
||||
- rnacos:8848
|
||||
NamespaceId: test
|
||||
Group: LONE_SERVICES
|
||||
RegisterIP: order
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
etc/product.yaml
|
||||
run.toml
|
||||
@@ -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"]
|
||||
|
||||
@@ -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
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -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"),
|
||||
}
|
||||
}
|
||||
|
||||
+60
-42
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
run.toml
|
||||
etc/user.yaml
|
||||
@@ -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"]
|
||||
@@ -1,6 +1,6 @@
|
||||
Nacos:
|
||||
Hosts:
|
||||
- nacos:8848
|
||||
- rnacos:8848
|
||||
NamespaceId: test
|
||||
Group: LONE_SERVICES
|
||||
RegisterIP: user
|
||||
|
||||
Reference in New Issue
Block a user