Created project for gRPC task.
This commit is contained in:
parent
0f4364b2f2
commit
337a6e73e8
4 changed files with 297 additions and 0 deletions
143
task05/pom.xml
Normal file
143
task05/pom.xml
Normal file
|
@ -0,0 +1,143 @@
|
||||||
|
<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/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>de.hftstuttgart.vs</groupId>
|
||||||
|
<artifactId>task05</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<!-- Feel free to delete the comment at the end of these lines. It is just
|
||||||
|
for safely updating the version in our release process. -->
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<grpc.version>1.54.1</grpc.version><!-- CURRENT_GRPC_VERSION -->
|
||||||
|
<protobuf.version>3.22.3</protobuf.version>
|
||||||
|
<protoc.version>3.22.3</protoc.version>
|
||||||
|
<!-- required for JDK 8 -->
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.grpc</groupId>
|
||||||
|
<artifactId>grpc-bom</artifactId>
|
||||||
|
<version>${grpc.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.grpc</groupId>
|
||||||
|
<artifactId>grpc-netty-shaded</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.protobuf</groupId>
|
||||||
|
<artifactId>protobuf-java-util</artifactId>
|
||||||
|
<version>${protobuf.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.grpc</groupId>
|
||||||
|
<artifactId>grpc-protobuf</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.grpc</groupId>
|
||||||
|
<artifactId>grpc-services</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.grpc</groupId>
|
||||||
|
<artifactId>grpc-stub</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.grpc</groupId>
|
||||||
|
<artifactId>grpc-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.9.0</version> <!-- prevent downgrade via protobuf-java-util -->
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
<version>31.1-jre</version> <!-- prevent downgrade of version in protobuf-java-util -->
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.tomcat</groupId>
|
||||||
|
<artifactId>annotations-api</artifactId>
|
||||||
|
<version>6.0.53</version>
|
||||||
|
<scope>provided</scope> <!-- not needed at runtime -->
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.grpc</groupId>
|
||||||
|
<artifactId>grpc-testing</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<version>3.4.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<extensions>
|
||||||
|
<extension>
|
||||||
|
<groupId>kr.motd.maven</groupId>
|
||||||
|
<artifactId>os-maven-plugin</artifactId>
|
||||||
|
<version>1.7.1</version>
|
||||||
|
</extension>
|
||||||
|
</extensions>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.xolstice.maven.plugins</groupId>
|
||||||
|
<artifactId>protobuf-maven-plugin</artifactId>
|
||||||
|
<version>0.6.1</version>
|
||||||
|
<configuration>
|
||||||
|
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
|
||||||
|
<pluginId>grpc-java</pluginId>
|
||||||
|
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>compile</goal>
|
||||||
|
<goal>compile-custom</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-enforcer-plugin</artifactId>
|
||||||
|
<version>1.4.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>enforce</id>
|
||||||
|
<goals>
|
||||||
|
<goal>enforce</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<rules>
|
||||||
|
<requireUpperBoundDeps/>
|
||||||
|
</rules>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
55
task05/src/main/java/de/hftstuttgart/vs/HelloClient.java
Normal file
55
task05/src/main/java/de/hftstuttgart/vs/HelloClient.java
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
package de.hftstuttgart.vs;
|
||||||
|
|
||||||
|
|
||||||
|
import io.grpc.Channel;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureChannelCredentials;
|
||||||
|
import io.grpc.ManagedChannel;
|
||||||
|
import io.grpc.StatusRuntimeException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class HelloClient {
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(HelloClient.class.getName());
|
||||||
|
|
||||||
|
private final GreeterGrpc.GreeterBlockingStub blockingStub;
|
||||||
|
|
||||||
|
public HelloClient(Channel channel) {
|
||||||
|
blockingStub = GreeterGrpc.newBlockingStub(channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void greet(String name) {
|
||||||
|
logger.info("Will try to greet " + name + " ...");
|
||||||
|
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
|
||||||
|
HelloReply response;
|
||||||
|
try {
|
||||||
|
response = blockingStub
|
||||||
|
.sayHello(request);
|
||||||
|
} catch (StatusRuntimeException e) {
|
||||||
|
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
logger.info("Greeting: " + response.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
String user = "world";
|
||||||
|
String target = "localhost:50051";
|
||||||
|
|
||||||
|
ManagedChannel channel = Grpc
|
||||||
|
.newChannelBuilder(target, InsecureChannelCredentials.create())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try {
|
||||||
|
HelloClient client = new HelloClient(channel);
|
||||||
|
client.greet(user);
|
||||||
|
} finally {
|
||||||
|
channel
|
||||||
|
.shutdownNow()
|
||||||
|
.awaitTermination(5, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
75
task05/src/main/java/de/hftstuttgart/vs/HelloServer.java
Normal file
75
task05/src/main/java/de/hftstuttgart/vs/HelloServer.java
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
package de.hftstuttgart.vs;
|
||||||
|
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
|
import io.grpc.Server;
|
||||||
|
import io.grpc.stub.StreamObserver;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class HelloServer {
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(HelloServer.class.getName());
|
||||||
|
|
||||||
|
private Server server;
|
||||||
|
|
||||||
|
private void start() throws IOException {
|
||||||
|
/* The port on which the server should run */
|
||||||
|
int port = 50051;
|
||||||
|
server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
|
||||||
|
.addService(new GreeterImpl())
|
||||||
|
.build()
|
||||||
|
.start();
|
||||||
|
logger.info("Server started, listening on " + port);
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
|
||||||
|
System.err.println("*** shutting down gRPC server since JVM is shutting down");
|
||||||
|
try {
|
||||||
|
HelloServer.this.stop();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace(System.err);
|
||||||
|
}
|
||||||
|
System.err.println("*** server shut down");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void stop() throws InterruptedException {
|
||||||
|
if (server != null) {
|
||||||
|
server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Await termination on the main thread since the grpc library uses daemon threads.
|
||||||
|
*/
|
||||||
|
private void blockUntilShutdown() throws InterruptedException {
|
||||||
|
if (server != null) {
|
||||||
|
server.awaitTermination();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main launches the server from the command line.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) throws IOException, InterruptedException {
|
||||||
|
final HelloServer server = new HelloServer();
|
||||||
|
server.start();
|
||||||
|
server.blockUntilShutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
static class GreeterImpl extends GreeterGrpc.GreeterImplBase {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
|
||||||
|
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build();
|
||||||
|
responseObserver.onNext(reply);
|
||||||
|
responseObserver.onCompleted();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
24
task05/src/main/proto/helloworld.proto
Normal file
24
task05/src/main/proto/helloworld.proto
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
option java_multiple_files = true;
|
||||||
|
option java_package = "de.hftstuttgart.vs";
|
||||||
|
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;
|
||||||
|
}
|
Loading…
Reference in a new issue