Merge pull request #2353 from eugenp/BAEL-1022-grpc-ganesh
BAEL-1022-grpc-ganesh
This commit is contained in:
commit
aeec8f53f9
|
@ -0,0 +1,74 @@
|
|||
<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>grpc</groupId>
|
||||
<artifactId>grpc-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>grpc-demo</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<io.grpc.version>1.5.0</io.grpc.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-netty</artifactId>
|
||||
<version>${io.grpc.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-protobuf</artifactId>
|
||||
<version>${io.grpc.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-stub</artifactId>
|
||||
<version>${io.grpc.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<extensions>
|
||||
<extension>
|
||||
<groupId>kr.motd.maven</groupId>
|
||||
<artifactId>os-maven-plugin</artifactId>
|
||||
<version>1.5.0.Final</version>
|
||||
</extension>
|
||||
</extensions>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.xolstice.maven.plugins</groupId>
|
||||
<artifactId>protobuf-maven-plugin</artifactId>
|
||||
<version>0.5.0</version>
|
||||
<configuration>
|
||||
<protocArtifact>
|
||||
com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}
|
||||
</protocArtifact>
|
||||
<pluginId>grpc-java</pluginId>
|
||||
<pluginArtifact>
|
||||
io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier}
|
||||
</pluginArtifact>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
<goal>compile-custom</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,28 @@
|
|||
package org.baeldung.grpc.client;
|
||||
|
||||
import org.baeldung.grpc.HelloRequest;
|
||||
import org.baeldung.grpc.HelloResponse;
|
||||
import org.baeldung.grpc.HelloServiceGrpc;
|
||||
|
||||
import io.grpc.ManagedChannel;
|
||||
import io.grpc.ManagedChannelBuilder;
|
||||
|
||||
public class GrpcClient {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080)
|
||||
.usePlaintext(true)
|
||||
.build();
|
||||
|
||||
HelloServiceGrpc.HelloServiceBlockingStub stub
|
||||
= HelloServiceGrpc.newBlockingStub(channel);
|
||||
|
||||
HelloResponse helloResponse = stub.hello(HelloRequest.newBuilder()
|
||||
.setFirstName("Baeldung")
|
||||
.setLastName("gRPC")
|
||||
.build());
|
||||
|
||||
System.out.println("Response received from server:\n" + helloResponse);
|
||||
|
||||
channel.shutdown();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package org.baeldung.grpc.server;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import io.grpc.Server;
|
||||
import io.grpc.ServerBuilder;
|
||||
|
||||
public class GrpcServer {
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
Server server = ServerBuilder.forPort(8080)
|
||||
.addService(new HelloServiceImpl()).build();
|
||||
|
||||
System.out.println("Starting server...");
|
||||
server.start();
|
||||
System.out.println("Server started!");
|
||||
server.awaitTermination();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package org.baeldung.grpc.server;
|
||||
|
||||
import org.baeldung.grpc.HelloRequest;
|
||||
import org.baeldung.grpc.HelloResponse;
|
||||
import org.baeldung.grpc.HelloServiceGrpc.HelloServiceImplBase;
|
||||
|
||||
import io.grpc.stub.StreamObserver;
|
||||
|
||||
public class HelloServiceImpl extends HelloServiceImplBase {
|
||||
|
||||
@Override
|
||||
public void hello(
|
||||
HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
|
||||
System.out.println("Request received from client:\n" + request);
|
||||
|
||||
String greeting = new StringBuilder().append("Hello, ")
|
||||
.append(request.getFirstName())
|
||||
.append(" ")
|
||||
.append(request.getLastName())
|
||||
.toString();
|
||||
|
||||
HelloResponse response = HelloResponse.newBuilder()
|
||||
.setGreeting(greeting)
|
||||
.build();
|
||||
|
||||
responseObserver.onNext(response);
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
syntax = "proto3";
|
||||
option java_multiple_files = true;
|
||||
package org.baeldung.grpc;
|
||||
|
||||
message HelloRequest {
|
||||
string firstName = 1;
|
||||
string lastName = 2;
|
||||
}
|
||||
|
||||
message HelloResponse {
|
||||
string greeting = 1;
|
||||
}
|
||||
|
||||
service HelloService {
|
||||
rpc hello(HelloRequest) returns (HelloResponse);
|
||||
}
|
Loading…
Reference in New Issue