Go:grpc
一、grpc安装
将 https://github.com/google/go-genproto 修改文件名放到 $GOPATH/src/google.golang.org/genproto
将 https://github.com/grpc/grpc-go 修改文件名放到 $GOPATH/src/google.golang.org/grpc
将 https://github.com/golang/text 放到 $GOPATH/src/golang.org/x/text
将 https://github.com/golang/net 放到 $GOPATH/src/golang.org/x/net 然后
cd $GOPATH/src/
go install google.golang.org/grpc
PS:protobuf的安装不做介绍。
二、grpc的helloworld示例
服务端:
package main import (
"context"
"log"
"net" "google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
) const (
PORT = ":8080"
) type server struct{} // SayHello实现了helloworld.pb.go中的GreeterServer接口
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.Name)
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
} func main() {
// 监听
lis, err := net.Listen("tcp", PORT)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
// new服务对象
s := grpc.NewServer()
// 注册服务
pb.RegisterGreeterServer(s, &server{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
客户端:
package main import (
"context"
"log"
"time" "google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
) const (
ADDR = "localhost:8080"
NAME = "World"
) func main() {
// 建立与服务器的连接
conn, err := grpc.Dial(ADDR, grpc.WithInsecure())
if err != nil {
log.Fatalf("连接失败: %v", err)
}
defer conn.Close() // 调用proto的函数创建客户端连接句柄
c := pb.NewGreeterClient(conn) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel() // 调用proto的SayHello函数
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: NAME})
if err != nil {
log.Fatalf("调用SayHello失败: %v", err)
}
log.Println(r.Message)
}
Go:grpc的更多相关文章
- 基于HTTP/2和protobuf的RPC框架:GRPC
谷歌发布的首款基于HTTP/2和protobuf的RPC框架:GRPC Google 刚刚开源了grpc, 一个基于HTTP2 和 Protobuf 的高性能.开源.通用的RPC框架.Protobu ...
- 带入gRPC:gRPC Streaming, Client and Server
带入gRPC:gRPC Streaming, Client and Server 原文地址:带入gRPC:gRPC Streaming, Client and Server 前言 本章节将介绍 gRP ...
- 带入gRPC:gRPC Deadlines
带入gRPC:gRPC Deadlines 原文地址:带入gRPC:gRPC Deadlines项目地址:https://github.com/EDDYCJY/go... 前言 在前面的章节中,已经介 ...
- 跟我一起学Go系列:gRPC 入门必备
RPC 的定义这里就不再说,看文章的同学都是成熟的开发.gRPC 是 Google 开源的高性能跨语言的 RPC 方案,该框架的作者 Louis Ryan 阐述了设计这款框架的动机,有兴趣的同学可以看 ...
- 跟我一起学 Go 系列:gRPC 拦截器
Go gRPC 学习系列: 跟我一起学Go系列:gRPC 入门必备 第一篇内容我们已经基本了解到 gRPC 如何使用 .对应的三种流模式.现在已经可以让服务端和客户端互相发送消息.本篇仍然讲解功能性的 ...
- grpc-gateway:grpc对外提供http服务的解决方案
我所在公司的项目是采用基于Restful的微服务架构,随着微服务之间的沟通越来越频繁,就希望可以做成用rpc来做内部的通讯,对外依然用Restful.于是就想到了google的grpc. 使用grpc ...
- ScalaPB(3): gRPC streaming
接着上期讨论的gRPC unary服务我们跟着介绍gRPC streaming,包括: Server-Streaming, Client-Streaming及Bidirectional-Streami ...
- grpc-gateway:grpc转换为http协议对外提供服务
我所在公司的项目是采用基于Restful的微服务架构,随着微服务之间的沟通越来越频繁,就希望可以做成用rpc来做内部的通讯,对外依然用Restful.于是就想到了google的grpc. 使用grpc ...
- grpc:gRPC Concepts
本文介绍一些主要的gRPC概念. 服务定义 gRPC支持4种方法: 1.Unary RPCs where the client sends a single request to the server ...
随机推荐
- LQB2013A03振兴中华
最近状态出了点问题呜呜呜,可能是天有点热吧加上有一点点不太舒服,,,稳住啊! 明显一个递归(但是就是不会写) 递归:(一般这种找有多少个的题,返回值都是int) 首先找变化的东西当作参数.(本题是坐标 ...
- Python os.stat_float_times() 方法
概述 os.stat_float_times() 方法用于决定stat_result是否以float对象显示时间戳.高佣联盟 www.cgewang.com 语法 stat_float_times() ...
- PHP array_udiff_uassoc() 函数
实例 比较两个数组的键名和键值(使用用户自定义函数进行比较),并返回差集: <?phpfunction myfunction_key($a,$b){if ($a===$b){return 0;} ...
- PHP is_writable() 函数
定义和用法 is_writable() 函数检查指定的文件是否可写. 如果文件可写,该函数返回 TRUE. 语法 is_writable(file) 参数 描述 file 必需.规定要检查的文件. 提 ...
- 一站式搞定Ubuntu共享环境配置
1. 添加linux用户 安装的开发用的虚拟机,一般不直接使用root账户,会新建一个普通用户,然后在/etc/sudoers添加上sudo的权限即可. 使用如下命令: sudo adduser -- ...
- Spark Streaming——Spark第一代实时计算引擎
虽然SparkStreaming已经停止更新,Spark的重点也放到了 Structured Streaming ,但由于Spark版本过低或者其他技术选型问题,可能还是会选择SparkStreami ...
- Python 错误 异常
8 错误,调试和测试 8.1错误处理 所有的异常来自 BaseException 记录错误 : # err_logging.py import logging def foo(s): return 1 ...
- find the lowest number location
before #设定路径列表Path def find_path2(heightmap, x, y, water_level=557,path=[]): #global path #设定坐标 右0 左 ...
- 2020-07-02:在浏览器输入一个url后按回车,会发生什么?
福哥答案2020-07-02: 简单回答: 域名解析. 建立TCP连接. 请求. 处理. 响应. 释放TCP连接. 页面渲染. 中级回答: 域名解析 浏览器DNS缓存. 操作系统DNS缓存. 路由器缓 ...
- C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...