与 gRPC 应用互通

与 gRPC 应用互通

1.介绍

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

2.如何互通

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

	srv, err := server.NewServer(
        server.WithServerProtocol(
        protocol.WithPort(20000),
        protocol.WithTriple(),
        ),
    )

3.案例

3.1服务端介绍

服务端proto文件

源文件路径:dubbo-go-sample/rpc/grpc/proto/greet.proto

syntax = "proto3";

package greet;

option go_package = "github.com/apache/dubbo-go-samples/rpc/grpc/proto;greet";

message GreetRequest {
  string name = 1;
}

message GreetResponse {
  string greeting = 1;
}

service GreetService {
  rpc Greet(GreetRequest) returns (GreetResponse) {}
}

dubbo-go服务端

源文件路径:dubbo-go-sample/rpc/grpc/go-server/cmd/main.go

type GreetTripleServer struct {
}

func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
	resp := &greet.GreetResponse{Greeting: "dubbo:" + req.Name}
	return resp, nil
}

func main() {
	srv, err := server.NewServer(
		server.WithServerProtocol(
			protocol.WithPort(20000),
			protocol.WithTriple(),
		),
	)
	if err != nil {
		panic(err)
	}

	if err := greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil {
		panic(err)
	}

	if err := srv.Serve(); err != nil {
		logger.Error(err)
	}
}

grpc服务端

源文件路径: dubbo-go-sample/rpc/grpc/grpc-server/cmd/main.go

type server struct {
	pb.UnimplementedGreetServiceServer
}

func (s *server) Greet(ctx context.Context, req *pb.GreetRequest) (*pb.GreetResponse, error) {
	resp := &pb.GreetResponse{Greeting: "grpc:" + req.Name}
	return resp, nil
}

func main() {
	lis, err := net.Listen("tcp", "127.0.0.1:20001")
	if err != nil {
		logger.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	pb.RegisterGreetServiceServer(s, &server{})
	logger.Infof("server listening at %v", lis.Addr())
	if err := s.Serve(lis); err != nil {
		logger.Fatalf("failed to serve: %v", err)
	}
}

3.2客户端介绍

dubbo-go客户端

源文件路径:dubbo-go-sample/rpc/grpc/go-client/cmd/main.go

package main

import (
	"context"

	"dubbo.apache.org/dubbo-go/v3/client"
	_ "dubbo.apache.org/dubbo-go/v3/imports"
	greet "github.com/apache/dubbo-go-samples/rpc/grpc/proto"
	"github.com/dubbogo/gost/log/logger"
)

func main() {
	// test connect with dubbo
	dubboCli, err := client.NewClient(
		client.WithClientURL("127.0.0.1:20000"),
	)
	if err != nil {
		panic(err)
	}

	svc, err := greet.NewGreetService(dubboCli)
	if err != nil {
		panic(err)
	}

	resp, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"})
	if err != nil {
		logger.Error(err)
	}
	logger.Infof("Greet response: %s", resp.Greeting)

	// test connect with grpc
	grpcCli, err := client.NewClient(
		client.WithClientURL("127.0.0.1:20001"),
	)
	if err != nil {
		panic(err)
	}
	svc, err = greet.NewGreetService(grpcCli)
	if err != nil {
		panic(err)
	}
	resp, err = svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"})
	if err != nil {
		logger.Error(err)
	}
	logger.Infof("Greet response: %s", resp.Greeting)
}

grpc客户端

源文件路径:dubbo-go-sample/rpc/grpc/grpc-client/cmd/main.go

package main

import (
	"context"
	"time"

	"github.com/dubbogo/gost/log/logger"

	pb "github.com/apache/dubbo-go-samples/rpc/grpc/proto"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func main() {
	// test connect with grpc
	grpcConn, err := grpc.Dial("127.0.0.1:20001", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		logger.Fatalf("did not connect: %v", err)
	}
	defer grpcConn.Close()
	c := pb.NewGreetServiceClient(grpcConn)
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	resp, err := c.Greet(ctx, &pb.GreetRequest{Name: "hello world"})
	if err != nil {
		logger.Fatalf("could not greet: %v", err)
	}
	logger.Infof("Greet response: %s", resp.Greeting)

	// test connect with dubbo
	dubboConn, err := grpc.Dial("127.0.0.1:20000", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		logger.Fatalf("did not connect: %v", err)
	}
	defer dubboConn.Close()
	c = pb.NewGreetServiceClient(dubboConn)
	ctx, cancel = context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	resp, err = c.Greet(ctx, &pb.GreetRequest{Name: "hello world"})
	if err != nil {
		logger.Fatalf("could not greet: %v", err)
	}
	logger.Infof("Greet response: %s", resp.Greeting)
}

3.3案例效果

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

Dubbo-go客户端输出:

Greet response: dubbo:hello world
Greet response: grpc:hello world

grpc客户端输出:

Greet response: grpc:hello world
Greet response: dubbo:hello world
最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)