近一年来一直在用公司内wiki进行技术调研以及记录,后期有时间将逐步迁移至博客园

参考资料:

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

https://www.cnblogs.com/gutousu/p/9951956.html

可以一次性的在一个 .proto 文件中定义服务并使用任何支持它的语言去实现客户端和服务器,反过来,它们可以在各种环境中,从Google的服务器到你自己的平板电脑- gRPC 帮你解决了不同语言间通信的复杂性以及环境的不同.使用 protocol buffers 还能获得其他好处,包括高效的序列号,简单的 IDL 以及容易进行接口更新,无注册中心

依赖包

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-all</artifactId>
</dependency>

build

<build>

    <extensions>
<extension>
<!-- provides os.detected.classifier (i.e. linux-x86_64, osx-x86_64) property -->
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.1</version>
</extension>
</extensions> <plugins> <plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
</pluginArtifact>
<!-- proto文件目录 -->
<protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
</configuration>
<executions>
<execution>
<id>bean</id>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
<configuration>
<!-- 生成的Java文件目录 -->
<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>
</configuration>
</execution> <execution>
<id>grpc</id>
<goals>
<goal>compile-custom</goal>
<goal>test-compile-custom</goal>
</goals>
<configuration>
<!-- 生成的grpc-Java文件目录 -->
<outputDirectory>${project.build.directory}/generated-sources/protobuf/grpc-java
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

proto-demo

syntax = "proto2";

package java_test;

service TestService {
rpc method (Request) returns (Result) { }
} message Request {
optional string request1 = 1;
optional string request2 = 2;
} message Result {
optional string result1 = 1;
optional string result2 = 2;
}

编译项目,生成bean文件和通信文件

server端demo

package com.baidu.traffic.sc.test;

import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java_test.TestGrpc;
import java_test.TestServiceGrpc;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component; import java.io.IOException; /**
* Created by liushouyun on 2019-12-20 12:36.
*/ @Component
public class JavaGrpcServer extends TestServiceGrpc.TestServiceImplBase implements InitializingBean { @Override
public void method(TestGrpc.Request request, StreamObserver<TestGrpc.Result> responseObserver) {
TestGrpc.Result result = TestGrpc.Result.newBuilder().setResult1("结果1").setResult2("结果2不想给你").build();
responseObserver.onNext(result);
responseObserver.onCompleted();
// super.method(request, responseObserver);
} @Override
public void afterPropertiesSet() throws IOException {
ServerBuilder.forPort(8582)
.addService(new JavaGrpcServer())
.build().start();
}
}

client端demo

package com.baidu.traffic.signal.test;

import io.grpc.Channel;
import io.grpc.ManagedChannelBuilder;
import io.swagger.annotations.Api;
import java_test.TestGrpc;
import java_test.TestServiceGrpc;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* Created by liushouyun on 2019-12-20 12:36.
*/ @Api(tags = "grpc-test")
@RestController
@RequestMapping(value = "/test/grpc")
@Slf4j
public class JavaGrpcClient { private Channel channel = channel(); @GetMapping("/run")
public TestResult run() {
TestServiceGrpc.TestServiceBlockingStub testServiceBlockingStub = TestServiceGrpc.newBlockingStub(channel);
TestGrpc.Request request = TestGrpc.Request.newBuilder().setRequest1("ha?").setRequest2("en?").build();
TestGrpc.Result result = testServiceBlockingStub.method(request);
return new TestResult(result.getResult1(), result.getResult2());
} @Data
@AllArgsConstructor
@NoArgsConstructor
private class TestResult {
private String result1;
private String result2;
} private Channel channel() {
return ManagedChannelBuilder
.forAddress("127.0.0.1", 8582)
.usePlaintext()
.build();
}
}

GRPC-JAVA的更多相关文章

  1. gRPC Java的代码架构

    RPC(远程过程调用) 的架构最常见的是"动态代理"方式,事先定义好接口,用一个代理假装实现了这个接口(真正的实现放在服务端),供客户端调用,代理内部将该方法调用封装成一个网络请求 ...

  2. gRPC java 客户端,服务器端通讯使用json格式

    使用 protobuf 作为通讯内容序列化的简单例子请看:http://www.cnblogs.com/ghj1976/p/5458176.html . 本文是使用 json 做为内容序列化的简单例子 ...

  3. gRPC(Java) keepAlive机制研究

    基于java gRPC 1.24.2 分析 结论 gRPC keepAlive是grpc框架在应用层面连接保活的一种措施.即当grpc连接上没有业务数据时,是否发送pingpong,以保持连接活跃性, ...

  4. 用Java开发gRPC服务的例子分析

    本文的代码例子来自:https://github.com/grpc/grpc-java  定义服务 这一步与其他语言完全一样,需要定义gRPC的服务.方法.request和response的类型. 完 ...

  5. 开始食用grpc(之一)

    开始食用grpc(之一) 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9501353.html ```   记一次和一锅们压马路,路过一咖啡厅(某巴克),随口 ...

  6. gRPC官方文档(概览)

    文章来自gRPC 官方文档中文版 概览 开始 欢迎进入 gRPC 的开发文档,gRPC 一开始由 google 开发,是一款语言中立.平台中立.开源的远程过程调用(RPC)系统. 本文档通过快速概述和 ...

  7. gRPC入坑记

    概要 由于gRPC主要是谷歌开发的,由于一些已知的原因,gRPC跑demo还是不那么顺利的.单独写这一篇,主要是gRPC安装过程中的坑太多了,记录下来让大家少走弯路. 主要的坑: 如果使用PHP.Py ...

  8. gRPC Learning Notes

    简介 更多内容参考:https://www.grpc.io/docs/guides/ gRPC 是一个高性能.开源和通用的 RPC 框架,面向移动和 HTTP/2 设计.目前提供 C.Java 和 G ...

  9. RPC详解

    RPC(Remote Procedure Call),即远程过程调用,是一个分布式系统间通信的必备技术,本文体系性地介绍了 RPC 包含的核心概念和技术,希望读者读完文章,一提到 RPC,脑中不是零碎 ...

  10. 理解REST和RPC

    REST 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 网站开发,完全可以采用软件开发的模式.但是传统上,软件和网络是两个不同的领域,很少有交集:软件开发主要针对单机环境,网络则主要研究 ...

随机推荐

  1. C# 动态调用webservice代码

    /// <summary> /// 动态调用WebService /// </summary> /// <param name="url">UR ...

  2. 指针进阶(数组指针 )(C语言)

    1. 数组名的理解 在指针入门中我们在使用指针访问数组的内容时,有这样的代码: int arr[10] = {1,2,3,4,5,6,7,8,9,10}; int *p = &arr[0]; ...

  3. 好好的Typora收费了!_2022_01_20

    好好的Typora收费了!_2022_01_20 用惯了Typora真的很难再去换别的MarkDown编辑工具了,导出都找不到合适的.1.0以前的不需要验证的版本直接就不能打开了,真是头大. 到处找不 ...

  4. ESP8266 + L298N

    L298N 知乎教程 L298N ESP8266 + L298N 连线 电机转的方向 电源引脚 VCC 外接直流电源引脚,电压范围在5~35V之间 GND GND是接地引脚,连接到电源负极 5V 驱动 ...

  5. 《用广义CNOT门产生质数幂维的图态》

    参考文献:Graph states of prime-power dimension from generalized CNOT quantum circuit 主机文件:<2016质数图态.p ...

  6. 记录一个opencv的imread方法无法读取成功的问题,【设计到visual studio和静态库(lib)匹配的问题】

    一.为什么会遇到这个问题 公司需要对多图进行拼接,经过多番查找发现了OpenStitching这个库.可以实现多图拼接.在python段尝试了之后感觉效果不错,所以使用Visual Studio进行C ...

  7. PA1-碎碎念

    part 1 8.27 方便管理,主要是想熟悉下git的操作 先创建并且切换到一个新的分支: git commit --allow-empty -am "before starting PA ...

  8. Apache Tomcat AJP 实现负载均衡

    大部分一开始接触WEB服务器的人可能和我一样对为什么有Apache又有Tomcat服务器感到奇怪(它们还都是Apache开发的呵呵),其实他们不是冗余的服务器,虽然他们都能对外提供WEB服务器,但总的 ...

  9. How to display XML in a JTree using JDOM

    How to display XML in a JTree using JDOM This brief tutorial will explain how to use Java to make an ...

  10. 前端项目部署之pushstate-server

    pushstate-server 内部的原理是通过 connect 服务器,开启一个端口,将 dist/index.html 文件作为静态模板输出 这种方式可以将本地的项目打包成静态文件,以服务的方式 ...