1.grpm 安装:

git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc

2.proto, protoc-gen-go 安装:

go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

3.protoc 安装:

git clone https://github.com/protocolbuffers/protobuf.git
cd $GOPATH/src/github.com/protocolbuffers/
./autogen.sh
./configure
make -j12
make install

4.go-genproto 安装(运行时需要的依赖,下载完移动到相应的目录里面)

git https://github.com/googleapis/go-genproto.git

5.protoc 查看版本:

[root@wangjq]# protoc --version
libprotoc 3.10.

6.目录结构:

[root@wangjq demo]# tree
.
├── client
│   └── main.go
├── example
│   └── data.proto
└── server
└── main.go directories, files

7.data.proto 内容如下

syntax = "proto3"; //指定proto版本

package proto;

//定义请求结构
message HelloRequest{
string name=;
} //定义响应结构
message HelloReply{
string message=;
} //定义Hello服务
service Hello{
//定义服务中的方法
rpc SayHello(HelloRequest) returns (HelloReply){}
}

8.生成 data.pb.go 文件

protoc -I . --go_out=plugins=grpc:. ./data.proto 

9.服务端代码

package main

import (
pb "demo/example"
"fmt"
"golang.org/x/net/context"
"google.golang.org/grpc"
"net"
) const (
//gRPC服务地址
Address = "127.0.0.1:50052"
) //定义一个helloServer并实现约定的接口
type helloService struct{} func (h helloService) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
resp := new(pb.HelloReply)
resp.Message = "hello" + in.Name + "."
return resp, nil
} var HelloServer = helloService{} func main() {
listen, err := net.Listen("tcp", Address)
if err != nil {
fmt.Printf("failed to listen:%v", err)
} //实现gRPC Server
s := grpc.NewServer()
//注册helloServer为客户端提供服务
pb.RegisterHelloServer(s, HelloServer) //内部调用了s.RegisterServer()
fmt.Println("Listen on" + Address) s.Serve(listen)
}

10.客户端代码

package main

import (
pb "demo/example"
"fmt"
"golang.org/x/net/context"
"google.golang.org/grpc"
) const (
Address = "127.0.0.1:50052"
) func main() {
//连接gRPC服务器
conn, err := grpc.Dial(Address, grpc.WithInsecure())
if err != nil {
fmt.Println(err)
}
defer conn.Close() //初始化客户端
c := pb.NewHelloClient(conn) //调用方法
reqBody := new(pb.HelloRequest)
reqBody.Name = "gRPC"
r, err := c.SayHello(context.Background(), reqBody)
if err != nil {
fmt.Println(err)
}
fmt.Println(r.Message)
}

11.运行测试

服务端运行:
[root@wangjq demo]# go run server/main.go
Listen on127.0.0.: 客户端运行:
[root@wangjq demo]# go run client/main.go
hellogRPC.

golang grpc demo的更多相关文章

  1. golang 并发demo 写入 redis

    原文链接:golang 并发demo 写入 redis 源代码: package main import ( "fmt" "runtime" "str ...

  2. Golang gRPC调试工具

    目录 Golang gRPC调试工具 1. 命令行工具 grpcurl 1.1 安装 1.2 验证 1.3 注册反射 1.4 使用示例 2. web调试工具grpcui 2.1 安装 2.2 验证 2 ...

  3. golang gRPC初探

    gRPC使用protocol buffers作为Interface Definition Language (IDL). gRPC的底层信息交互格式也使用的是protocol buffers. 默认情 ...

  4. Golang gRPC学习(04): Deadlines超时限制

    为什么要使用Deadlines 当我们使用gRPC时,gRPC库关系的是连接,序列化,反序列化和超时执行.Deadlines 允许gRPC客户端设置自己等待多长时间来完成rpc操作,直到出现这个错误 ...

  5. Golang gRPC 示例

    1.安装gRPC runtime go get google.golang.org/grpc 为了自动生成Golang的gRPC代码,需要安装protocal buffers compiler以及对应 ...

  6. Golang gRPC 和 gRPC-gateway 结合使用

    一.安装 go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway go get -u github.com/g ...

  7. Golang gRPC 使用

    一.概念 1.gRPC默认使用protocol buffers,这是google开源的一套成熟的结构数据序列化机制(当然也可以使用其他数据格式如JSON),可以用proto files创建gRPC服务 ...

  8. [golang grpc] 框架介绍

    官方网站 http://www.grpc.io/ http://www.grpc.io/docs/quickstart/go.html grpc安装 • go安装 目前grpc需要go 1.5以上版本 ...

  9. win7环境下,golang thrift demo代码编译不通过

    用官方的教程代码:http://thrift.apache.org/tutorial/go 用网友提供的代码:Golang RPC 之 Thrift 都出现如下情况 状况1: 编辑器中就会提醒 Can ...

随机推荐

  1. three.js 数学方法之Matrix4

    今天郭先生说一说three.js中的Matrix4,相较于Matrix3来说,Matrix4和three.js联系的更紧密,因为在4x4矩阵最常用的用法是作为一个变换矩阵.这使得表示三维空间中的一个点 ...

  2. dp入门例题(1)

    按摩师问题 https://leetcode-cn.com/problems/the-masseuse-lcci/ (找好状态转移方程) 今天只和昨天的状态相关,依然是分类讨论: 今天不接受预约:或者 ...

  3. Seaborn基础1

    import seaborn as sns import numpy as np import matplotlib.pyplot as plt # # 折线图 def sinplot(flip = ...

  4. Django学习路

    1.脚本不能随便运行,没准 linux 运行完就上不去了 2.pip 在 linux 上 写 pip3 同理 python 写为 python3 3.在 pycharm 上安装库之后,在命令提示符中依 ...

  5. 《分享》Graphql入门与实践

    最近项目用到了graphql,学习了一些并在公司做了一个小分享,希望对你有帮助 一.介绍 Graphql是一种面向数据的API查询语言 Graphql给前端提供一种强力的查询工具,我们可以根据自己定义 ...

  6. nodeJs + js 大文件分片上传

    简单的文件上传 一.准备文件上传的条件: 1.安装nodejs环境 2.安装vue环境 3.验证环境是否安装成功 二.实现上传步骤 1.前端部分使用 vue-cli 脚手架,搭建一个 demo 版本, ...

  7. 和菜鸟一起学linux之DBUS基础学习记录(转)

    转自:https://www.cnblogs.com/wuyida/p/6299998.html D-Bus三层架构 D-Bus是一个为应用程序间通信的消息总线系统, 用于进程之间的通信.它是个3层架 ...

  8. Python内置OS模块用法详解

    大家好,从今天起早起Python将持续更新由小甜同学从初学者的角度学习Python的笔记,其特点就是全文大多由新手易理解的代码与注释及动态演示.刚入门的读者千万不要错过! 很多人学习python,不知 ...

  9. 事件 - DOM编程

    何为 DOM 事件,HTML DOM 使JavaScript 有能力对 HTML 事件做出反应. 1. 事件流 一个 DOM 事件可以分为捕获过程.触发过程.冒泡过程. 下面一个<a>元素 ...

  10. 15、Java中级进阶 面向对象 继承

    1.何为面向对象 其本质是以建立模型体现出来的抽象思维过程和面向对象的方法(百度百科)是一种编程思维,也是一种思考问题的方式 如何建立面向对象的思维呢?1.先整体,再局部2.先抽象,再具体3.能做什么 ...