Skip to content

Commit e43226d

Browse files
committed
tests grpc java
1 parent 304d790 commit e43226d

File tree

9 files changed

+589
-0
lines changed

9 files changed

+589
-0
lines changed

java-grpc-demo/pom.xml

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>me.zaccoding</groupId>
8+
<artifactId>demo</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<io.grpc.version>1.16.1</io.grpc.version>
13+
<os-maven-plugin.version>1.6.1</os-maven-plugin.version>
14+
<protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version>
15+
<grpc.version>1.20.0</grpc.version>
16+
</properties>
17+
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>io.grpc</groupId>
22+
<artifactId>grpc-netty</artifactId>
23+
<version>${grpc.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>io.grpc</groupId>
27+
<artifactId>grpc-protobuf</artifactId>
28+
<version>${grpc.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>io.grpc</groupId>
32+
<artifactId>grpc-stub</artifactId>
33+
<version>${grpc.version}</version>
34+
</dependency>
35+
</dependencies>
36+
37+
<build>
38+
<extensions>
39+
<extension>
40+
<groupId>kr.motd.maven</groupId>
41+
<artifactId>os-maven-plugin</artifactId>
42+
<version>1.6.1</version>
43+
</extension>
44+
</extensions>
45+
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<configuration>
51+
<source>1.8</source>
52+
<target>1.8</target>
53+
</configuration>
54+
</plugin>
55+
56+
<plugin>
57+
<groupId>org.xolstice.maven.plugins</groupId>
58+
<artifactId>protobuf-maven-plugin</artifactId>
59+
<version>0.6.1</version>
60+
<configuration>
61+
<protocArtifact>
62+
com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}
63+
</protocArtifact>
64+
<pluginId>grpc-java</pluginId>
65+
<pluginArtifact>
66+
io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier}
67+
</pluginArtifact>
68+
<attachProtoSources>false</attachProtoSources>
69+
</configuration>
70+
<executions>
71+
<execution>
72+
<goals>
73+
<goal>compile</goal>
74+
<goal>compile-custom</goal>
75+
</goals>
76+
</execution>
77+
</executions>
78+
</plugin>
79+
</plugins>
80+
</build>
81+
82+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package demo;
2+
3+
import demo.sample1.client.Sample1GrpcClient;
4+
import demo.sample1.server.Sample1GrpcServer;
5+
import demo.util.SimpleLogger;
6+
7+
/**
8+
* @GitHub : https://github.com/zacscoding
9+
*/
10+
public class DemoApplication {
11+
12+
public static void main(String[] args) throws Exception {
13+
SampleRunner runner = SampleRunner.SAMPLE1;
14+
15+
SimpleLogger.println("Running samples {} - {}", runner, runner.getDescription());
16+
switch (runner) {
17+
case SAMPLE1:
18+
runSample1();
19+
break;
20+
default:
21+
SimpleLogger.println("Invalid sample runner : " + runner);
22+
}
23+
}
24+
25+
private static void runSample1() throws Exception {
26+
final String address = "127.0.0.1";
27+
final Integer port = 8888;
28+
29+
Sample1GrpcServer server = new Sample1GrpcServer(port);
30+
server.start();
31+
32+
Sample1GrpcClient client = new Sample1GrpcClient(address, port);
33+
client.request();
34+
35+
Sample1GrpcClient client2 = new Sample1GrpcClient(address, port);
36+
client2.request();
37+
38+
server.interrupt();
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package demo;
2+
3+
/**
4+
* @GitHub : https://github.com/zacscoding
5+
*/
6+
public enum SampleRunner {
7+
8+
SAMPLE1("Basic grpc server & client");
9+
10+
String description;
11+
12+
SampleRunner(String description) {
13+
this.description = description;
14+
}
15+
16+
public String getDescription() {
17+
return description;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package demo.sample1.client;
2+
3+
import demo.sample1.protos.HelloRequest;
4+
import demo.sample1.protos.HelloResponse;
5+
import demo.sample1.protos.HelloServiceGrpc;
6+
import demo.util.SimpleLogger;
7+
import io.grpc.ManagedChannel;
8+
import io.grpc.ManagedChannelBuilder;
9+
import java.util.Objects;
10+
11+
/**
12+
* https://github.com/eugenp/tutorials/tree/master/grpc/
13+
*/
14+
public class Sample1GrpcClient {
15+
16+
private String address;
17+
private int port;
18+
19+
public Sample1GrpcClient(String address, Integer port) {
20+
this.address = Objects.requireNonNull(address, "address must be not null");
21+
this.port = Objects.requireNonNull(port, "port must be not null");
22+
}
23+
24+
public void request() {
25+
ManagedChannel channel = ManagedChannelBuilder.forAddress(address, port)
26+
.usePlaintext()
27+
.build();
28+
29+
HelloServiceGrpc.HelloServiceBlockingStub stub = HelloServiceGrpc.newBlockingStub(channel);
30+
31+
HelloResponse helloResponse = stub.hello(
32+
HelloRequest.newBuilder()
33+
.setFirstName("zaccoding")
34+
.setLastName("gRPC")
35+
.build()
36+
);
37+
38+
SimpleLogger.info("Received gRPC response : {}", helloResponse);
39+
40+
channel.shutdown();
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* for protos
3+
*/
4+
package demo.sample1.protos;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package demo.sample1.server;
2+
3+
import demo.sample1.protos.HelloRequest;
4+
import demo.sample1.protos.HelloResponse;
5+
import demo.sample1.protos.HelloServiceGrpc.HelloServiceImplBase;
6+
import demo.util.SimpleLogger;
7+
import io.grpc.stub.StreamObserver;
8+
9+
/**
10+
* https://github.com/eugenp/tutorials/tree/master/grpc/
11+
*/
12+
public class HelloServiceImpl extends HelloServiceImplBase {
13+
14+
@Override
15+
public void hello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
16+
SimpleLogger.println("[Sample1HelloService] Request received from client : \n{}", request);
17+
18+
String greeting = new StringBuilder().append("Hello,")
19+
.append(request.getFirstName())
20+
.append(" ")
21+
.append(request.getLastName())
22+
.toString();
23+
24+
HelloResponse response = HelloResponse.newBuilder()
25+
.setGreeting(greeting)
26+
.build();
27+
28+
responseObserver.onNext(response);
29+
responseObserver.onCompleted();
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package demo.sample1.server;
2+
3+
import demo.util.SimpleLogger;
4+
import io.grpc.Server;
5+
import io.grpc.ServerBuilder;
6+
import java.util.Objects;
7+
8+
/**
9+
* https://github.com/eugenp/tutorials/tree/master/grpc/
10+
*/
11+
public class Sample1GrpcServer extends Thread {
12+
13+
private Server server;
14+
private int port;
15+
16+
public Sample1GrpcServer(Integer port) {
17+
super.setDaemon(true);
18+
this.port = Objects.requireNonNull(port, "port must be not null");
19+
}
20+
21+
@Override
22+
public void run() {
23+
if (isRunning()) {
24+
SimpleLogger.info("Already sample1 grpc server is running", null);
25+
return;
26+
}
27+
28+
try {
29+
SimpleLogger.info("[Server] Try to start sample1 grpc server with {}", port);
30+
server = ServerBuilder.forPort(port)
31+
.addService(new HelloServiceImpl())
32+
.build();
33+
34+
server = server.start();
35+
server.awaitTermination();
36+
} catch (Exception e) {
37+
if (isRunning()) {
38+
server.shutdown();
39+
}
40+
41+
if (e instanceof InterruptedException) {
42+
return;
43+
}
44+
SimpleLogger.error("[Server] Failed to start sample1 grcp server", e);
45+
}
46+
}
47+
48+
public boolean isRunning() {
49+
return server != null
50+
&& !server.isShutdown()
51+
&& !server.isTerminated();
52+
}
53+
}

0 commit comments

Comments
 (0)