[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)和传 ...
随机推荐
- 3.Java集合-HashSet实现原理及源码分析
一.HashSet概述: HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持,它不保证set的迭代顺序很久不变.此类允许使用null元素 二.HashSet的实现: 对于Ha ...
- 阿里P7级教你如何在Spring Boot应用程序中使用Redis
在Spring Boot应用程序中使用Redis缓存的步骤: 1.要获得Redis连接,我们可以使用Lettuce或Jedis客户端库,Spring Boot 2.0启动程序spring-boot-s ...
- MySQL数据库的基本认识与操作
Mysql是一个数据库,但是我们安装的mysql数据库服务,服务就会有状态,启动,停止,重启.我们使用mysql必须保证mysql启动. 使用mysql数据库需要连接数据库 Mysql -u -p - ...
- Python使用selenium模拟点击(二)
本篇文章是接着第一篇文章讲的 具体可看第一篇:https://www.cnblogs.com/whatarey/p/10477754.html 要实现功能>搜索完毕,自动点击 这个功能做的停操蛋 ...
- RT-Thread--线程管理
线程管理的功能特点 RT-Thread系统中线程是调度的最小单位: 线程分为:系统线程和用户线程,系统线程是由 RT-Thread 内核创建的线程,用户线程是由应用程序创建的线程,这两类线程都会从内核 ...
- win10激活密钥
专业版:W269N-WFGWX-YVC9B-4J6C9-T83GX 企业版:NPPR9-FWDCX-D2C8J-H872K-2YT43 家庭版:TX9XD-98N7V-6WMQ6-BX7FG-H8Q9 ...
- Ubuntu系统---系统驱动丢失、Kernel内核卸载、禁止更新
Ubuntu系统---系统驱动丢失.Kernel内核卸载.禁止更新 一早开机发现,ubuntu字体异常,字体很大,直接反应是驱动坏了.一查,确实丢失英伟达驱动,为什么呢?莫名的消失.想知道:1.英伟达 ...
- 线程池中 submit()和 execute()方法有什么区别?(未完成)
线程池中 submit()和 execute()方法有什么区别?(未完成)
- python3 多线程和多进程
一.线程和进程 1.操作系统中,线程是CPU调度和分派的基本单位,线程依存于程序中 2.操作系统中,进程是系统进行资源分配和调度的一个基本单位,一个程序至少有一个进程 3.一个进程由至少一个线程组成, ...
- Oracle中统计block空闲情况的一个SQL语句
此SQL来自网络,地址见具体内容.介绍表空间回收原理的文章参考此链接: https://oracle-base.com/articles/misc/reclaiming-unused-space#sh ...