transfer_middleware/cmd/rpc/transfer.go

68 lines
1.9 KiB
Go
Raw Normal View History

2024-06-07 11:11:04 +08:00
package main
import (
"flag"
"fmt"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
2024-06-18 16:34:14 +08:00
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/zrpc"
2024-06-07 11:11:04 +08:00
"github.com/zeromicro/zero-contrib/zrpc/registry/nacos"
2024-06-18 16:34:14 +08:00
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
2024-06-07 11:11:04 +08:00
"strconv"
"strings"
"trasfer_middleware/cmd/rpc/internal/config"
"trasfer_middleware/cmd/rpc/internal/server"
"trasfer_middleware/cmd/rpc/internal/svc"
"trasfer_middleware/cmd/rpc/pb/transfer"
2024-07-16 18:39:48 +08:00
"trasfer_middleware/until"
2024-06-07 11:11:04 +08:00
)
2024-07-16 18:54:13 +08:00
var configFile = flag.String("f", fmt.Sprintf("%s%s", "../../config/", until.GetConfig()), "the config file")
2024-06-07 11:11:04 +08:00
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
ctx := svc.NewServiceContext(c)
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
transfer.RegisterTransferServer(grpcServer, server.NewTransferServer(ctx))
if c.Mode == service.DevMode || c.Mode == service.TestMode {
reflection.Register(grpcServer)
}
})
registerNacos(&c)
defer s.Stop()
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
s.Start()
}
func registerNacos(c *config.Config) {
sc := []constant.ServerConfig{}
for _, v := range c.Nacos.Hosts {
splitSlice := strings.Split(v, ":")
port, _ := strconv.Atoi(splitSlice[1])
sc = append(sc, *constant.NewServerConfig(splitSlice[0], uint64(port)))
}
cc := &constant.ClientConfig{
NamespaceId: c.Nacos.NameSpace,
TimeoutMs: c.Nacos.TimeOut,
NotLoadCacheAtStart: true,
2024-07-16 16:57:26 +08:00
Username: c.Nacos.Username,
Password: c.Nacos.Password,
2024-06-07 11:11:04 +08:00
LogDir: "/tmp/nacos/log",
CacheDir: "/tmp/nacos/cache",
LogLevel: "debug",
}
opts := nacos.NewNacosConfig(c.Nacos.ServiceName, c.ListenOn, sc, cc)
err := nacos.RegisterService(opts)
if err != nil {
panic(err)
}
}