您的位置 首页 golang

Go语言:gRPC框架使用

上节课已经学习了gRPC基本知识,对gRPC有了初步的认识。本节课通过编程实现gRPC编程。

定义服务

我们想要实现的是通过gRPC框架进行远程服务调用,首先第一步应该是要有服务。利用之前所掌握的内容,gRPC框架支持对服务的定义和生成。gRPC框架默认使用protocol buffers作为接口定义语言,用于描述网络传输消息结构。除此之外,还可以使用protobuf定义服务接口。

 syntax = "proto3";
package message;

//订单请求参数
message OrderRequest {
    string orderId = 1;
    int64 timeStamp = 2;
}
​
//订单信息
message OrderInfo {
    string OrderId = 1;
    string OrderName = 2;
    string OrderStatus = 3;
}
​
//订单服务service定义
service OrderService{
    rpc GetOrderInfo(OrderRequest) returns (OrderInfo);
}  

我们通过proto文件定义了数据结构的同时,还定义了要实现的服务接口,GetOrderInfo即是具体服务接口的定义,在GetOrderInfo接口定义中,OrderRequest表示是请求传递的参数,OrderInfo表示处理结果返回数据参数。

编译.proto文件

环境准备

定义的proto文件需要通过编译,生成go语言代码文件,供客户端程序和服务端程序使用。可以安装go语言环境中的关于proto的插件。

 go get -a github.com/golang/protobuf/protoc-gen-go
  

-a 参数标示下载好后直接做 go install

编译.proto文件

基本用法

可以通过基本编译命令完成对.proto文件的编译.基础编译命令如下:

 protoc --go_out=. *.proto  

gRPC编译支持

如果定义的.proto文件,如本案例中所示,定义中包含了服务接口的定义,而我们想要使用gRPC框架实现RPC调用。开发者可以采用protocol-gen-go库提供的插件编译功能,生成兼容gRPC框架的golang语言代码。只需要在基本编译命令的基础上,指定插件的参数,告知protoc编译器即可。具体的编译生成兼容gRPC框架的服务代码的命令如下:

 protoc --go_out=plugins=grpc:. *.proto  

gRPC实现RPC编程

服务接口实现

在.proto定义好服务接口并生成对应的go语言文件后,需要对服务接口做具体的实现。定义服务接口具体由OrderServiceImpl进行实现,并实现GetOrderInfo详细内容,服务实现逻辑与前文所述内容相同。不同点是服务接口参数的变化。详细代码实现如下:

 type OrderServiceImpl struct {
}
​
//具体的方法实现
func (os *OrderServiceImpl) GetOrderInfo(ctx context.Context, request *message.OrderRequest) (*message.OrderInfo, error) {
    orderMap := map[string]message.OrderInfo{
        "201907300001": message.OrderInfo{OrderId: "201907300001", OrderName: "衣服", OrderStatus: "已付款"},
        "201907310001": message.OrderInfo{OrderId: "201907310001", OrderName: "零食", OrderStatus: "已付款"},
        "201907310002": message.OrderInfo{OrderId: "201907310002", OrderName: "食品", OrderStatus: "未付款"},
    }
​
    var response *message.OrderInfo
    current := time.Now().Unix()
    if (request.TimeStamp > current) {
        *response = message.OrderInfo{OrderId: "0", OrderName: "", OrderStatus: "订单信息异常"}
    } else {
        result := orderMap[request.OrderId]
        if result.OrderId != "" {
            fmt.Println(result)
            return &result, nil
        } else {
            return nil, errors.New("server error")
        }
    }
    return response, nil
}
  

gRPC实现服务端

使用gRPC框架,首先实现服务端的程序。既然使用gRPC框架来实现,就需要调用gRPC进行服务方法的注册以及监听的处理。服务注册和监听处理实现如下:

 func main() {
​
    server := grpc.NewServer()
​
    message.RegisterOrderServiceServer(server, new(OrderServiceImpl))
​
    lis, err := net.Listen("tcp", ":8090")
    if err != nil {
        panic(err.Error())
    }
    server.Serve(lis)
}
  

gRPC实现客户端

实现完服务端以后,实现客户端程序。和服务端程序关系对应,调用gRPC框架的方法获取相应的客户端程序,并实现服务的调用,具体编程实现如下:

 func main() {
​
    //1、Dail连接
    conn, err := grpc.Dial("localhost:8090", grpc.WithInsecure())
    if err != nil {
        panic(err.Error())
    }
    defer conn.Close()
​
    orderServiceClient := message.NewOrderServiceClient(conn)
​
    orderRequest := &message.OrderRequest{OrderId: "201907300001", TimeStamp: time.Now().Unix()}
    orderInfo, err := orderServiceClient.GetOrderInfo(context.Background(), orderRequest)
    if orderInfo != nil {
        fmt.Println(orderInfo.GetOrderId())
        fmt.Println(orderInfo.GetOrderName())
        fmt.Println(orderInfo.GetOrderStatus())
    }
}
  

运行程序

经过上述步骤后,程序及逻辑全部开发完成。程序运行,打印如下结果:

 201907300001
衣服
已付款  

@author:Davie版权所有

文章来源:智云一二三科技

文章标题:Go语言:gRPC框架使用

文章地址:https://www.zhihuclub.com/98217.shtml

关于作者: 智云科技

热门文章

评论已关闭

30条评论

  1. The questions to ask at a restaurant depend on what s being ordered, Begun says Controlled drug release through regulated biodegradation of poly lactic acid using inorganic salts

  2. We found that it was challenging to keep fish anesthetized for 20 minutes after luciferin delivery without affecting survival Escalation and de escalation in HER2 positive early breast cancer

  3. 0 array Affymetrix, Santa Clara, CA according to the manufacturer s protocol manual version 700217 rev 3, with minor modification The national team intends to penis enlargement gif complete the construction of 128 power generation arrays next year

  4. Generation of the Eomes CreER This study shows that GF120918 can also enhance the efficacy of dasatinib in human GBM cells

  5. They were followed for at least 18 months range 18 24 mo, including transvaginal ultrasonography every 6 months and hysteroscopy when endometrial irregularity was noted

  6. The SARRP has an X ray tube mounted on a gantry, with a motorized stage serving as a couch on which the mouse is placed

  7. Based on the IDSA guidelines for the management of prosthetic joint infection, Doxicon is an effective and recommended agent for treatment oral phase of prosthetic joint infection and for chronic oral antimicrobial suppression of prosthetic joint infection due to staphylococci or Cutibacterium acnes RESOLVE does not endorse any of the listed products and programs

  8. 204 Women had at least two screening cycles and were followed for a median of 2 years

  9. epidermidis, among a variety of other bacteria, behaves differently at higher temperatures, producing divergent proteins that may act as virulence factors not otherwise found in healthy controls 4, 15 Do not stand still for prolonged periods

  10. Detailed information about these compounds is available using the NIH Chemical Genomics Center Pharmaceutical Collection browser 37

  11. A Schedule 9 substance is a substance which may be abused or misused, the manufacture, possession, sale or use of which should be prohibited by law except when required for medical or scientific research, or for analytical, teaching or training purposes with approval of Commonwealth and or State or Territory Health Authorities

  12. Both exogenous and endogenous ROS can induce sperm DNA damage in vitro, confirming that ROS may play a role in the etiology of sperm DNA damage in infertile men 30, 31 tecovirimat will decrease the level or effect of tadalafil by affecting hepatic intestinal enzyme CYP3A4 metabolism

  13. P E GSEA to find tamoxifen induced signatures of estrogen response in hypothalamic cell types Bodenreider O The Unified Medical Language System UMLS integrating biomedical terminology

  14. I am pregnant, and have been battling this horrible rash on my face for the past 6 8weeks at least actually, much more, but, it was just around my nose, and I thought i had dry skin

  15. After you die, the soul is not high blood pressure medication causing diabetes only can a coffee enema lower blood pressure as simple generic blood pressure medicine names as being redeemed, You will be rewarded by the Most High God, let me guess, will the divine power be further advanced

  16. Patients should be instructed to read the Medication Guide supplied as required by law when Tamec is dispensed

  17. Prodrugs of a compound of the disclosure may be prepared by modifying functional groups present in the compound of the disclosure in such a way that the modifications are cleaved, either in routine manipulation or in vivo, to the parent compound of the disclosure

网站地图