1,关于grpc-go


golang 能够能够做grpc的服务端和client。

官网的文档:

http://www.grpc.io/docs/quickstart/go.html

https://github.com/grpc/grpc-go

和之前写的java的grpcclient调用同样。也须要使用protobuf的配置文件。

可是golang以下的类库很的简单。并且golang的性能也很强悍呢。

有些简单的业务逻辑真的能够使用golang进行开发。

性能强悍并且。消耗的资源也很小。

java感觉上已经很的臃肿了。

项目已经上传到github上面了。

https://github.com/freewebsys/grpc-go-demo

2,生成代码


定义proto文件:

syntax = "proto3";
package helloworld; // The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
} // The request message containing the user's name.
message HelloRequest {
string name = 1;
} // The response message containing the greetings
message HelloReply {
string message = 1;
}

3,生成代码,服务端,client调用

cd src/helloworld

protoc -I ./ helloworld.proto –go_out=plugins=grpc:.

会生成一个go的helloworld.pb.go 文件。里面包含了grpc的远程调用和protobuf的序列化。

server.go

package main

import (
"log"
"net"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "github.com/freewebsys/grpc-go-demo/src/helloworld"
"google.golang.org/grpc/reflection"
"fmt"
) 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) {
fmt.Println("######### get client request name :"+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)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
// Register reflection service on gRPC server.
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}

client.go

package main

import (
"log"
"os"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "github.com/freewebsys/grpc-go-demo/src/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]
}
r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("####### get server Greeting response: %s", r.Message)
}

4,执行服务端&client代码


go run server/main.go

go run clinet/main.go

同一时候,能够使用java的client和服务端 <<===>> go的服务端client

相互调用。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZnJlZXdlYnN5cw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" title="">

5,总结


本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/59483427 未经博主同意不得转载。

博主地址是:http://blog.csdn.net/freewebsys

grpc 服务的远程调用还是很的简单的。

可是这个仅仅是一个helloworld ,真正要在企业内部使用的时候还须要一个注冊中心。

管理全部的服务。

初步计划使用 consul 存储数据。

由于consul 上面集成了许多的好东西。还有个简单的可视化的界面。

比etcd功能多些。

可是性能上面差一点。只是也很强悍了。

企业内部使用的注冊中心。已经足够了。

grpc(3):使用 golang 开发 grpc 服务端和client的更多相关文章

  1. 十分钟学会Golang开发gRPC服务

    gRPC是Google发起的一个开源RPC框架,使用HTTP/2传输协议,使用Protocol Buffers编码协议,相比RESTful框架的程序性能提高不少,而且当前流行的编程语言基本都已经支持. ...

  2. Delphi XE5通过WebService开发Web服务端和手机客户端

    Delphi XE5通过WebService开发Web服务端和手机客户端介绍 我们开发一个三层的android程序 建立一个webservices  stand-alone vcl applicati ...

  3. 项目二:企业级java电商网站开发(服务端)

    声明:项目源于网络,支持正版教程,学习使用,仅记录在此 项目介绍 企业级java电商网站开发(服务端),模块划分:用户管理,商品管理,商品品类管理,订单管理,订单详情管理,购物车管理,收货地址管理,支 ...

  4. [并发并行]_[线程模型]_[Pthread线程使用模型之三 客户端/服务端模型(Client/Server]

    Pthread线程使用模型之三 客户端/服务端模型(Client/Server) 场景 1.在客户端/服务端模型时,客户端向服务端请求一些数据集的操作. 服务端执行执行操作独立的(多进程或跨网络)– ...

  5. WCF服务端调用client.

    wcf服务端 1,新建一个"windows窗口程序"名称为WCFServer2. 2.然后加入一个"WCF服务"名称为Service1. 详细步骤为:解决方式试 ...

  6. gprc-java与golang分别实现服务端,客户端,跨语言通信(二.golang实现)

    1.编译器protoc, 下载地址:https://github.com/protocolbuffers/protobuf/releases  (下载对应的版本, 解压后放到go的bin中) 2.安装 ...

  7. gprc-java与golang分别实现服务端,客户端,跨语言通信(一.java实现)

    1.在pom中引入 <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty< ...

  8. cocos2d-x开发:服务端基础库封装

    元旦前面几天都在忙着面试,随后的几天也就一直在做服务端基础库开发方面的工作.对于服务端开发,是很久之前的事情了.那时候我还在大学读书,一直都是在倒腾服务端开发方面的东西,毕业后参加公司工作就是一直从事 ...

  9. iOS推送小结(证书的生成、客户端的开发、服务端的开发)

    1.推送过程简介 1.1.App启动过程中,使用UIApplication::registerForRemoteNotificationTypes函数与苹果的APNS服务器通信,发出注册远程推送的申请 ...

随机推荐

  1. shell命令行混合进制计算器smartbc

    需要简单的计算的时候,不想用GUI的计算器,能在shell下直接计算就最好了 查了下,有个东西叫 bc,  具体的使用就不赘述了,可以运行bc,然后进去计算,也可以echo传递过去,大概是像这样 ec ...

  2. UVALIVE 3891 The Teacher's Side of Math

    One of the tasks students routinely carry out in their mathematics classes is to solve a polynomial ...

  3. krpano--控制热点跳转到场景的指定视角

    krpano课堂(肥宗) · 2015-07-13 19:32 有这么一种情况,假设我们用三个场景,这三个场景恰好是一条街道的同一方向的三个拍摄点.如上图. 我们可以通过设置A.B.C三个场景中的vi ...

  4. SSH Secure Shell 无法登录:server responded "algorithm negotiation failed”

    SSH Secure Shell Client 连接 ubuntu系统报错 修改ssh的配置文件 /etc/ssh/sshd_config在配置文件中添加: Ciphers aes128-cbc,ae ...

  5. Delphi - 字符串 详解

    来自:http://www.cnblogs.com/huangjacky/archive/2009/12/10/1620950.html ------------------------------- ...

  6. k8s的Rolling Update(滚动更新应用)

    滚动更新是一次只更新一小部分副本,成功后,再更新更多的副本,最终完成所有副本的更新.滚动更新的最大的好处是零停机,整个更新过程始终有副本在运行,从而保证了业务的连续性. 下面我们部署三副本应用: 初始 ...

  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统

    http://www.tuicool.com/articles/NfyqQr 本节主要知识点是easyui 的手风琴加树结构做菜单导航 有园友抱怨原来菜单非常难看,但是基于原有树形无限级别的设计,没有 ...

  8. HDU 多校1.10

  9. golang笔记:cookie

    在同一个问题上栽了两次,以后碰到cookie出问题多半都是因为这个. Request.Cookie(name)取Cookie的时候,返回值只有name和value cookie.go cookies ...

  10. POJ2342 Anniversary party(动态规划)(树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6635   Accepted: 3827 ...