Découvrez gRPC! Il s'agit d'un mémorandum tel que l'ajustement de l'emplacement de construction.
[pom.xml]
<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>com</groupId>
<artifactId>grpc.demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>grpc.demo2</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/io.grpc/grpc-protobuf -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.22.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.grpc/grpc-netty -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.22.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.grpc/grpc-stub -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.22.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.9.0</version>
</dependency>
<!--Parce qu'il est hors de la bibliothèque standard de Java 9-->
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<defaultGoal>clean generate-sources compile install</defaultGoal>
<plugins>
<plugin>
<groupId>com.github.os72</groupId>
<artifactId>protoc-jar-maven-plugin</artifactId>
<version>3.8.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<includeMavenTypes>direct</includeMavenTypes>
<inputDirectories>
<include>src/main/resources</include>
</inputDirectories>
<outputTargets>
<outputTarget>
<type>java</type>
<outputDirectory>src/main/java</outputDirectory>
</outputTarget>
<outputTarget>
<type>grpc-java</type>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.22.1</pluginArtifact>
<outputDirectory>src/main/java</outputDirectory>
</outputTarget>
</outputTargets>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Créez un dossier de ressources sous src / main et créez un fichier prototype.
[helloworld.proto]
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.example.helloworld";
package helloworld;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
Vous devriez maintenant avoir le paquet io.grpc.example.helloworld et la source spécifiés ci-dessus.
Maintenant, implémentons le serveur et le client et voyons comment cela fonctionne.
[Server.java]
public class Server {
public static void main(String[] args) throws Exception {
int port = 8080;
io.grpc.Server server = ServerBuilder.forPort(port).addService((BindableService) new GreeterImpl()).build();
server.start();
server.awaitTermination();
}
}
[GreeterImpl.java]
public class GreeterImpl extends GreeterGrpc.GreeterImplBase {
public void sayHello(HelloRequest reqest, StreamObserver<HelloReply> responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + reqest.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
[Client.java]
public class Client {
public static void main(String[] args) {
String name = "Takeshi";
int port = 8080;
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", port).usePlaintext().build();
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloReply reply = stub.sayHello(request);
System.out.println("Reply: " + reply.getMessage());
}
}
Exécutez le client après le démarrage du serveur.
Reply: Hello Takeshi
Takeshi est de retour sain et sauf.
GRPC Project Setup in Java Points pour l'introduction de gRPC en Java
Recommended Posts