Complete an RPC call

1. Generate Demo project

Use the installed dubbogo-cli tool to create a demo project.

$ mkdir quickstart
$ cd quickstart
$ dubbogo-cli newDemo.
$ tree .
.
├── api
│ ├── samples_api.pb.go
│ ├── samples_api.proto
│ └── samples_api_triple.pb.go
├── go-client
│ ├── cmd
│ │ └── client.go
│ └── conf
│ └── dubbogo.yaml
├── go-server
│ ├── cmd
│ │ └── server.go
│ └── conf
│ └── dubbogo.yaml
└── go.mod

You can see that the generated project contains a client project and a server project, as well as related configuration files.

1.1 View the interface description file helloworld.proto

syntax = "proto3";
package api;

option go_package = "./;api";

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (User) {}
  // Sends a greeting via stream
  rpc SayHelloStream (stream HelloRequest) returns (stream User) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message User {
  string name = 1;
  string id = 2;
  int32 age = 3;
}

In the demo project, an interface description file is generated by default. The interface service name is api.Greeter, which contains two RPC methods, the input parameter is HelloRequest, and the return value is User. The two methods are ordinary RPC methods and Streaming type RPC methods.

1.2 (*Optional) Compile the pb interface with the installed compilation tool

$ cd api
$ protoc --go_out=. --go-triple_out=. ./samples_api.proto

Parameter meaning: --go_out=. Use the protoc-gen-go plugin installed above to generate files to the current directory, --go-triple_out=. Use the protoc-gen-go-triple installed above Plugins, generate files to the current directory.

After executing this command, two files will be generated, namely helloworld.pb (contains proto structure) and helloworld_triple.pb.go (contains triple protocol interface).

In the demo project, these two files are pre-generated. After modifying the .proto file, execute the command generation again to overwrite it.

2. Open an RPC call

Execution in the project root directory

$ go mod tidy

Pull to the latest framework dependencies:

module helloworld

go 1.17

require (
dubbo.apache.org/dubbo-go/v3 v3.0.1
github.com/dubbogo/grpc-go v1.42.9
github.com/dubbogo/triple v1.1.8
google.golang.org/protobuf v1.27.1
)

require (
...
)

Start the server and the client successively: Open two terminals, execute go run . in the go-server/cmd and go-client/cmd folders respectively, and you can see the output on the client:

client response result: name: "Hello laurence" id: "12345" age:21

Get the result of the call successfully

3. more

Careful readers can find that the server written in the above example can accept ordinary RPC and streaming RPC call requests from the client. At present, only the Client for common calls has been written. Readers can try to write streaming clients and servers based on the examples in the samples library.


Last modified January 2, 2023: Enhance Dubbogo docs (#1800) (71c8e72274)