一、环境

jdk8+maven+IDEA

二、 我的理解+官方文档

1、官方文档:https://codelabs.developers.google.com/codelabs/cloud-grpc-java/index.html#2

2、我的理解:grpc是利用.proto文件用相应的语言编译生成相应的代码,这样就能实现不同语言平台之间的调用。最神奇的是仿佛客户端和服务器端调用的是相同的函数,在相同函数里面实现通信,相当的容易理解。

三、我的github+代码

1、github链接:https://github.com/cysisu/grpc-java.git

2、代码如下:

(1)pop.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>com.grpc.cube</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>io.grpc</groupId><artifactId>grpc-netty</artifactId><version>1.7.0</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-protobuf</artifactId><version>1.7.0</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-stub</artifactId><version>1.7.0</version></dependency></dependencies><build><extensions><extension><groupId>kr.motd.maven</groupId><artifactId>os-maven-plugin</artifactId><version>1.5.0.Final</version></extension></extensions><plugins><plugin><groupId>org.xolstice.maven.plugins</groupId><artifactId>protobuf-maven-plugin</artifactId><version>0.5.0</version><configuration><protocArtifact>com.google.protobuf:protoc:3.4.0:exe:${os.detected.classifier}</protocArtifact><pluginId>grpc-java</pluginId><pluginArtifact>io.grpc:protoc-gen-grpc-java:1.7.0:exe:${os.detected.classifier}</pluginArtifact></configuration><executions><execution><goals><goal>compile</goal><goal>compile-custom</goal></goals></execution></executions></plugin></plugins></build></project>

(2) 客户端:

package com.example.grpc;import io.grpc.*;public class Client
{public static void main( String[] args ) throws Exception{// Channel is the abstraction to connect to a service endpoint// Let's use plaintext communication because we don't have certsfinal ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:8080").usePlaintext(true).build();// It is up to the client to determine whether to block the call// Here we create a blocking stub, but an async stub,// or an async stub with Future are always possible.GreetingServiceGrpc.GreetingServiceBlockingStub stub = GreetingServiceGrpc.newBlockingStub(channel);GreetingServiceOuterClass.HelloRequest request =GreetingServiceOuterClass.HelloRequest.newBuilder().setName("Ray").build();// Finally, make the call using the stubGreetingServiceOuterClass.HelloResponse response =stub.greeting(request);System.out.println(response);// A Channel should be shutdown before stopping the process.channel.shutdownNow();}
}

(3)服务器端调用的函数:

package com.example.grpc;import io.grpc.stub.StreamObserver;public class GreetingServiceImpl extends GreetingServiceGrpc.GreetingServiceImplBase {public void greeting(GreetingServiceOuterClass.HelloRequest request,StreamObserver<GreetingServiceOuterClass.HelloResponse> responseObserver) {// HelloRequest has toString auto-generated.System.out.println(request);// You must use a builder to construct a new Protobuffer objectGreetingServiceOuterClass.HelloResponse response = GreetingServiceOuterClass.HelloResponse.newBuilder().setGreeting("Hello there, " + request.getName()).build();// Use responseObserver to send a single response backresponseObserver.onNext(response);// When you are done, you must call onCompleted.responseObserver.onCompleted();}
}

(5)服务器端:

package com.example.grpc;
import io.grpc.*;public class App
{public static void main( String[] args ) throws Exception{// Create a new server to listen on port 8080Server server = ServerBuilder.forPort(8080).addService(new GreetingServiceImpl()).build();// Start the serverserver.start();// Server threads are running in the background.System.out.println("Server started");// Don't exit the main thread. Wait until server is terminated.server.awaitTermination();}
}

(6)proto文件:

syntax = "proto3";
package com.example.grpc;// Request payload
message HelloRequest {// Each message attribute is strongly typed.// You also must assign a "tag" number.// Each tag number is unique within the message.string name = 1;// This defines a strongly typed list of Stringrepeated string hobbies = 2;// There are many more basics types, like Enum, Map// See https://developers.google.com/protocol-buffers/docs/proto3// for more information.
}message HelloResponse {string greeting = 1;
}// Defining a Service, a Service can have multiple RPC operations
service GreetingService {// Define a RPC operationrpc greeting(HelloRequest) returns (HelloResponse);
}

GRPC java实现demo相关推荐

  1. Java类Demo中存在_Java中的数据类型转换

    先来看一个题: Java类Demo中存在方法func0.func1.func2.func3和func4,请问该方法中,哪些是不合法的定义?( ) public class Demo{ float fu ...

  2. 海康威视(HIKVISION) JAVA SDK Demo成功运行

    海康威视(HIKVISION) JAVA SDK Demo成功运行 引子 下载 说明 代码编译 添加文件 问题 排查 总结 引子 调海康的JAVA SDK花费时间较长,所以记录下,为其他小伙伴填填坑, ...

  3. java用虹软人脸识别SDK实现人脸识别,运行ArcSoft的Java版本Demo出错,未检出人脸

    java.lang.UnsatisfiedLinkError: Can't load library: d:\face_libs\libarcsoft_ java用虹软人脸识别SDK实现人脸识别 网上 ...

  4. java用虹软人脸识别SDK实现人脸识别,运行ArcSoft的Java版本Demo出错,未检出人脸(已解决)

    java用虹软人脸识别SDK实现人脸识别,运行ArcSoft的Java版本Demo出错,未检出人脸问题已解决!!! 原因: 虹软人脸识别SDK版本问题! 下载的java版本的demo的SDK是arcs ...

  5. 微信公众号Java接入demo

    微信公众号Java接入demo 前不久买了一台服务,本来是用来当梯子用的,后来买了一个域名搭了一个博客网站,后来不怎么在上面写博客一直闲着,最近申请了一个微信公众号就想着弄点什么玩玩.周末没事就鼓捣了 ...

  6. Java类Demo中存在方法func1、func2、func3和func4,请问该方法中,哪些是不合法的定义?( )

    Java类Demo中存在方法func1.func2.func3和func4,请问该方法中,哪些是不合法的定义?( ) public class Demo{float func1(){int i=1;r ...

  7. grpc java 泛型_gRPC中Any类型的使用(Java和NodeJs端)

    工作中要把原来Java服务端基于SpringMVC的服务改为使用gRPC直接调用.由于原Service的返回值为动态的Map类型,key值不确定,且value的类型不唯一,因此使用了protobuf ...

  8. grpc java 基础教程

    1 RPC 框架原理 RPC 框架的目标就是让远程服务调用更加简单.透明,RPC 框架负责屏蔽底层的传输方式(TCP 或者 UDP).序列化方式(XML/Json/ 二进制)和通信细节.服务调用者可以 ...

  9. java junit 运行_appium+java+junit demo运行

    对java熟悉一些,所以想用java把appium给做起来.今天用myeclipse给环境和Demo弄了一下,网上没有一篇全套资料的. 各块环境的搭建: 1.安装appium客户端,省略下载和安装步骤 ...

最新文章

  1. Python-高阶函数
  2. java mysql安装教_大学java教案之MySQL安装图解
  3. C#通过WMI的wind32 的API函数实现msinfo32的本地和远程计算机的系统摘要信息查看功能...
  4. leetcode算法题--有效的括号
  5. ESP32 OTA升级策略
  6. 单链表反转的原理和python代码实现
  7. Python的运算符和编码
  8. java 对比度,java批改图片亮度对比度
  9. qtreewidget点击空白处时取消以选项_VUE+elementUI 点击页面空白处弹窗不隐藏
  10. linux服务器运维操作命令,Windows和Linux系统服务器运维基本操作指令
  11. 【翻译】Test-After Development is not Test-Driven Development
  12. java笔记:第6章 面向对象程序设计
  13. CC2530存储器映射
  14. python可视化界面开发实例-Python可视化界面编程入门
  15. 怎么查看Win7系统无线网络密码
  16. 2012服务器系统有什么版本的,Windows server 2012操作系统有哪几个版本
  17. 第一学期ACM之旅总结篇
  18. access to同义替换_雅思写作高频100必备词(附高级替换)
  19. 认证授权中解决AuthenticationManager 无法注入
  20. Java简繁体转换分享

热门文章

  1. LIstview滑动时不加载图片,停止时加载!
  2. Smali源代码分析教程(转)
  3. DELPHI listbox去除重复项
  4. 智能音箱AEC中的回采信号
  5. MTK 平台上如何给 camera 添加一种 preview size
  6. python用os模块自动打开软件_Python实现自动打开电脑应用的示例代码
  7. java如何保证redis设置过期时间的原子性_2020年4月Redis面试题和答案整理
  8. java B2B2C Springcloud电子商城系统-通过消息队列传输zipkin日志
  9. Hyperledger Fabric 1.2 --- Chaincode Operator 解读和测试(一)
  10. swift 基础学习之属性修饰符