forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<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> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-netty</artifactId> | ||
<version>1.4.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-protobuf</artifactId> | ||
<version>1.4.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-stub</artifactId> | ||
<version>1.4.0</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> |
28 changes: 28 additions & 0 deletions
28
grpc/src/main/java/org/baeldung/grpc/client/GrpcClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
grpc/src/main/java/org/baeldung/grpc/server/GrpcServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
grpc/src/main/java/org/baeldung/grpc/server/HelloServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |