随着业务规模的增长,单体应用的存在已经无法满足系统的需求,分布式架构逐渐成为了主流。而在分布式系统中,RPC成为了不可或缺的一部分,它提供了一种方便、高效、可靠的远程调用服务方式,使得各个服务之间能够快速、稳定地进行数据交互和调用。
而对于跨语言的RPC调用来说,要求通信协议和序列化协议均需要兼容多种编程语言,因而实现起来相对困难。本文将介绍如何利用go-zero框架实现跨语言的分布式RPC调用,旨在为读者提供一种实用的解决方案。
- go-zero框架简介
go-zero是一个轻量级的Web框架,它采用了go语言原生的net/http模块,提供一套简洁、易用、高性能的API开发方式,可以方便地将HTTP服务与微服务相结合。go-zero可以帮助我们快速搭建分布式的、高并发的服务端应用程序,可以免费地在GitHub上获取代码和文档。
- 实现跨语言的RPC调用
2.1 定义服务
我们在go-zero中定义服务时需要先编写一个proto文件,定义服务端和客户端之间的通信接口。假设我们定义了一个名叫Example的服务,它包含两个方法:
syntax = "proto3";
package rpc;
service Example {
rpc SayHello (Request) returns (Response);
rpc GetUser (UserRequest) returns (UserResponse);
}
message Request {
string name = 1;
}
message Response {
string message = 1;
}
message UserRequest {
string id = 1;
}
message UserResponse {
string name = 1;
string email = 2;
}
定义完proto文件后,我们需要使用protobuf编译器将其编译成go语言的源文件,执行以下命令:
protoc --go_out=. --go-grpc_out=. rpc.proto
这将生成rpc.pb.go和rpc_grpc.pb.go两个文件。
2.2 实现服务端
在go-zero框架中,我们可以使用go-grpc模块来实现grpc服务。在实现服务端时,需要实现proto文件中定义的接口,并且使用go-zero提供的server.NewServer并调用AddService方法添加服务,然后在Init方法中启动grpc服务。
package server
import (
"context"
"rpc"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/stores/sqlx"
"github.com/tal-tech/go-zero/core/syncx"
"github.com/tal-tech/go-zero/zrpc"
"google.golang.org/grpc"
)
type ExampleContext struct {
Logx logx.Logger
SqlConn sqlx.SqlConn
CacheConn syncx.SharedCalls
}
type ExampleServer struct {
Example rpc.ExampleServer
}
func NewExampleServer(ctx ExampleContext) *ExampleServer {
return &ExampleServer{
Example: &exampleService{
ctx: ctx,
},
}
}
func (s *ExampleServer) Init() {
server := zrpc.MustNewServer(zrpc.RpcServerConf{
BindAddress: "localhost:7777",
})
rpc.RegisterExampleServer(server, s.Example)
server.Start()
}
type exampleService struct {
ctx ExampleContext
}
func (s *exampleService) SayHello(ctx context.Context, req *rpc.Request) (*rpc.Response, error) {
return &rpc.Response{
Message: "Hello, " + req.Name,
}, nil
}
func (s *exampleService) GetUser(ctx context.Context, req *rpc.UserRequest) (*rpc.UserResponse, error) {
// 查询数据库
return &rpc.UserResponse{
Name: "name",
Email: "email",
}, nil
}
在服务器上,我们可以使用Init方法来启动RPC服务器,使用MustNewServer来创建RPC服务器,必须传入一个RpcServerConf结构,包含我们要绑定的地址。
2.3 实现客户端
在go-zero框架中,我们可以使用zrpc模块来实现grpc客户端。使用zrpc.Dial创建连接并实例化rpc客户端。
package client
import (
"context"
"rpc"
"google.golang.org/grpc"
)
type ExampleClient struct {
client rpc.ExampleClient
}
func NewExampleClient(conn *grpc.ClientConn) *ExampleClient {
return &ExampleClient{
client: rpc.NewExampleClient(conn),
}
}
func (c *ExampleClient) SayHello(name string) (string, error) {
resp, err := c.client.SayHello(context.Background(), &rpc.Request{
Name: name,
})
if err != nil {
return "", err
}
return resp.Message, nil
}
func (c *ExampleClient) GetUser(id string) (*rpc.UserResponse, error) {
return c.client.GetUser(context.Background(), &rpc.UserRequest{
Id: id,
})
}
在客户端上,我们只需要使用NewExampleClient函数创建RPC客户端即可。SayHello方法的作用是在服务端上获取一个响应,并将其返回。GetUser方法向服务器获取用户信息的响应,将其以UserResponse的形式返回。
2.4 测试
现在,我们已经实现了服务端和客户端的代码,我们可以通过以下代码来测试:
package main
import (
"fmt"
"log"
"rpc_example/client"
"rpc_example/server"
"google.golang.org/grpc"
)
func main() {
ctx := server.ExampleContext{}
conn, err := grpc.Dial("localhost:7777", grpc.WithInsecure())
if err != nil {
log.Fatalf("grpc.Dial err :%v", err)
}
defer conn.Close()
client := client.NewExampleClient(conn)
re
.........................................................