zoukankan      html  css  js  c++  java
  • 创建服务端并调用

    创建服务端并调用

    syntax = "proto3";
    package services;
    message ProdRequest {
        int32 prod_id = 1; //传入id
    }
    
    message ProdResponse {
        int32 prod_stock = 1; //商品库存
    }
    
    service ProdService {
        rpc GetProdStock (ProdRequest) returns (ProdResponse); //ProdRequest这里作为参数传进来表示
    }

    通过protoc --go_out=plugins=grpc:../services Prod.proto 生成grpc中间文件

    然后创建一个实现了中间文件中的Server接口和Client接口的结构体

    package services
    
    import (
        "context"
    )
    
    type ProdService struct {
    }
    
    func (this *ProdService) GetProdStock(ctx context.Context, request *ProdRequest) (*ProdResponse, error) {
        return &ProdResponse{ProdStock: 20}, nil
    }
    

    将实现了接口的struct注册到rpcServer中去

    package main
    
    import (
        "google.golang.org/grpc"
        "grpcpro/services"
        "net"
    )
    
    func main() {
        rpcServer := grpc.NewServer()
        services.RegisterProdServiceServer(rpcServer, new(services.ProdService)) //将实现的Server注册到rpcServer中区
    
        lis, _ := net.Listen("tcp", ":8081")
        rpcServer.Serve(lis)
    }
    




  • 相关阅读:
    冲刺2 05
    冲刺02 04
    人月神话阅读笔记01
    进度条
    团队冲刺第十天
    团队冲刺第九天
    学习进度条13
    团队冲刺第八天
    怎样买书更便宜
    冲刺第七天
  • 原文地址:https://www.cnblogs.com/hualou/p/12070536.html
Copyright © 2011-2022 走看看