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. pdfmake.js使用及其源码分析

    公司项目在需要将页面的文本导出成DPF,和支持打印时,一直没有做过这样的功能,花了一点时间将其做了出来,并且本着开源的思想和技术分享的目的,将自己的编码经验分享给大家,希望对大家有用. 现在是有一个文 ...

  2. Oracle帐户被锁了,如何解锁

    原文链接:https://jingyan.baidu.com/article/25648fc144b76b9191fd0087.html 背景:Oracle帐户在密码被连续输入错误3次的情况下就会锁定 ...

  3. Python 字典(Dictionary) copy()方法

    描述 Python 字典(Dictionary) copy() 函数返回一个字典的浅复制.高佣联盟 www.cgewang.com 语法 copy()方法语法: dict.copy() 参数 NA. ...

  4. PHP utf8_encode() 函数

    定义和用法 utf8_encode() 函数把 ISO-8859-1 字符串编码为 UTF-8.高佣联盟 www.cgewang.com Unicode 是全球标准,已经发展到能够通过每个字符/符号的 ...

  5. C/C++编程笔记:C++入门知识丨认识C++的函数和对象

    一. 本篇要学习的内容和知识结构概览 二. 知识点逐条分析 1. 混合型语言 C++源文件的文件扩展名为.cpp, 也就是c plus plus的简写, 在该文件里有且只能有一个名为main的主函数, ...

  6. 4.2 省选模拟赛 旅行路线 广义SAM

    \(n\leq 100000\) 题目上求出 多少条本质不同的路线. 首先定义了 相似的城市为度数相同的城市. 还定义了两条路线相同当且仅当长度相同 且对应位置的城市都是相似的. 考虑这张图的形态 n ...

  7. setOff与scrollTop区别

    1.offsetTop     : 当前对象到其上级层顶部的距离. 不能对其进行赋值.设置对象到页面顶部的距离请用style.top属性. 2.offsetLeft    : 当前对象到其上级层左边的 ...

  8. JAVA的基本程序设计结构(下)

    字符串 Java没有内置的字符串类型,而是在标准Java类库中提供了一个预定义类,叫做 String. String e=""; //an empty String String ...

  9. iptables看门狗

    近来业内很多服务器因redis造成服务器被黑,这个攻击的防范重点在于防火墙!! 有时为了方便我们可能会将iptables临时关闭,方便完倘若忘记把它打开,黑客大摇大摆就走进来. 这时候,我们需要条看门 ...

  10. Django中信号signal针对model的使用

    Django中实现对数据库操作的记录除了使用[开源插件]还可以使用信号signal独立实现 信号机制-观察者模式-发布与订阅:signal - 配置 # 文件路径:Django/myapps/__in ...