Skip to content

Commit 8a3242e

Browse files
committed
Add Armeria gRPC server and client examples
1 parent 888351f commit 8a3242e

File tree

8 files changed

+446
-0
lines changed

8 files changed

+446
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
<parent>
6+
<artifactId>armeria-example</artifactId>
7+
<groupId>com.example.armeria</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>armeria-grpc-client</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>${project.groupId}</groupId>
18+
<artifactId>armeria-grpc-server</artifactId>
19+
<version>${project.version}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.linecorp.armeria</groupId>
23+
<artifactId>armeria-grpc</artifactId>
24+
<version>0.99.2</version>
25+
</dependency>
26+
</dependencies>
27+
28+
<build>
29+
<extensions>
30+
<extension>
31+
<groupId>kr.motd.maven</groupId>
32+
<artifactId>os-maven-plugin</artifactId>
33+
</extension>
34+
</extensions>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.xolstice.maven.plugins</groupId>
38+
<artifactId>protobuf-maven-plugin</artifactId>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
</project>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.exmaple.grpc.armeria;
2+
3+
4+
import java.util.concurrent.CountDownLatch;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import com.example.grpc.armeria.Hello.HelloReply;
10+
import com.example.grpc.armeria.Hello.HelloRequest;
11+
import com.example.grpc.armeria.HelloServiceGrpc.HelloServiceBlockingStub;
12+
import com.example.grpc.armeria.HelloServiceGrpc.HelloServiceFutureStub;
13+
import com.example.grpc.armeria.HelloServiceGrpc.HelloServiceStub;
14+
import com.google.common.util.concurrent.FutureCallback;
15+
import com.google.common.util.concurrent.Futures;
16+
import com.google.common.util.concurrent.ListenableFuture;
17+
import com.google.common.util.concurrent.MoreExecutors;
18+
19+
import com.linecorp.armeria.client.Clients;
20+
21+
import io.grpc.stub.StreamObserver;
22+
23+
public final class ArmeriaGrpcClient {
24+
25+
private static final Logger logger = LoggerFactory.getLogger(ArmeriaGrpcClient.class);
26+
27+
public static void main(String[] args) throws InterruptedException {
28+
final String uri = "gproto+http://127.0.0.1:8080/";
29+
30+
// Creates blocking gRPC client
31+
final HelloServiceBlockingStub blockingStub = Clients.newClient(uri, HelloServiceBlockingStub.class);
32+
final String blockingResponse = blockingStub.hello(HelloRequest.newBuilder().setName("Armeria").build())
33+
.getMessage();
34+
logger.info("Reply of 'hello': {}", blockingResponse);
35+
36+
// Creates non-blocking gRPC client with Guava ListenableFuture
37+
final HelloServiceFutureStub futureStub = Clients.newClient(uri, HelloServiceFutureStub.class);
38+
final ListenableFuture<HelloReply> future =
39+
futureStub.lazyHello(HelloRequest.newBuilder().setName("Armeria").build());
40+
41+
final CountDownLatch countDownLatch1 = new CountDownLatch(1);
42+
Futures.addCallback(future, new FutureCallback<HelloReply>() {
43+
@Override
44+
public void onSuccess(HelloReply result) {
45+
logger.info("Reply of 'lazyHello': {}", result.getMessage());
46+
countDownLatch1.countDown();
47+
}
48+
49+
@Override
50+
public void onFailure(Throwable t) {
51+
throw new Error(t); // Should never reach here.
52+
}
53+
}, MoreExecutors.directExecutor());
54+
55+
countDownLatch1.await();
56+
57+
// Creates non-blocking gRPC client with StreamObserver
58+
final HelloServiceStub helloService = Clients.newClient(uri, HelloServiceStub.class);
59+
final CountDownLatch countDownLatch2 = new CountDownLatch(1);
60+
helloService.lotsOfReplies(
61+
HelloRequest.newBuilder().setName("Armeria").build(),
62+
new StreamObserver<HelloReply>() {
63+
64+
@Override
65+
public void onNext(HelloReply value) {
66+
logger.info("Reply of 'lotsOfReplies: {}", value.getMessage());
67+
}
68+
69+
@Override
70+
public void onError(Throwable t) {
71+
throw new Error(t); // Should never reach here.
72+
}
73+
74+
@Override
75+
public void onCompleted() {
76+
countDownLatch2.countDown();
77+
}
78+
});
79+
countDownLatch2.await();
80+
}
81+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
<parent>
6+
<artifactId>armeria-example</artifactId>
7+
<groupId>com.example.armeria</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>armeria-grpc-server</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.linecorp.armeria</groupId>
18+
<artifactId>armeria-grpc</artifactId>
19+
<version>0.99.2</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>io.projectreactor</groupId>
23+
<artifactId>reactor-core</artifactId>
24+
<version>3.3.4.RELEASE</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.slf4j</groupId>
28+
<artifactId>slf4j-simple</artifactId>
29+
<version>1.7.29</version>
30+
<scope>runtime</scope>
31+
</dependency>
32+
33+
</dependencies>
34+
35+
<build>
36+
<extensions>
37+
<extension>
38+
<groupId>kr.motd.maven</groupId>
39+
<artifactId>os-maven-plugin</artifactId>
40+
</extension>
41+
</extensions>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.xolstice.maven.plugins</groupId>
45+
<artifactId>protobuf-maven-plugin</artifactId>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
</project>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.example.grpc.armeria;
2+
3+
import java.net.InetSocketAddress;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import com.example.grpc.armeria.Hello.HelloRequest;
9+
10+
import com.linecorp.armeria.common.grpc.GrpcSerializationFormats;
11+
import com.linecorp.armeria.server.HttpServiceWithRoutes;
12+
import com.linecorp.armeria.server.Server;
13+
import com.linecorp.armeria.server.docs.DocService;
14+
import com.linecorp.armeria.server.docs.DocServiceFilter;
15+
import com.linecorp.armeria.server.grpc.GrpcService;
16+
17+
import io.grpc.protobuf.services.ProtoReflectionService;
18+
import io.grpc.reflection.v1alpha.ServerReflectionGrpc;
19+
20+
public final class ArmeriaGrpcServer {
21+
22+
private static final Logger logger = LoggerFactory.getLogger(ArmeriaGrpcServer.class);
23+
24+
public static void main(String[] args) throws Exception {
25+
final Server server = newServer(8080, 8443);
26+
27+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
28+
server.stop().join();
29+
logger.info("Server has been stopped.");
30+
}));
31+
32+
server.start().join();
33+
final InetSocketAddress localAddress = server.activePort().localAddress();
34+
final boolean isLocalAddress = localAddress.getAddress().isAnyLocalAddress() ||
35+
localAddress.getAddress().isLoopbackAddress();
36+
logger.info("Server has been started. Serving DocService at http://{}:{}/docs",
37+
isLocalAddress ? "127.0.0.1" : localAddress.getHostString(), localAddress.getPort());
38+
}
39+
40+
static Server newServer(int httpPort, int httpsPort) throws Exception {
41+
final HelloRequest exampleRequest = HelloRequest.newBuilder().setName("Armeria").build();
42+
final HttpServiceWithRoutes grpcService =
43+
GrpcService.builder()
44+
.addService(new HelloServiceImpl())
45+
// See https://github.com/grpc/grpc-java/blob/master/documentation/server-reflection-tutorial.md
46+
.addService(ProtoReflectionService.newInstance())
47+
.supportedSerializationFormats(GrpcSerializationFormats.values())
48+
.enableUnframedRequests(true)
49+
// You can set useBlockingTaskExecutor(true) in order to execute all gRPC
50+
// methods in the blockingTaskExecutor thread pool.
51+
// .useBlockingTaskExecutor(true)
52+
.build();
53+
54+
return Server.builder()
55+
.http(httpPort)
56+
.https(httpsPort)
57+
.tlsSelfSigned()
58+
.service(grpcService)
59+
// You can access the documentation service at http://127.0.0.1:8080/docs.
60+
// See https://line.github.io/armeria/server-docservice.html for more information.
61+
.serviceUnder("/docs", DocService.builder()
62+
.exampleRequestForMethod(HelloServiceGrpc.SERVICE_NAME,
63+
"Hello", exampleRequest)
64+
.exampleRequestForMethod(HelloServiceGrpc.SERVICE_NAME,
65+
"LazyHello", exampleRequest)
66+
.exampleRequestForMethod(HelloServiceGrpc.SERVICE_NAME,
67+
"BlockingHello", exampleRequest)
68+
.exclude(DocServiceFilter.ofServiceName(
69+
ServerReflectionGrpc.SERVICE_NAME))
70+
.build())
71+
.build();
72+
}
73+
74+
private ArmeriaGrpcServer() {}
75+
}

0 commit comments

Comments
 (0)