thrift usage

RPC Client

Write IDL

// hello.thrift
namespace go hello.example

struct HelloReq {
    1: string Name
}

struct HelloResp {
    1: string RespBody;
}

service HelloService {
    HelloResp HelloMethod(1: HelloReq request);
    HelloResp HelloMethod1(1: HelloReq request);
    HelloResp HelloMethod2(1: HelloReq request);
}

Execute Command

Note: If the project is located outside of GOPATH, gomod must be specified. GOPATH defaults to a path relative to GOPATH as the name, and gomod may not be specified.

cwgo client  --type RPC  --idl hello.thrift  --server_name hellotest --module {{your_module_name}}

Generate Code

├── hello. thrift # IDL file
├── kitex_gen # Generate code related to IDL content
│ └── hello
│ └── example
│ ├── hello.go # The product of thriftgo, the go code containing the content defined by hello.thrift
│ ├── helloservice
│ │ ├── client.go # provides NewClient API
│ │ ├── helloservice.go # Provides some definitions shared by client.go and server.go
│ │ ├── invoker.go
│ │ └── server.go # provides NewServer API
│ ├── k-consts.go
│ └── k-hello.go # code generated by kitex outside of thriftgo's product
└── rpc
     └── hellotest
         ├── hellotest_client.go # client wrapper code
         ├── hellotest_default.go # client default implementation code
         └── hellotest_init.go # client initialization code

HTTP Client

Write IDL

Write a simple IDL for generating HTTP Server, which requires adding api.$method and api.base_domain annotations are used to fill in uri and host, please refer to them for details Hertz IDL Annotation Description.

// hello.thrift
namespace go hello.example

struct HelloReq {
    1: string Name (api.query="name");
}

struct HelloResp {
    1: string RespBody;
}


service HelloService {
    HelloResp HelloMethod1(1: HelloReq request) (api.get="/hello1");
    HelloResp HelloMethod2(1: HelloReq request) (api.get="/hello2");
    HelloResp HelloMethod3(1: HelloReq request) (api.get="/hello3");
}(
     api.base_domain="http://127.0.0.1:8888";
 )

Execute Command

Note: If the project is located outside of GOPATH, gomod must be specified. GOPATH defaults to a path relative to GOPATH as the name, and gomod may not be specified.

cwgo client  --type HTTP  --idl hello.thrift  --server_name hellotest --module {{your_module_name}}

Generate Code

A default client implementation is provided in hello_service.go, and users can use it directly. If there is a need for custom configuration, you can use the options provided in hertz_client.go to customize the complex configuration of the Client.

.
├── biz
│ └── http
│ └── hello_service
│ ├── hello_service.go # client initialization and calling code
│ └── hertz_client.go # client specific implementation code
├── hello. thrift # IDL file
└── hertz_gen #IDL content-related generated code
     └── hello
         └── example
             └── hello.go

client default implementation code

var defaultClient, _ = NewHelloServiceClient("http://127.0.0.1:8888")

func HelloMethod1(context context.Context, req *example.HelloReq, reqOpt ...config.RequestOption) (resp *example.HelloResp, rawResponse *protocol.Response, err error) {
return defaultClient.HelloMethod1(context, req, reqOpt...)
}

func HelloMethod2(context context.Context, req *example.HelloReq, reqOpt ...config.RequestOption) (resp *example.HelloResp, rawResponse *protocol.Response, err error) {
return defaultClient.HelloMethod2(context, req, reqOpt...)
}

func HelloMethod3(context context.Context, req *example.HelloReq, reqOpt ...config.RequestOption) (resp *example.HelloResp, rawResponse *protocol.Response, err error) {
return defaultClient.HelloMethod3(context, req, reqOpt...)
}

Last modified September 14, 2024 : doc: add kitex v0.11.0 release log (#1135) (796ca7c)