热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

IDEA搭建gRPC服务

一、什么是gRPCgRPC是一个高性能、通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP2协议标准而设计,基于ProtoBuf(

一、什么是gRPC
gRPC是一个高性能、通用的开源 RPC 框架,其由 Google 主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers) 序列化协议开发,且支持众多开发语言。gRPC 提供了一种简单的方法来精确地定义服务和为 iOS、Android 和后台支持服务自动生成可靠性很强的客户端功能库。客户端充分利用高级流和链接功能,从而有助于节省带宽、降低的 TCP 链接次数、节省 CPU 使用、和电池寿命。
gRPC使用 ProtoBuf 来定义服务,ProtoBuf 是由 Google 开发的一种数据序列化协议(类似于 XML、JSON、hessian)。ProtoBuf 能够将数据进行序列化,并广泛应用在数据存储、通信协议等方面。不过,当前 gRPC 仅支持 Protobuf ,且不支持在浏览器中使用。由于 gRPC 的设计能够支持支持多种数据格式。
二、首先搭建maven项目
在这里插入图片描述
1.然后在src/main目录下的proto目录
在这里插入图片描述
2.设置把proto设置成java的sources,选择file->project structure->modules
在这里插入图片描述
3.创建hello_service.proto的文件
在这里插入图片描述
4.hello_service.proto与hello.proto
4.1、hello.proto

syntax = "proto3";package com.xxx.tutorial.demo.grpc;option java_multiple_files = true;
option java_package = "com.xxx.tutorial.model";
option java_outer_classname = "Hello";message HelloRequest{ string name = 1; int32 id = 2;
}
message HelloResponse{ string message = 1;
}

4.2、hello_service.proto

syntax = "proto3";package com.xxx.tutorial.demo.grpc;option java_multiple_files = true;
option java_package = "com.xxx.tutorial.service";
option java_outer_classname = "GreetingService";import "hello.proto";service HelloService{ rpc sayHello(HelloRequest) returns (HelloResponse);
}

5.需要导入一个IDEA插件Protobuf
在这里插入图片描述
6.使用maven进行编译
在这里插入图片描述
7.选择install等待一会进行生成GRPC代码
在这里插入图片描述
三、应用springboot搭建GRPC项目
8.比如说创建简单的helloworld.proto

syntax = "proto3";option java_multiple_files = true;
//定义输出的目录,生成的目录就是“net/devh/examples/grpc/lib”下面
option java_package = "net.devh.examples.grpc.lib";
//定义输出的文件名称,生成在lib下的就是HelloWorldProto.class
option java_outer_classname = "HelloWorldProto";// The greeting service definition.
//定义的接口的类,这里会生成一个SimpleGrpc.class,服务端需要来实现的
service Simple {//定义接口方法rpc SayHello (HelloRequest) returns (HelloReply) {}
}//请求参数
message HelloRequest {string name = 1;
}//返回结果
message HelloReply {string message = 1;
}

9.将gradle转换成maven
注意此代码是gradle转写maven的gradle文件,并非项目文件。进行gradle打包需要将此代码注释

apply plugin: &#39;maven&#39;task writeNewPom <<{pom {project {inceptionYear &#39;2018&#39;licenses {license {name &#39;The Apache Software License, Version 2.0&#39;url &#39;http://www.apache.org/licenses/LICENSE-2.0.txt&#39;distribution &#39;repo&#39;}}}}.writeTo("pom.xml")
}

在这里插入图片描述
再运行右侧 gradle writeNewPom 即可在当前项目中生成pom.xml文件。
10.创建springboot的依赖
在这里插入图片描述
build.gradle

apply plugin: &#39;java&#39;
apply plugin: &#39;com.google.protobuf&#39;
apply plugin: &#39;idea&#39;repositories {maven { url "https://plugins.gradle.org/m2/" }
}dependencies {compile "io.grpc:grpc-netty:1.10.0"compile "io.grpc:grpc-protobuf:1.10.0"compile "io.grpc:grpc-stub:1.10.0"
}protobuf {protoc {// The artifact spec for the Protobuf Compilerartifact &#61; &#39;com.google.protobuf:protoc:3.0.0&#39;}plugins {// Optional: an artifact spec for a protoc plugin, with "grpc" as// the identifier, which can be referred to in the "plugins"// container of the "generateProtoTasks" closure.grpc {artifact &#61; &#39;io.grpc:protoc-gen-grpc-java:1.0.0-pre2&#39;}}generateProtoTasks {ofSourceSet(&#39;main&#39;)*.plugins {// Apply the "grpc" plugin whose spec is defined above, without// options. Note the braces cannot be omitted, otherwise the// plugin will not be added. This is because of the implicit way// NamedDomainObjectContainer binds the methods.grpc { }}}
}buildscript {repositories {maven { url "https://plugins.gradle.org/m2/" }}dependencies {classpath &#39;com.google.protobuf:protobuf-gradle-plugin:0.8.4&#39;}
}

进行编译
在这里插入图片描述
11.创建客户端项目
在这里插入图片描述
build.gradle

buildscript {ext {springBootVersion &#61; &#39;2.0.2.RELEASE&#39;}repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}apply plugin: &#39;java&#39;
apply plugin: &#39;eclipse&#39;
apply plugin: &#39;org.springframework.boot&#39;
apply plugin: &#39;io.spring.dependency-management&#39;//apply plugin: &#39;maven&#39;group &#61; &#39;com.example&#39;
version &#61; &#39;0.0.1-SNAPSHOT&#39;
sourceCompatibility &#61; 1.8repositories {mavenCentral()
}dependencies {compile project(&#39;:grpc-lib&#39;)compile("net.devh:grpc-client-spring-boot-starter:1.4.0.RELEASE")compile(&#39;org.springframework.boot:spring-boot-starter-web&#39;)
}//
//task writeNewPom <<{
// pom {
// project {
// inceptionYear &#39;2018&#39;
// licenses {
// license {
// name &#39;The Apache Software License, Version 2.0&#39;
// url &#39;http://www.apache.org/licenses/LICENSE-2.0.txt&#39;
// distribution &#39;repo&#39;
// }
// }
// }
// }.writeTo("pom.xml")
//} as String

GRPCClientApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;&#64;SpringBootApplication
public class GRpcClientApplication {public static void main(String[] args) {SpringApplication.run(GRpcClientApplication.class, args);}
}

GrpcClientController

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;&#64;RestController
public class GrpcClientController {&#64;Autowiredprivate GrpcClientService grpcClientService;&#64;RequestMapping("/")public String printMessage(&#64;RequestParam(defaultValue &#61; "Michael") String name) {return grpcClientService.sendMessage(name);}
}

GrpcClientService


import io.grpc.Channel;
import net.devh.examples.grpc.lib.HelloReply;
import net.devh.examples.grpc.lib.HelloRequest;
import net.devh.examples.grpc.lib.SimpleGrpc;
import net.devh.springboot.autoconfigure.grpc.client.GrpcClient;
import org.springframework.stereotype.Service;&#64;Service
public class GrpcClientService {&#64;GrpcClient("local-grpc-server")private Channel serverChannel;public String sendMessage(String name) {SimpleGrpc.SimpleBlockingStub stub &#61; SimpleGrpc.newBlockingStub(serverChannel);HelloReply response &#61; stub.sayHello(HelloRequest.newBuilder().setName(name).build());return response.getMessage();}
}

application.properties

server.port&#61;8080
spring.application.name&#61;local-grpc-client
grpc.client.local-grpc-server.host&#61;127.0.0.1
grpc.client.local-grpc-server.port&#61;9898
grpc.client.local-grpc-server.enableKeepAlive&#61;true
grpc.client.local-grpc-server.keepAliveWithoutCalls&#61;true

12.服务端项目


build.gradle

buildscript {ext {springBootVersion &#61; &#39;2.0.2.RELEASE&#39;}repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")}
}apply plugin: &#39;java&#39;
apply plugin: &#39;eclipse&#39;
apply plugin: &#39;org.springframework.boot&#39;
apply plugin: &#39;io.spring.dependency-management&#39;group &#61; &#39;com.example&#39;
version &#61; &#39;0.0.1-SNAPSHOT&#39;
sourceCompatibility &#61; 1.8repositories {mavenCentral()
}dependencies {compile(&#39;org.springframework.boot:spring-boot-starter-web&#39;)compile &#39;net.devh:grpc-server-spring-boot-starter:1.4.0.RELEASE&#39;//注意&#xff0c;需要依赖grpc-lib项目compile project(&#39;:grpc-lib&#39;)testCompile(&#39;org.springframework.boot:spring-boot-starter-test&#39;)
}
buildscript {dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:0.8.4")}
}

GRpcApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;&#64;SpringBootApplication
public class GRpcApplication {public static void main(String[] args) {SpringApplication.run(GRpcApplication.class, args);}
}

GrpcServerService

import io.grpc.stub.StreamObserver;
import net.devh.examples.grpc.lib.HelloReply;
import net.devh.examples.grpc.lib.HelloRequest;
import net.devh.examples.grpc.lib.SimpleGrpc;
import net.devh.springboot.autoconfigure.grpc.server.GrpcService;&#64;GrpcService(SimpleGrpc.class)
public class GrpcServerService extends SimpleGrpc.SimpleImplBase{&#64;Overridepublic void sayHello(HelloRequest req, StreamObserver responseObserver) {HelloReply reply &#61; HelloReply.newBuilder().setMessage("Hello &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;> " &#43; req.getName()).build();responseObserver.onNext(reply);responseObserver.onCompleted();}
}

application.properties

#服务端名称
spring.application.name&#61;local-grpc-server
#服务端运行端口
server.port&#61;8888
#grpc通信端口
grpc.server.port&#61;9898

13.大功告成。
14.待续…
15.https://github.com/863473007/springboot-gRPC/tree/master/springboot-grpc


推荐阅读
  • 小白轻松使用axis2构建webservice
    引言:使用axis2是来实现webservice接口是比较常见的,就我来说,如果要学一个首次接触东西,简单了解相关基础概念 ... [详细]
  • 阿里首席架构师科普RPC框架
    RPC概念及分类RPC全称为RemoteProcedureCall,翻译过来为“远程过程调用”。目前,主流的平台中都支持各种远程调用技术,以满足分布式系统架构中不同的系统之间的远程 ... [详细]
  • 在这分布式系统架构盛行的时代,很多互联网大佬公司开源出自己的分布式RPC系统框架,例如:阿里的dubbo,谷歌的gRPC,apache的Thrift。而在我们公司一直都在推荐使用d ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • flowable工作流 流程变量_信也科技工作流平台的技术实践
    1背景随着公司业务发展及内部业务流程诉求的增长,目前信息化系统不能够很好满足期望,主要体现如下:目前OA流程引擎无法满足企业特定业务流程需求,且移动端体 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • Java自带的观察者模式及实现方法详解
    本文介绍了Java自带的观察者模式,包括Observer和Observable对象的定义和使用方法。通过添加观察者和设置内部标志位,当被观察者中的事件发生变化时,通知观察者对象并执行相应的操作。实现观察者模式非常简单,只需继承Observable类和实现Observer接口即可。详情请参考Java官方api文档。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • java布尔字段用is前缀_POJO类中布尔类型的变量都不要加is前缀详解
    前言对应阿里巴巴开发手册第一章的命名风格的第八条。【强制】POJO类中布尔类型的变量都不要加is前缀,否则部分框架解析会引起序列化错误。反例:定义为基本 ... [详细]
  • SOA架构理解理解SOA架构,了解ESB概念,明白SOA与微服务的区别和联系,了解SOA与热门技术的结合与应用。1、面向服务的架构SOASOA(ServiceOrien ... [详细]
  • 阿里云监控URL的配置笔记
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了阿里云监控URL的配置笔记相关的知识,希望对你有一定的参考价值。有很多细节需要记录 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • Hadoop2.6.0 + 云centos +伪分布式只谈部署
    3.0.3玩不好,现将2.6.0tar.gz上传到usr,chmod-Rhadoop:hadophadoop-2.6.0,rm掉3.0.32.在etcp ... [详细]
author-avatar
lovely蓝衣13
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有