Develop RPC Server & RPC Client

Dubbo-go Quick Start

Based on the Triple protocol defined by Dubbo, you can easily write browser and gRPC compatible RPC services that run on both HTTP/1 and HTTP/2 simultaneously. The Dubbo Go SDK supports defining services using IDL or programming language-specific methods and provides a lightweight API for publishing or invoking these services.

This example demonstrates the RPC communication pattern based on the Triple protocol. The example uses Protocol Buffer to define the RPC service and demonstrates the processes of code generation, service publishing, and service access.

Prerequisites

Since we are using Protocol Buffer, we first need to install the relevant code generation tools, including protoc, protoc-gen-go, and protoc-gen-go-triple.

  1. Install protoc

    Check the Protocol Buffer Compiler Installation Guide

  2. Install protoc plugins

    Next, we install the plugins protoc-gen-go and protoc-gen-go-triple.

    1. go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
    2. go install dubbo.apache.org/dubbo-go/v3/cmd/protoc-gen-go-triple@v3.0.1

    Make sure protoc-gen-go and protoc-gen-go-triple are in your PATH. You can verify this with which protoc-gen-go. If that command does not work, please execute the following commands:

    1. [ -n "$(go env GOBIN)" ] && export PATH="$(go env GOBIN):${PATH}"
    2. [ -n "$(go env GOPATH)" ] && export PATH="$(go env GOPATH)/bin:${PATH}"

Quick Run Example

Download Example Source Code

We maintain a series of dubbo-go usage examples in the apache/dubbo-go-samples repository to help users quickly learn how to use dubbo-go.

You can download the example zip file and unzip it, or clone the repository:

  1. $ git clone --depth 1 https://github.com/apache/dubbo-go-samples

Switch to the quick start example directory:

  1. $ cd dubbo-go-samples/helloworld

Run Server

In the go-server/cmd directory:

Run the following command to start the server:

  1. $ go run server.go

Use cURL to verify that the server has been started correctly:

  1. $ curl \
  2. --header "Content-Type: application/json" \
  3. --data '{"name": "Dubbo"}' \
  4. http://localhost:20000/greet.GreetService/Greet
  5. Greeting: Hello world

Run Client

Open a new terminal and run the following command in the go-client/cmd directory to start the client:

  1. $ go run client.go
  2. Greeting: Hello world

This is a complete development process of a dubbo-go RPC communication service.

Source Code Explanation

Next, we will explain the source code of the dubbo-go-samples/helloworld example.

Define Service

The example uses Protocol Buffer (IDL) to define the Dubbo service.

  1. syntax = "proto3";
  2. package greet;
  3. option go_package = "github.com/apache/dubbo-go-samples/helloworld/proto;greet";
  4. message GreetRequest {
  5. string name = 1;
  6. }
  7. message GreetResponse {
  8. string greeting = 1;
  9. }
  10. service GreetService {
  11. rpc Greet(GreetRequest) returns (GreetResponse) {}
  12. }

This file declares a service called GreetService, defining the Greet method along with its request parameter GreetRequest and return value GreetResponse.

Generate Code

Before running the server or client, we need to generate the relevant code using protoc-gen-go and protoc-gen-go-triple.

  1. protoc --go_out=. --go_opt=paths=source_relative \
  2. --go-triple_out=. --go-triple_opt=paths=source_relative \
  3. ./greet.proto

After running the above command, you will see the following generated files in the target directory:

  1. proto
  2. ├── greet.pb.go
  3. ├── greet.proto
  4. └── greet.triple.go

In the proto/greet/v1 package, there are two parts:

  • greet.pb.go is generated by Google’s standard protoc-gen-go, which contains the structures of GreetRequest, GreetResponse, and the encoding/decoding rules.
  • greet.triple.go is produced by the custom Dubbo plugin protoc-gen-go-triple and includes key information, including the generated interface GreetService, constructors, and more.

Implement Service

Next, we need to add business logic by implementing the greet.GreetService interface.

  1. type GreetTripleServer struct {
  2. }
  3. func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
  4. resp := &greet.GreetResponse{Greeting: req.Name}
  5. return resp, nil
  6. }

Start Server

Create a new Server, register the GreetTripleServer we implemented earlier, and then initialize and start the Server, which will listen for requests on the specified port.

  1. func main() {
  2. srv, err := server.NewServer(
  3. server.WithServerProtocol(
  4. protocol.WithPort(20000),
  5. protocol.WithTriple(),
  6. ),
  7. )
  8. if err != nil {
  9. panic(err)
  10. }
  11. if err := greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil {
  12. panic(err)
  13. }
  14. if err := srv.Serve(); err != nil {
  15. logger.Error(err)
  16. }
  17. }

Access Service

The simplest way is to use an HTTP/1.1 POST request to access the service, passing the parameters as standard JSON format in the HTTP payload. Here is an example using a cURL command:

  1. curl \
  2. --header "Content-Type: application/json" \
  3. --data '{"name": "Dubbo"}' \
  4. http://localhost:20000/greet.GreetService/Greet

You can also use a Dubbo client to request the service. First, obtain the service proxy from the generated code in the greet package, specify the server address, and initialize it. Then you can initiate an RPC call.

  1. func main() {
  2. cli, err := client.NewClient(
  3. client.WithClientURL("127.0.0.1:20000"),
  4. )
  5. if err != nil {
  6. panic(err)
  7. }
  8. svc, err := greet.NewGreetService(cli)
  9. if err != nil {
  10. panic(err)
  11. }
  12. resp, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"})
  13. if err != nil {
  14. logger.Error(err)
  15. }
  16. logger.Infof("Greet response: %s", resp.Greeting)
  17. }

This is the basic working principle of dubbo-go RPC!

More Content

More Features of RPC Framework

Learn about Streaming communication models, configuring timeout durations, passing headers, and more framework configurations.

Governance capabilities such as service discovery

Learn how to use dubbo-go to develop microservices, incorporating service discovery, observability, traffic control, and more service governance capabilities.


Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)