详解Windows10+golang+gRPC环境搭建


当前第2页 返回上一页

以上依赖库都下载完成后后,重新安装gRPC:

1

2

3

4

5

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src

$ go install google.golang.org/grpc/

 

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src

$

可见已经没有错误了,也没啥输出。。。

验证gRPC是否OK
启两个bash窗口,分别执行如下指令:

1

go run google.golang.org/grpc/examples/helloworld/greeter_server/main.go

1

go run google.golang.org/grpc/examples/helloworld/greeter_client/main.go

db85cee7a7d667411407846f1174860.png

如图可以看到服务端收到了客户端发送的消息

3、编译rpc

对于编写好的proto文件,需要经过编译才能变成.go文件,例如:
$GOPATHgoogle.golang.orggrpcexampleshelloworldhelloworld目录中有如下文件:

1

2

3

4

5

6

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)

$ ll

total 16

-rw-r--r-- 1 ASUS 197121 4938 6月   1 21:46 helloworld.pb.go

-rw-r--r-- 1 ASUS 197121 1208 6月   1 21:46 helloworld.proto

-rw-r--r-- 1 ASUS 197121 2823 6月   1 21:46 helloworld_grpc.pb.go

我们先将.go文件备份一下

1

2

3

4

5

6

7

8

9

10

11

12

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)

$ mv helloworld.pb.go helloworld.pb.go.bak

 

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)

$ mv helloworld_grpc.pb.go helloworld_grpc.pb.go.bak

 

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)

$ ll

total 16

-rw-r--r-- 1 ASUS 197121 4938 6月   1 21:46 helloworld.pb.go.bak

-rw-r--r-- 1 ASUS 197121 1208 6月   1 21:46 helloworld.proto

-rw-r--r-- 1 ASUS 197121 2823 6月   1 21:46 helloworld_grpc.pb.go.bak

然后执行:

1

2

3

4

5

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)

$ protoc --go_out=plugins=grpc:. helloworld.proto

'protoc-gen-go' ????????????????????????е????

?????????????

--go_out: protoc-gen-go: Plugin failed with status code 1.

发现有错误,需要安装protoc-gen-go
执行如下命令安装:

1

2

3

4

5

6

7

8

9

10

11

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src

$ go install github.com/golang/protobuf/protoc-gen-go/

 

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src

$ cd ../bin

 

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/bin

$ ll

total 11852

-rwxr-xr-x 1 ASUS 197121 3702272 5月  27 07:06 protoc.exe*

-rwxr-xr-x 1 ASUS 197121 8431104 6月   1 23:13 protoc-gen-go.exe*

安装完成后,在$GOPATH/bin目录下会生成protoc-gen-go.exe文件
然后再执行编译proto文件:
会生成一个helloworld.pb.go的文件

总结

有几个库,需要了解下:

1、https://github.com/protocolbuffers/protobuf
这个是google开源的protobuf源码库,这个库里面包含了常用的各种语言实现protobuf的源码

2、https://github.com/golang/protobuf
这个库是golang的protobuf开源库,查看这个库的README.md可以发现,这个库被google.golang.org/protobuf替代了,点开这个链接可以发现,这个库对应的源码git仓库为:
https://github.com/protocolbuffers/protobuf-go

问题:

目前看开源社区,github的protobuf会逐渐被google.golang.org的库替代,但是当前插件还是得用github的protoc-gen-go,
如果用google.golang.org/protobuf/cmd/protoc-gen-go的话(即go install google.golang.org/protobuf/cmd/protoc-gen-go,这个同样会在$GOPATH/bin目录下生成protoc-gen-go.exe),会导致如下错误

1

2

3

4

5

6

7

8

9

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)

$ protoc --go_out=plugins=grpc:. helloworld.proto

--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC

 

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src/google.golang.org/grpc/examples/helloworld/helloworld (master)

$ protoc --go-grpc_out=. helloworld.proto

'protoc-gen-go-grpc' ????????????????????????е????

?????????????

--go-grpc_out: protoc-gen-go-grpc: Plugin failed with status code 1.

后面会有单独的protoc-gen-go-grpc来生成grpc接口,但是这块还在代码review阶段,待发布。。。

以上就是详解Windows10+golang+gRPC环境搭建的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

关于 golang 协程调度

搭建coolpy+mqtt 服务

手撸golang go与微服务 saga模式之8 集成测试

kubernetes-helm详细介绍及使用

在 docker 下 golang 的 gomaxprocs 初始化混乱问题

golang 默认几核运行

golang中的闭包

golang cap是什么意思

golang面向对象编程之构造函数【struct&new】

golang中的channel通道

更多相关阅读请进入《rpc》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...