go grpc遠端介面呼叫實現

2022-03-12 15:18:06 字數 1968 閱讀 6563

// grpc序列化/反序列化成對應語言的物件

// 1.寫idl(資料型別+方法)

// 2.生成對應語言的序列化/反序列化**

// 3.方法需要自己實現

// 環境(將gopath/bin加入path)

//安裝grpc引擎

go get -u google.golang.org/grpc

//安裝grpc-go外掛程式(適配go語言)

}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) , nil

}func main()

s := grpc.newserver()

pb.registergreeterserver(s, &server{})

if err := s.serve(lis); err != nil

}

// 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()

defer conn.close()

c := pb.newgreeterclient(conn)

// contact the server and print out its response.

name := defaultname

if len(os.args) > 1

ctx, cancel := context.withtimeout(context.background(), time.second)

defer cancel()

r, err := c.sayhello(ctx, &pb.hellorequest)

if err != nil

log.printf("greeting: %s", r.getmessage())

}

python 實現呼叫遠端介面

在python中我們可以使用requests模組來實現呼叫遠端介面 一 安裝requests模組 pip install requests二 使用requests模組實現get方式呼叫遠端介面 使用get方式呼叫遠端介面主要使用了requests模組的get方法 requests.get get方法...

Python呼叫遠端Socket介面

web應用通訊通常都喜歡用http介面,但不排除直接socket通訊的情況。socket除了server端構建麻煩些 需要考慮很多實際情況 對於呼叫者來說構建個client端其實不比http麻煩多少。usr bin env python coding utf 8 auther linvo impor...

python如何呼叫 遠端介面

在python中我們可以使用requests模組來實現呼叫遠端介面 一 安裝requests模組 pip install requests二 使用requests模組實現get方式呼叫遠端介面 使用get方式呼叫遠端介面主要使用了requests模組的get方法 requests.get get方法...