与 gRPC 应用互通

与 gRPC 应用互通

1.介绍

triple 协议 100% 兼容 gRPC,本示例演示使用 dubbo-go 开发与 grpc 互调的应用,可在此查看 完整示例源码地址

2.如何互通

Dubbo-go的Triple协议能够兼容grpc协议 在创建服务端时,可以设置protocol.WithTriple()使用Triple协议

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

3.案例

3.1服务端介绍

服务端proto文件

源文件路径: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服务端

源文件路径: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服务端

源文件路径: 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客户端介绍

dubbo-go客户端

源文件路径: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客户端

源文件路径: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案例效果

先启动dubbo-go服务端和grpc服务端,然后启动dubbo-go客户端和grpc客户端,观察控制台输出

Dubbo-go客户端输出:

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

grpc客户端输出:

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

最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)