Intercommunication with gRPC applications

Intercommunication with gRPC applications

1. Introduction

The triple protocol is 100% compatible with gRPC. This example demonstrates using dubbo-go to develop applications that interoperate with gRPC. You can view the complete example source code here.

2. How to Intercommunicate

The Triple protocol of Dubbo-go is compatible with the gRPC protocol. When creating the server, you can set protocol.WithTriple() to use the Triple protocol.

  1. srv, err := server.NewServer(
  2. server.WithServerProtocol(
  3. protocol.WithPort(20000),
  4. protocol.WithTriple(),
  5. ),
  6. )

3. Example

3.1 Server Introduction

Server proto File

Source file path: dubbo-go-sample/rpc/grpc/proto/greet.proto

  1. syntax = "proto3";
  2. package greet;
  3. option go_package = "github.com/apache/dubbo-go-samples/rpc/grpc/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. }

dubbo-go Server

Source file path: dubbo-go-sample/rpc/grpc/go-server/cmd/main.go

  1. type GreetTripleServer struct {
  2. }
  3. func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
  4. resp := &greet.GreetResponse{Greeting: "dubbo:" + req.Name}
  5. return resp, nil
  6. }
  7. func main() {
  8. srv, err := server.NewServer(
  9. server.WithServerProtocol(
  10. protocol.WithPort(20000),
  11. protocol.WithTriple(),
  12. ),
  13. )
  14. if err != nil {
  15. panic(err)
  16. }
  17. if err := greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil {
  18. panic(err)
  19. }
  20. if err := srv.Serve(); err != nil {
  21. logger.Error(err)
  22. }
  23. }

gRPC Server

Source file path: dubbo-go-sample/rpc/grpc/grpc-server/cmd/main.go

  1. type server struct {
  2. pb.UnimplementedGreetServiceServer
  3. }
  4. func (s *server) Greet(ctx context.Context, req *pb.GreetRequest) (*pb.GreetResponse, error) {
  5. resp := &pb.GreetResponse{Greeting: "grpc:" + req.Name}
  6. return resp, nil
  7. }
  8. func main() {
  9. lis, err := net.Listen("tcp", "127.0.0.1:20001")
  10. if err != nil {
  11. logger.Fatalf("failed to listen: %v", err)
  12. }
  13. s := grpc.NewServer()
  14. pb.RegisterGreetServiceServer(s, &server{})
  15. logger.Infof("server listening at %v", lis.Addr())
  16. if err := s.Serve(lis); err != nil {
  17. logger.Fatalf("failed to serve: %v", err)
  18. }
  19. }

3.2 Client Introduction

dubbo-go Client

Source file path: dubbo-go-sample/rpc/grpc/go-client/cmd/main.go

  1. package main
  2. import (
  3. "context"
  4. "dubbo.apache.org/dubbo-go/v3/client"
  5. _ "dubbo.apache.org/dubbo-go/v3/imports"
  6. greet "github.com/apache/dubbo-go-samples/rpc/grpc/proto"
  7. "github.com/dubbogo/gost/log/logger"
  8. )
  9. func main() {
  10. // test connect with dubbo
  11. dubboCli, err := client.NewClient(
  12. client.WithClientURL("127.0.0.1:20000"),
  13. )
  14. if err != nil {
  15. panic(err)
  16. }
  17. svc, err := greet.NewGreetService(dubboCli)
  18. if err != nil {
  19. panic(err)
  20. }
  21. resp, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"})
  22. if err != nil {
  23. logger.Error(err)
  24. }
  25. logger.Infof("Greet response: %s", resp.Greeting)
  26. // test connect with grpc
  27. grpcCli, err := client.NewClient(
  28. client.WithClientURL("127.0.0.1:20001"),
  29. )
  30. if err != nil {
  31. panic(err)
  32. }
  33. svc, err = greet.NewGreetService(grpcCli)
  34. if err != nil {
  35. panic(err)
  36. }
  37. resp, err = svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"})
  38. if err != nil {
  39. logger.Error(err)
  40. }
  41. logger.Infof("Greet response: %s", resp.Greeting)
  42. }

gRPC Client

Source file path: dubbo-go-sample/rpc/grpc/grpc-client/cmd/main.go

  1. package main
  2. import (
  3. "context"
  4. "time"
  5. "github.com/dubbogo/gost/log/logger"
  6. pb "github.com/apache/dubbo-go-samples/rpc/grpc/proto"
  7. "google.golang.org/grpc"
  8. "google.golang.org/grpc/credentials/insecure"
  9. )
  10. func main() {
  11. // test connect with grpc
  12. grpcConn, err := grpc.Dial("127.0.0.1:20001", grpc.WithTransportCredentials(insecure.NewCredentials()))
  13. if err != nil {
  14. logger.Fatalf("did not connect: %v", err)
  15. }
  16. defer grpcConn.Close()
  17. c := pb.NewGreetServiceClient(grpcConn)
  18. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  19. defer cancel()
  20. resp, err := c.Greet(ctx, &pb.GreetRequest{Name: "hello world"})
  21. if err != nil {
  22. logger.Fatalf("could not greet: %v", err)
  23. }
  24. logger.Infof("Greet response: %s", resp.Greeting)
  25. // test connect with dubbo
  26. dubboConn, err := grpc.Dial("127.0.0.1:20000", grpc.WithTransportCredentials(insecure.NewCredentials()))
  27. if err != nil {
  28. logger.Fatalf("did not connect: %v", err)
  29. }
  30. defer dubboConn.Close()
  31. c = pb.NewGreetServiceClient(dubboConn)
  32. ctx, cancel = context.WithTimeout(context.Background(), time.Second)
  33. defer cancel()
  34. resp, err = c.Greet(ctx, &pb.GreetRequest{Name: "hello world"})
  35. if err != nil {
  36. logger.Fatalf("could not greet: %v", err)
  37. }
  38. logger.Infof("Greet response: %s", resp.Greeting)
  39. }

3.3 Example Results

Start the dubbo-go server and gRPC server first, then start the dubbo-go client and gRPC client, and observe the console output.

Dubbo-go client output:

  1. Greet response: dubbo:hello world
  2. Greet response: grpc:hello world

gRPC client output:

  1. Greet response: grpc:hello world
  2. Greet response: dubbo:hello world

Feedback

Was this page helpful?

Yes No

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