Skip to content

Commit 38989db

Browse files
committed
code for gRPC
1 parent 9b143b8 commit 38989db

5 files changed

Lines changed: 165 additions & 0 deletions

File tree

grpc/pom.xml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>grpc</groupId>
6+
<artifactId>grpc-demo</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>grpc-demo</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>io.grpc</groupId>
20+
<artifactId>grpc-netty</artifactId>
21+
<version>1.4.0</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>io.grpc</groupId>
25+
<artifactId>grpc-protobuf</artifactId>
26+
<version>1.4.0</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>io.grpc</groupId>
30+
<artifactId>grpc-stub</artifactId>
31+
<version>1.4.0</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>junit</groupId>
35+
<artifactId>junit</artifactId>
36+
<version>4.12</version>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
<build>
41+
<extensions>
42+
<extension>
43+
<groupId>kr.motd.maven</groupId>
44+
<artifactId>os-maven-plugin</artifactId>
45+
<version>1.5.0.Final</version>
46+
</extension>
47+
</extensions>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.xolstice.maven.plugins</groupId>
51+
<artifactId>protobuf-maven-plugin</artifactId>
52+
<version>0.5.0</version>
53+
<configuration>
54+
<protocArtifact>
55+
com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}
56+
</protocArtifact>
57+
<pluginId>grpc-java</pluginId>
58+
<pluginArtifact>
59+
io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier}
60+
</pluginArtifact>
61+
</configuration>
62+
<executions>
63+
<execution>
64+
<goals>
65+
<goal>compile</goal>
66+
<goal>compile-custom</goal>
67+
</goals>
68+
</execution>
69+
</executions>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.baeldung.grpc.client;
2+
3+
import org.baeldung.grpc.HelloRequest;
4+
import org.baeldung.grpc.HelloResponse;
5+
import org.baeldung.grpc.HelloServiceGrpc;
6+
7+
import io.grpc.ManagedChannel;
8+
import io.grpc.ManagedChannelBuilder;
9+
10+
public class GrpcClient {
11+
public static void main(String[] args) throws InterruptedException {
12+
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080)
13+
.usePlaintext(true)
14+
.build();
15+
16+
HelloServiceGrpc.HelloServiceBlockingStub stub
17+
= HelloServiceGrpc.newBlockingStub(channel);
18+
19+
HelloResponse helloResponse = stub.hello(HelloRequest.newBuilder()
20+
.setFirstName("Baeldung")
21+
.setLastName("gRPC")
22+
.build());
23+
24+
System.out.println("Response received from server:\n" + helloResponse);
25+
26+
channel.shutdown();
27+
}
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.baeldung.grpc.server;
2+
3+
import java.io.IOException;
4+
5+
import io.grpc.Server;
6+
import io.grpc.ServerBuilder;
7+
8+
public class GrpcServer
9+
{
10+
public static void main(String[] args) throws IOException, InterruptedException {
11+
Server server = ServerBuilder.forPort(8080)
12+
.addService(new HelloServiceImpl()).build();
13+
14+
System.out.println("Starting server...");
15+
server.start();
16+
System.out.println("Server started!");
17+
server.awaitTermination();
18+
}
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.baeldung.grpc.server;
2+
3+
import org.baeldung.grpc.HelloRequest;
4+
import org.baeldung.grpc.HelloResponse;
5+
import org.baeldung.grpc.HelloServiceGrpc.HelloServiceImplBase;
6+
7+
import io.grpc.stub.StreamObserver;
8+
9+
public class HelloServiceImpl extends HelloServiceImplBase {
10+
11+
@Override
12+
public void hello(
13+
HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
14+
System.out.println("Request received from client:\n" + request);
15+
16+
String greeting = new StringBuilder().append("Hello, ")
17+
.append(request.getFirstName())
18+
.append(" ")
19+
.append(request.getLastName())
20+
.toString();
21+
22+
HelloResponse response = HelloResponse.newBuilder()
23+
.setGreeting(greeting)
24+
.build();
25+
26+
responseObserver.onNext(response);
27+
responseObserver.onCompleted();
28+
}
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
syntax = "proto3";
2+
option java_multiple_files = true;
3+
package org.baeldung.grpc;
4+
5+
message HelloRequest {
6+
string firstName = 1;
7+
string lastName = 2;
8+
}
9+
10+
message HelloResponse {
11+
string greeting = 1;
12+
}
13+
14+
service HelloService {
15+
rpc hello(HelloRequest) returns (HelloResponse);
16+
}

0 commit comments

Comments
 (0)