• [go]grpc远程接口调用实现


    // grpc序列化/反序列化成对应语言的对象
    
    // 1.写idl(数据类型+方法) 
    // 2.生成对应语言的序列化/反序列化代码 
    // 3.方法需要自己实现
    
    // 环境(将gopath/bin加入path)
    
    //安装grpc引擎
    go get -u google.golang.org/grpc
    
    //安装grpc-go插件(适配go语言)
    go get -u github.com/golang/protobuf/protoc-gen-go
    
    //helloworld.proto
    
    // The request message containing the user's name.
    message HelloRequest {
      string name = 1;
    }
    
    // The response message containing the greetings
    message HelloReply {
      string message = 1;
    }
    
    service Greeter {
      // 定义sayhello方法
      rpc SayHello (HelloRequest) returns (HelloReply) {}
    }
    
    
    protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
    
    // server实现
    
    import (
    	"context"
    	"log"
    	"net"
    
    	"google.golang.org/grpc"
    	pb "google.golang.org/grpc/examples/helloworld/helloworld"
    )
    
    const (
    	port = ":50051"
    )
    
    // server is used to implement helloworld.GreeterServer.
    type server struct{}
    
    // SayHello implements helloworld.GreeterServer
    func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
    	log.Printf("Received: %v", in.GetName())
    	return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
    }
    
    func main() {
    	lis, err := net.Listen("tcp", port)
    	if err != nil {
    		log.Fatalf("failed to listen: %v", err)
    	}
    	s := grpc.NewServer()
    	pb.RegisterGreeterServer(s, &server{})
    	if err := s.Serve(lis); err != nil {
    		log.Fatalf("failed to serve: %v", err)
    	}
    }
    
    // client实现
    
    import (
    	"context"
    	"log"
    	"os"
    	"time"
    
    	"google.golang.org/grpc"
    	pb "google.golang.org/grpc/examples/helloworld/helloworld"
    )
    
    const (
    	address     = "localhost:50051"
    	defaultName = "world"
    )
    
    func main() {
    	// Set up a connection to the server.
    	conn, err := grpc.Dial(address, grpc.WithInsecure())
    	if err != nil {
    		log.Fatalf("did not connect: %v", err)
    	}
    	defer conn.Close()
    	c := pb.NewGreeterClient(conn)
    
    	// Contact the server and print out its response.
    	name := defaultName
    	if len(os.Args) > 1 {
    		name = os.Args[1]
    	}
    	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    	defer cancel()
    	r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
    	if err != nil {
    		log.Fatalf("could not greet: %v", err)
    	}
    	log.Printf("Greeting: %s", r.GetMessage())
    }
    
  • 相关阅读:
    Excel表导入数据库时带小数点的数据会变成科学计数样式的解决方法
    C# 具有合计行的DataGridViewNiceDataGridView1.0
    nginx 【使用echo调试】【地址复写】
    ts 【申明文件】
    createreactapp 【引入ui框架样式,全局样式被处理成模块化样式处理方法】
    node 【node服务器搭建1:安转node 和pm2】
    http 【前后端缓存】【nginx配合缓存】
    nginx 【匹配规则】【开启gzip压缩】
    nginx【nginx配置】
    react 【useMome、useCallback原理详解】
  • 原文地址:https://www.cnblogs.com/iiiiiher/p/12076616.html
Copyright © 2020-2023  润新知