80 lines
2.0 KiB
Go
80 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"github.com/suyiiyii/hertz101/app/user/biz/dal"
|
|
"github.com/suyiiyii/hertz101/app/user/biz/dal/mysql"
|
|
"github.com/suyiiyii/hertz101/app/user/biz/dal/query"
|
|
"github.com/suyiiyii/hertz101/common/mtl"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/cloudwego/kitex/pkg/klog"
|
|
"github.com/cloudwego/kitex/pkg/rpcinfo"
|
|
"github.com/cloudwego/kitex/server"
|
|
kitexlogrus "github.com/kitex-contrib/obs-opentelemetry/logging/logrus"
|
|
consul "github.com/kitex-contrib/registry-consul"
|
|
"github.com/suyiiyii/hertz101/app/user/conf"
|
|
"github.com/suyiiyii/hertz101/rpc_gen/kitex_gen/user/userservice"
|
|
"go.uber.org/zap/zapcore"
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
)
|
|
|
|
func main() {
|
|
|
|
//err := godotenv.Load()
|
|
//if err != nil {
|
|
// klog.Error(err.Error())
|
|
//}
|
|
dal.Init()
|
|
query.Use(mysql.DB)
|
|
opts := kitexInit()
|
|
|
|
mtl.InitTracing(conf.GetConf().Kitex.Service)
|
|
|
|
svr := userservice.NewServer(new(UserServiceImpl), opts...)
|
|
|
|
err := svr.Run()
|
|
if err != nil {
|
|
klog.Error(err.Error())
|
|
}
|
|
}
|
|
|
|
func kitexInit() (opts []server.Option) {
|
|
// address
|
|
addr, err := net.ResolveTCPAddr("tcp", conf.GetConf().Kitex.Address)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
opts = append(opts, server.WithServiceAddr(addr))
|
|
|
|
// service info
|
|
opts = append(opts, server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{
|
|
ServiceName: conf.GetConf().Kitex.Service,
|
|
}))
|
|
|
|
r, err := consul.NewConsulRegister(conf.GetConf().Registry.RegistryAddress[0])
|
|
if err != nil {
|
|
klog.Fatal(err)
|
|
}
|
|
opts = append(opts, server.WithRegistry(r))
|
|
|
|
// klog
|
|
logger := kitexlogrus.NewLogger()
|
|
klog.SetLogger(logger)
|
|
klog.SetLevel(conf.LogLevel())
|
|
asyncWriter := &zapcore.BufferedWriteSyncer{
|
|
WS: zapcore.AddSync(&lumberjack.Logger{
|
|
Filename: conf.GetConf().Kitex.LogFileName,
|
|
MaxSize: conf.GetConf().Kitex.LogMaxSize,
|
|
MaxBackups: conf.GetConf().Kitex.LogMaxBackups,
|
|
MaxAge: conf.GetConf().Kitex.LogMaxAge,
|
|
}),
|
|
FlushInterval: time.Minute,
|
|
}
|
|
klog.SetOutput(asyncWriter)
|
|
server.RegisterShutdownHook(func() {
|
|
asyncWriter.Sync()
|
|
})
|
|
return
|
|
}
|