Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Chesnavsky Mark committed Nov 26, 2020
1 parent d90d9ae commit eca7f87
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 0 deletions.
70 changes: 70 additions & 0 deletions grpc-client/pom.xml
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 grpc-client/src/main/java/org/example/grpcbase/GrpcClient.java
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();
}
}
16 changes: 16 additions & 0 deletions grpc-client/src/main/proto/HelloService.proto
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);
}
70 changes: 70 additions & 0 deletions grpc-server/pom.xml
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 grpc-server/src/main/java/org/example/grpcbase/GrpcServer.java
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();
}
}
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();
}
}
16 changes: 16 additions & 0 deletions grpc-server/src/main/proto/HelloService.proto
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);
}

0 comments on commit eca7f87

Please sign in to comment.