1、执行 go get google.golang.org/grpc 报错
解决办法:
1.cd到GOPATH下执行git clone https://github.com/grpc/grpc-go grpc
2.然后根据报错clone相应的缺少的库:
git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text
示例代码:
helloworld.proto
syntax = "proto3";
package helloworld;
service Greeter{
//sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply){}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
编译: protoc -I . Hell world.prot —go_out=plugins=grpc:.
service.go
package main
import (
"context"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"net"
pb "test/protobuf"
)
type server struct{}
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "hello " + in.Name}, nil
}
func main() {
lis, err := net.Listen("tcp", ":30051")
if err != nil {
fmt.Println("failed to listen: %v", err)
return
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
reflection.Register(s)
if err := s.Serve(lis); err != nil {
fmt.Println("failed to server: %v", err)
}
}
client.go
package main
import (
"context"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
pb "test/protobuf"
"time"
)
type GrpcConn struct {
conn *grpc.ClientConn
state connectivity.State
addr string
request chan string
result chan string
}
func newGrpcConn(addr string, request chan string, result chan string) *GrpcConn {
conn, err := grpc.Dial("127.0.0.1:30051", grpc.WithInsecure())
if err != nil {
fmt.Printf("did not connect : %v", err)
} else {
fmt.Println("dial success")
}
return &GrpcConn{
addr: addr,
conn: conn,
state: connectivity.Idle,
request: request,
result: result,
}
}
func (this *GrpcConn) ConnState() {
if this.conn != nil {
this.conn.WaitForStateChange(context.Background(), this.state)
this.state = this.conn.GetState()
}
}
func (this *GrpcConn) ReConnect() {
if this.conn != nil && this.conn.GetState() == connectivity.TransientFailure {
this.conn.Close()
conn, err := grpc.Dial(this.addr, grpc.WithInsecure())
if err != nil {
fmt.Printf("did not connect : %v", err)
} else {
this.conn = conn
}
}
}
func (conn *GrpcConn) IsAvailable() bool {
return conn.state == connectivity.Ready
}
func (this *GrpcConn) SendDta() {
go func() {
for {
this.ConnState()
}
}()
if !this.IsAvailable() {
time.Sleep(time.Second)
fmt.Println("cur state is ", this.conn.GetState())
this.ReConnect()
return
}
data := <-this.request
c := pb.NewGreeterClient(this.conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: data})
if err != nil {
fmt.Printf("could not greet: %v", err)
this.result <- err.Error()
return
}
this.result <- r.Message
}
func main() {
var request chan string = make(chan string, 100)
var result chan string = make(chan string, 100)
conn := newGrpcConn("127.0.0.1:30051", request, result)
go func() {
for {
conn.SendDta()
}
}()
go func() {
var index int
for {
request <- fmt.Sprintf("%d", index)
index++
time.Sleep(time.Second)
}
}()
for {
v := <-result
fmt.Println(" result ", v)
}
}