IDEAjava开发grpc框架的服务端和客户端--helloworld实例参考:
1. java下使⽤gRPC的helloworld的demo实现
2. grpc官⽅⽂档中⽂版
3. ⽰例:
具体实施步骤:
1、新建⼀个普通的Maven项⽬:
点击下⼀步,再点击Finsh。
2、配置pom⽂件,导⼊grpc的依赖和插件
全部的pom内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"    xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId&pcprojects</groupId>
<artifactId>grpcExercise3</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<grpc-version>1.20.0</grpc-version>
</properties>
<dependencies>
<dependency>
<groupId&pc</groupId>
<artifactId>grpc-core</artifactId>
<version>${grpc-version}</version>
</dependency>
</dependency>
<dependency>
<groupId&pc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>${grpc-version}</version>
</dependency>
<dependency>
<groupId&pc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc-version}</version>
</dependency>
<dependency>
<groupId&pc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc-version}</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId&d.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId&lstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<protocArtifact&le.protobuf:protoc:3.7.1:exe:${os.detected.classifier}</protocArtifact>                    <pluginId>grpc-java</pluginId>
<pluginArtifact&pc:protoc-gen-grpc-java:1.9.1:exe:${os.detected.classifier}</pluginArtifact>                    <protoSourceRoot>src/main/proto</protoSourceRoot>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
idea开发安卓app教程<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
添加好依赖之后的显⽰:
添加之后选择右下键出现的import change,就能在Maven Projects中看到添加的依赖了。
3、编写helloworld.proto⽂件
在项⽬main⽬录下新建⼀个proto⽂件夹,再在此⽂件夹下创建⼀个helloworld.proto⽂件
helloworld.proto(跟官⽹上的⼀样)中的代码如下:
syntax = "proto3";
option java_multiple_files = true;
option java_package = "amples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
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;
}
重要的地⽅来了,编译helloworld.proto⽂件,编译此⽂件有两种⽅法,第⼀种使⽤和protoc-gen-grpc-java插件进⾏编译。第⼆种⽅法是⽤刚才在项⽬⾥边添加的插件进⾏编译。下边分别进⾏说明:
第⼀种:使⽤和protoc-gen-grpc-java插件进⾏编译
将和protoc-gen-grpc插件放到main⽬录中:
在项⽬的terminal窗⼝中执⾏如下两条指令:
执⾏完每条指令之后,窗⼝应该不会出现任何信息,在项⽬⽂件⽬录中会添加如下⽂件:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。