gprc

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值