// 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远程接口调用实现的更多相关文章

  1. SmartRoute之远程接口调用和负载

    基于接口的调用远比基于基础消息交互来得更简单和便于维护,特别在业务展现上,接口作为业务表现更适合其便利性.为了让SmartRoute更适合业务应用集成,在新的一年开始SmartRoute集成了远程接口 ...

  2. HttpClient远程接口调用-实名认证

    1.HttpClient远程接口调用 1)用户注册 注册按钮button提交表单时,要return false form表单 <!-- action="http://localhost ...

  3. HttpClient 远程接口调用方式

    远程接口调用方式HttpClient 问题:现在我们已经开发好了接口了,那该如何调用这个接口呢? 答:使用Httpclient客户端.   Httpclient简介 什么是httpclient Htt ...

  4. RMI(远程接口调用)

    1. RMI的原理: RMI系统结构,在客户端和服务器端都有几层结构. 方法调用从客户对象经占位程序(Stub).远程引用层(Remote Reference Layer)和传输层(Transport ...

  5. 【RPC】远程接口调用实例 的几种方式比较

    pring中,用JMS搞RPC时会用到: org.springframework.jms.remoting.JmsInvokerServiceExporter org.springframework. ...

  6. java之远程接口调用

    一.通过地址栏传值 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

  7. SpringBoot远程接口调用-RestTemplate使用

    在web服务中,调度远程url是常见的使用场景,最初多采用原生的HttpClient,现采用Spring整合的RestTemplate工具类进行.实操如下: 1. 配置 主要用以配置远程链接的相关参数 ...

  8. 以前写的一段aop,远程接口调用的日志。

    using System;using System.Collections.Generic;using System.Linq;using System.Text; using Microsoft.P ...

  9. Java RMI远程方法调用

    RMI(远程接口调用) 1. RMI的原理: RMI系统结构,在客户端和服务器端都有几层结构. 方法调用从客户对象经占位程序(Stub).远程引用层(Remote Reference Layer)和传 ...

随机推荐

  1. Java程序猿跳槽应该学哪些方面的技术

    互联网产品.大型企业级项目常会用到的: 并发处理技术 具体到Java上通常是涉及java.util.concurrent.并发锁机制.NIO等方面,当然最近比较火爆的Netty框架也可以作为高并发处理 ...

  2. SVN版本控制—branches、trunk、tag篇

    新建资源仓库时,可选择默认创建三个文件夹.这三个文件夹分别是[trunk][branches][tags] [Trunk] 一般用于存放目前项目主线,也就是项目所有功能模块的集合体,一整个项目所有代码 ...

  3. centos7下安装zookeeper&zookeeper集群的搭建

    一.centos7下安装zookeeper 1.zookeeper 下载地址 https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/ 2.安装步骤 ...

  4. python-----将图片与标注的xml坐标水平翻转

    我们做机器学习的时候,总会用到很多训练集,然后我们的数据比较少的时候,就可以将图片翻转标注.代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- # ...

  5. [Leetcode]Rotated Sorted Array问题

    LeetCode上牵扯到Rotated Sorted Array问题一共有四题,主要是求旋转数组的固定值或者最小值,都是考察二分查找的相关知识.在做二分查找有关的题目时,需要特别注重边界条件和跳出条件 ...

  6. 浅谈linux用户与用户组的概念

    原文链接;http://linuxme.blog.51cto.com/1850814/347086 作者:linuxme1.用户 用户是能够获取系统资源的权限的集合. .linux用户组的分类: a. ...

  7. c#winform多线程感想

    我很菜所以好好学!!! 最近在做一个关于识别的项目,手动识别和自动识别,为了更好的保证自动识别不会引起界面的卡顿等现象,所以简单的学习了一下多线程,也只是入门但还是记录一下. 一.首先了解一下用多线程 ...

  8. 尤娜博客系统 Una

    站长资讯平台:Una [‘尤娜’] 只是一个项目代号,没有特殊含义.尤娜是站在巨人的肩膀上开发完成的博客系统,旨在为程序员提供一个极简的内容创作管理平台,尤娜100%开放源代码,如果您对她感兴趣,Fo ...

  9. pandas实现hive的lag和lead函数 以及 first_value和last_value函数

    lag和lead VS shift 该函数的格式如下: 第一个参数为列名, 第二个参数为往上第n行(可选,默认为1), 第三个参数为默认值(当往上第n行为NULL时候,取默认值,如不指定,则为NULL ...

  10. node.js中允许的app对象声明方式

    伪对象形式 app = function () { console.log("我是一个初始化的app对象"); }; app.get=function () { console.l ...