美文网首页
Golang rpc框架: Kitex 简介

Golang rpc框架: Kitex 简介

作者: 夕午wuw | 来源:发表于2023-03-20 13:34 被阅读0次

本文通过演示Kitex 支持的PingPong、Oneway两种消息类型进行echo,对kitex的代码生成功能进行介绍

  1. 创建项目文件 test
mkdir test
cd test
  1. 初始化go module
go mod init test
  1. 编写idl文件
touch echo.thrift
namespace go echo

struct Request {
    1: string Msg
}

struct Response {
    1: string Msg
}

service EchoService {
    Response Echo(1: Request req); // pingpong method
    oneway void VisitOneway(1: Request req); // oneway method
}
  1. 安装Kitex
go install github.com/cloudwego/kitex/tool/cmd/kitex@latest
  1. 生成骨架
kitex -module test -service echoservice echo.thrift 

-module test是项目的go module名字, -service echoservice是生成的微服务名字,同时也是最后生成的可执行文件的名字 echo.thrift指定idl文件。生成后的项目结构如下:

├── build.sh
├── echo.thrift
├── go.mod
├── go.sum
├── handler.go
├── kitex_gen
│ └── echo
│ ├── echo.go
│ ├── echoservice
│ │ ├── client.go
│ │ ├── echoservice.go
│ │ ├── invoker.go
│ │ └── server.go
│ ├── k-consts.go
│ └── k-echo.go
├── kitex_info.yaml
├── main.go
└── script
└── bootstrap.sh

  1. 编写服务逻辑
    服务端的逻辑在handler.go中,写成如下形式
package main

import (
    "context"
    echo "test/kitex_gen/echo"
)

// EchoServiceImpl implements the last service interface defined in the IDL.
type EchoServiceImpl struct{}

// Echo implements the EchoServiceImpl interface.
func (s *EchoServiceImpl) Echo(ctx context.Context, req *echo.Request) (resp *echo.Response, err error) {
    return &echo.Response{Msg: "world"}, nil
}

// VisitOneway implements the EchoServiceImpl interface.
// Oneway methods are not guaranteed to receive 100% of the requests sent by the client.
// And the client may not perceive the loss of requests due to network packet loss.
// If possible, do not use oneway methods.
func (s *EchoServiceImpl) VisitOneway(ctx context.Context, req *echo.Request) (err error) {
    return nil
}
  1. 编译运行服务器
    编译:
sh build.sh

执行上述命令后,会生成一个 output 目录,里面含有我们的编译产物。

sh output/bootstrap.sh

执行上述命令后,Echo 服务开始运行

  1. 创建oneway和pingpong两种消息类型的client
mkdir client
cd client
mkdir oneway
mkdir pingpong

分别在oneway和pingpong目录下创建client.go

// oneway
package main

import (
    "context"
    "test/kitex_gen/echo"
    "test/kitex_gen/echo/echoservice"

    "github.com/cloudwego/kitex/client"
)

func main() {
    cli, err := echoservice.NewClient("destServiceName", client.WithHostPorts("0.0.0.0:8888"))
    if err != nil {
        panic(err)
    }
    req := echo.NewRequest()
    req.Msg = "hello"
    err = cli.VisitOneway(context.Background(), req)
    if err != nil {
        panic(err)
    }
    // no response return
}
// pingpong
package main

import (
    "context"
    "fmt"
    "test/kitex_gen/echo"
    "test/kitex_gen/echo/echoservice"

    "github.com/cloudwego/kitex/client"
)

func main() {
    cli, err := echoservice.NewClient("destServiceName", client.WithHostPorts("0.0.0.0:8888"))
    if err != nil {
        panic(err)
    }
    req := echo.NewRequest()
    req.Msg = "hello"
    resp, err := cli.Echo(context.Background(), req)
    if err != nil {
        panic(err)
    }
    fmt.Println(resp.Msg)
}

执行go run client.go即可完成两种方式的rpc调用。

相关文章

网友评论

      本文标题:Golang rpc框架: Kitex 简介

      本文链接:https://www.haomeiwen.com/subject/apzyrdtx.html