-
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
Chesnavsky Mark
committed
Nov 26, 2020
1 parent
d90d9ae
commit eca7f87
Showing
7 changed files
with
242 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,70 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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>org.example</groupId> | ||
<artifactId>grpc-client</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-netty</artifactId> | ||
<version>1.16.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-protobuf</artifactId> | ||
<version>1.16.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-stub</artifactId> | ||
<version>1.16.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<extensions> | ||
<extension> | ||
<groupId>kr.motd.maven</groupId> | ||
<artifactId>os-maven-plugin</artifactId> | ||
<version>1.6.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: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> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>6</source> | ||
<target>6</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
28 changes: 28 additions & 0 deletions
28
grpc-client/src/main/java/org/example/grpcbase/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.example.grpcbase; | ||
|
||
import io.grpc.ManagedChannel; | ||
import io.grpc.ManagedChannelBuilder; | ||
import org.example.grpc.base.HelloRequest; | ||
import org.example.grpc.base.HelloResponse; | ||
import org.example.grpc.base.HelloServiceGrpc; | ||
|
||
public class GrpcClient { | ||
|
||
public static void main(String[] args) { | ||
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080) | ||
.usePlaintext() | ||
.build(); | ||
|
||
HelloServiceGrpc.HelloServiceBlockingStub stub | ||
= HelloServiceGrpc.newBlockingStub(channel); | ||
|
||
HelloResponse helloResponse = stub.hello(HelloRequest.newBuilder() | ||
.setFirstName("Mark") | ||
.setLastName("Chesnavsky") | ||
.build()); | ||
|
||
System.out.println("Result: " + helloResponse.getGreeting()); | ||
|
||
channel.shutdown(); | ||
} | ||
} |
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.example.grpc.base; | ||
|
||
message HelloRequest { | ||
string firstName = 1; | ||
string lastName = 2; | ||
} | ||
|
||
message HelloResponse { | ||
string greeting = 1; | ||
} | ||
|
||
service HelloService { | ||
rpc hello(HelloRequest) returns (HelloResponse); | ||
} |
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,70 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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>org.example</groupId> | ||
<artifactId>grpc-server</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-netty</artifactId> | ||
<version>1.16.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-protobuf</artifactId> | ||
<version>1.16.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-stub</artifactId> | ||
<version>1.16.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<extensions> | ||
<extension> | ||
<groupId>kr.motd.maven</groupId> | ||
<artifactId>os-maven-plugin</artifactId> | ||
<version>1.6.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: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> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>6</source> | ||
<target>6</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
18 changes: 18 additions & 0 deletions
18
grpc-server/src/main/java/org/example/grpcbase/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,18 @@ | ||
package org.example.grpcbase; | ||
|
||
import io.grpc.Server; | ||
import io.grpc.ServerBuilder; | ||
|
||
import java.io.IOException; | ||
|
||
public class GrpcServer { | ||
|
||
public static void main(String[] args) throws IOException, InterruptedException { | ||
Server server = ServerBuilder | ||
.forPort(8080) | ||
.addService(new HelloServiceGrpcImpl()).build(); | ||
|
||
server.start(); | ||
server.awaitTermination(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
grpc-server/src/main/java/org/example/grpcbase/HelloServiceGrpcImpl.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,24 @@ | ||
package org.example.grpcbase; | ||
|
||
import io.grpc.stub.StreamObserver; | ||
import org.example.grpc.base.HelloRequest; | ||
import org.example.grpc.base.HelloResponse; | ||
import org.example.grpc.base.HelloServiceGrpc; | ||
|
||
public class HelloServiceGrpcImpl extends HelloServiceGrpc.HelloServiceImplBase { | ||
|
||
@Override | ||
public void hello( | ||
HelloRequest request, StreamObserver<HelloResponse> responseObserver) { | ||
|
||
String greeting = "Hello, " + request.getFirstName() | ||
+ " " + request.getLastName() + "!"; | ||
|
||
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.example.grpc.base; | ||
|
||
message HelloRequest { | ||
string firstName = 1; | ||
string lastName = 2; | ||
} | ||
|
||
message HelloResponse { | ||
string greeting = 1; | ||
} | ||
|
||
service HelloService { | ||
rpc hello(HelloRequest) returns (HelloResponse); | ||
} |