[go]grpc远程接口调用实现
// grpc序列化/反序列化成对应语言的对象
// 1.写idl(数据类型+方法)
// 2.生成对应语言的序列化/反序列化代码
// 3.方法需要自己实现
// 环境(将gopath/bin加入path)
//安装grpc引擎
go get -u google.golang.org/grpc
//安装grpc-go插件(适配go语言)
go get -u github.com/golang/protobuf/protoc-gen-go
//helloworld.proto
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
service Greeter {
// 定义sayhello方法
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
// server实现
import (
"context"
"log"
"net"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
port = ":50051"
)
// server is used to implement helloworld.GreeterServer.
type server struct{}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
// client实现
import (
"context"
"log"
"os"
"time"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
address = "localhost:50051"
defaultName = "world"
)
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())
}
[go]grpc远程接口调用实现的更多相关文章
- SmartRoute之远程接口调用和负载
基于接口的调用远比基于基础消息交互来得更简单和便于维护,特别在业务展现上,接口作为业务表现更适合其便利性.为了让SmartRoute更适合业务应用集成,在新的一年开始SmartRoute集成了远程接口 ...
- HttpClient远程接口调用-实名认证
1.HttpClient远程接口调用 1)用户注册 注册按钮button提交表单时,要return false form表单 <!-- action="http://localhost ...
- HttpClient 远程接口调用方式
远程接口调用方式HttpClient 问题:现在我们已经开发好了接口了,那该如何调用这个接口呢? 答:使用Httpclient客户端. Httpclient简介 什么是httpclient Htt ...
- RMI(远程接口调用)
1. RMI的原理: RMI系统结构,在客户端和服务器端都有几层结构. 方法调用从客户对象经占位程序(Stub).远程引用层(Remote Reference Layer)和传输层(Transport ...
- 【RPC】远程接口调用实例 的几种方式比较
pring中,用JMS搞RPC时会用到: org.springframework.jms.remoting.JmsInvokerServiceExporter org.springframework. ...
- java之远程接口调用
一.通过地址栏传值 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...
- SpringBoot远程接口调用-RestTemplate使用
在web服务中,调度远程url是常见的使用场景,最初多采用原生的HttpClient,现采用Spring整合的RestTemplate工具类进行.实操如下: 1. 配置 主要用以配置远程链接的相关参数 ...
- 以前写的一段aop,远程接口调用的日志。
using System;using System.Collections.Generic;using System.Linq;using System.Text; using Microsoft.P ...
- Java RMI远程方法调用
RMI(远程接口调用) 1. RMI的原理: RMI系统结构,在客户端和服务器端都有几层结构. 方法调用从客户对象经占位程序(Stub).远程引用层(Remote Reference Layer)和传 ...
随机推荐
- 【vue开发】vue指令Vue.directive使用教程
1.指令的注册 指令跟组件一样需要注册才能使用,同样有两种方式,一种是全局注册: ? 1 2 3 4 5 Vue.directive('dirName',function(){ //定义指令 ...
- chkconfig 系统服务管理
chkconfig --list 显示当前运行的所有服务/etc/inittabnetstat -antl all n端口 t tcp协议 l listen状态chkconfig --add ...
- C++ 正则查找
#include <iostream> #include <regex> using namespace std; int main() { string str; getli ...
- SQL Join 理解
对各种连接的理解,可以参照文章. 下面是对连接结果表条数统计的思考:假设有主表Ta有5条记录,从表Tb有4条记录 Ta corss join Tb, 结果为2表做笛卡尔积,5*4=20条 /*下面其它 ...
- 大数据之路week04--day03(网络编程)
哎,怎么感觉自己变得懒了起来,更新博客的频率变得慢了起来,可能是因为最近得知识开始变得杂变得难了起来,之前在上课的时候,也没有好好听这一方面的知识,所以,现在可以说是在学的新的知识,要先去把新的知识思 ...
- TODO 疑问:java中的工厂类,在实际项目中如何运用
正在读core of java,工厂类一直没搞懂.感觉和静态方法相类似,但是不知道是怎么运用在实际中. 20190711: 读了设计模式,里面给出的解读是这样的 Define an interface ...
- IDEA创建类似于Eclipse的source folder
1.新建普通文件夹目录directory 2.当前Module右键Open Mudule Settings(F12) 3.选中新建的文件夹并单击上面的Sources,看到文件夹颜色变化即成功.
- MNIST 数据集介绍
在学习机器学习的时候,首要的任务的就是准备一份通用的数据集,方便与其他的算法进行比较. MNIST数据集是一个手写数字数据集,每一张图片都是0到9中的单个数字,比如下面几个: MNIST数据库 ...
- Shell 05 Sed
一.基本用方法 1.sed文本处理工具的用法 用法1:前置命令 | sed [选项] '条件指令' 用法2:sed [选项] '条件指令' 文件.. .. 注意:没有条件时候,默认所有条件, ...
- java中VO的使用(组成复杂的实体类)
https://blog.csdn.net/G0_hw/article/details/78326359