feat: Dockerfile remove config
This commit is contained in:
@@ -1 +1,2 @@
|
||||
etc/product.yaml
|
||||
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
|
||||
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"`
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user