From 2012b613d204bdc415d4bc6bc7dfd921ee31d92b Mon Sep 17 00:00:00 2001 From: rogerogers Date: Wed, 29 May 2024 23:48:09 +0800 Subject: [PATCH] chore: make rpc client global Signed-off-by: rogerogers --- .../biz/handler/api/student_api.go | 20 ++++--------------- .../hertz-server/infra/rpc/client.go | 13 ++++++++++++ hz_kitex_demo/hertz-server/main.go | 2 ++ 3 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 hz_kitex_demo/hertz-server/infra/rpc/client.go diff --git a/hz_kitex_demo/hertz-server/biz/handler/api/student_api.go b/hz_kitex_demo/hertz-server/biz/handler/api/student_api.go index 433a679f..2c29fa04 100644 --- a/hz_kitex_demo/hertz-server/biz/handler/api/student_api.go +++ b/hz_kitex_demo/hertz-server/biz/handler/api/student_api.go @@ -6,13 +6,10 @@ import ( "context" "fmt" + "github.com/cloudwego/hertz/pkg/app" "hertz-examples/hz_demo/hertz-server/biz/model/api" + "hertz-examples/hz_demo/hertz-server/infra/rpc" "hertz-examples/hz_demo/hertz-server/kitex_gen/student/management" - "hertz-examples/hz_demo/hertz-server/kitex_gen/student/management/studentmanagement" - - client2 "github.com/cloudwego/kitex/client" - - "github.com/cloudwego/hertz/pkg/app" ) // QueryStudent . @@ -30,16 +27,12 @@ func QueryStudent(ctx context.Context, c *app.RequestContext) { // todo: do some other business processing // Use kitex client to make rpc calls and request back-end services - client, err := studentmanagement.NewClient("student", client2.WithHostPorts("127.0.0.1:8888")) - if err != nil { - panic(err) - } reqRpc := &management.QueryStudentRequest{ Num: fmt.Sprintf("%d", req.Num), } - respRpc, err := client.QueryStudent(ctx, reqRpc) + respRpc, err := rpc.Client.QueryStudent(ctx, reqRpc) if err != nil { panic(err) } @@ -74,18 +67,13 @@ func InsertStudent(ctx context.Context, c *app.RequestContext) { return } - client, err := studentmanagement.NewClient("student", client2.WithHostPorts("127.0.0.1:8888")) - if err != nil { - panic(err) - } - reqRpc := &management.InsertStudentRequest{ Num: req.Num, Name: req.Name, Gender: req.Gender, } - respRpc, err := client.InsertStudent(ctx, reqRpc) + respRpc, err := rpc.Client.InsertStudent(ctx, reqRpc) if err != nil { panic(err) } diff --git a/hz_kitex_demo/hertz-server/infra/rpc/client.go b/hz_kitex_demo/hertz-server/infra/rpc/client.go new file mode 100644 index 00000000..5b80a149 --- /dev/null +++ b/hz_kitex_demo/hertz-server/infra/rpc/client.go @@ -0,0 +1,13 @@ +package rpc + +import ( + "hertz-examples/hz_demo/hertz-server/kitex_gen/student/management/studentmanagement" + + client "github.com/cloudwego/kitex/client" +) + +var Client studentmanagement.Client + +func InitClient() { + Client, _ = studentmanagement.NewClient("student", client.WithHostPorts("127.0.0.1:8888")) +} diff --git a/hz_kitex_demo/hertz-server/main.go b/hz_kitex_demo/hertz-server/main.go index d1a16c04..a0e43d5b 100755 --- a/hz_kitex_demo/hertz-server/main.go +++ b/hz_kitex_demo/hertz-server/main.go @@ -5,6 +5,7 @@ package main import ( "context" "fmt" + "hertz-examples/hz_demo/hertz-server/infra/rpc" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/server" @@ -17,6 +18,7 @@ func main() { server.WithHostPorts("127.0.0.1:8080"), server.WithKeepAlive(true), // 默认就开启长连接 ) + rpc.InitClient() // This in can also do some business initialization work and the user decides what this part is