transfer_middleware/cmd/rpc/transfer.go

65 lines
1.7 KiB
Go

package main
import (
"flag"
"fmt"
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/zrpc"
"github.com/zeromicro/zero-contrib/zrpc/registry/nacos"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"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"
)
var configFile = flag.String("f", "../../config/transfer.yaml", "the config file")
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,
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)
}
}