From 231bdf80c234de47a79b312326a8dd6a0e74de2b Mon Sep 17 00:00:00 2001 From: copy rogers <40619032+rogerogers@users.noreply.github.com> Date: Fri, 7 Jun 2024 23:34:34 +0800 Subject: [PATCH] chore: make rpc client global (#147) * chore: make rpc client global Signed-off-by: rogerogers * chore: fix typo Signed-off-by: rogerogers * chore: upgrade workflow gofumt version Signed-off-by: rogerogers * chore: workflow use ubuntu-latest Signed-off-by: rogerogers * chore: fix lint Signed-off-by: rogerogers --------- Signed-off-by: rogerogers --- .github/workflows/pr-check.yml | 2 +- .github/workflows/tests.yml | 6 +++--- .../biz/handler/favorite/favorite_handler.go | 2 +- .../biz/router/favorite/middleware.go | 2 +- .../biz/handler/api/student_api.go | 20 ++++--------------- .../hertz-server/infra/rpc/client.go | 13 ++++++++++++ hz_kitex_demo/hertz-server/main.go | 3 +++ 7 files changed, 26 insertions(+), 22 deletions(-) create mode 100644 hz_kitex_demo/hertz-server/infra/rpc/client.go diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index ac47c251..e634a4cf 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -4,7 +4,7 @@ on: [pull_request] jobs: compliant: - runs-on: self-hosted + runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7f31b269..e97ee750 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -4,7 +4,7 @@ on: [push, pull_request] jobs: lint-and-ut: - runs-on: self-hosted + runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 @@ -23,8 +23,8 @@ jobs: - name: Lint run: | go vet -stdmethods=false $(go list ./...) - go install mvdan.cc/gofumpt@v0.5.0 - test -z "$(gofumpt -l -extra .)" + go install mvdan.cc/gofumpt@v0.6.0 + test -z "$(gofumpt -l -extra .)" - name: Unit Test run: go test -race -covermode=atomic -coverprofile=coverage.out ./... diff --git a/bizdemo/tiktok_demo/biz/handler/favorite/favorite_handler.go b/bizdemo/tiktok_demo/biz/handler/favorite/favorite_handler.go index eb275da9..c848a5db 100644 --- a/bizdemo/tiktok_demo/biz/handler/favorite/favorite_handler.go +++ b/bizdemo/tiktok_demo/biz/handler/favorite/favorite_handler.go @@ -32,7 +32,7 @@ import ( // FavoriteAction like and unlike operations of the logged-in user on the video. // -// @router /douyin/favortie/action/ [POST] +// @router /douyin/favorite/action/ [POST] func FavoriteAction(ctx context.Context, c *app.RequestContext) { var err error var req favorite.DouyinFavoriteActionRequest diff --git a/bizdemo/tiktok_demo/biz/router/favorite/middleware.go b/bizdemo/tiktok_demo/biz/router/favorite/middleware.go index 90ce53c6..432f37bc 100644 --- a/bizdemo/tiktok_demo/biz/router/favorite/middleware.go +++ b/bizdemo/tiktok_demo/biz/router/favorite/middleware.go @@ -56,7 +56,7 @@ func _favoriteMw() []app.HandlerFunc { return nil } -func _favortieMw() []app.HandlerFunc { +func _favoriteMw() []app.HandlerFunc { // your code... return nil } 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..f60bb230 100755 --- a/hz_kitex_demo/hertz-server/main.go +++ b/hz_kitex_demo/hertz-server/main.go @@ -6,6 +6,8 @@ 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 +19,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