From d0d26f58f10be05ed62e85c828585a9137a00081 Mon Sep 17 00:00:00 2001 From: Santiago Pericas-Geertsen Date: Fri, 2 Aug 2024 14:32:45 -0400 Subject: [PATCH 1/2] New MP gRPC end-to-end example that uses new gRPC proxy-based API. Signed-off-by: Santiago Pericas-Geertsen --- examples/microprofile/grpc/README.md | 15 ++ examples/microprofile/grpc/pom.xml | 114 +++++++++++++++ .../microprofile/grpc/StringService.java | 88 ++++++++++++ .../grpc/StringServiceClient.java | 68 +++++++++ .../microprofile/grpc/package-info.java | 16 +++ .../grpc/src/main/proto/strings.proto | 30 ++++ .../src/main/resources/META-INF/beans.xml | 25 ++++ .../grpc/src/main/resources/application.yaml | 48 +++++++ .../grpc/src/main/resources/client.p12 | Bin 0 -> 4181 bytes .../src/main/resources/logging.properties | 29 ++++ .../grpc/src/main/resources/server.p12 | Bin 0 -> 4133 bytes .../microprofile/grpc/StringServiceTest.java | 130 ++++++++++++++++++ examples/microprofile/pom.xml | 1 + 13 files changed, 564 insertions(+) create mode 100644 examples/microprofile/grpc/README.md create mode 100644 examples/microprofile/grpc/pom.xml create mode 100644 examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/StringService.java create mode 100644 examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/StringServiceClient.java create mode 100644 examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/package-info.java create mode 100644 examples/microprofile/grpc/src/main/proto/strings.proto create mode 100644 examples/microprofile/grpc/src/main/resources/META-INF/beans.xml create mode 100644 examples/microprofile/grpc/src/main/resources/application.yaml create mode 100644 examples/microprofile/grpc/src/main/resources/client.p12 create mode 100644 examples/microprofile/grpc/src/main/resources/logging.properties create mode 100644 examples/microprofile/grpc/src/main/resources/server.p12 create mode 100644 examples/microprofile/grpc/src/test/java/io/helidon/examples/microprofile/grpc/StringServiceTest.java diff --git a/examples/microprofile/grpc/README.md b/examples/microprofile/grpc/README.md new file mode 100644 index 000000000..4fa6c5906 --- /dev/null +++ b/examples/microprofile/grpc/README.md @@ -0,0 +1,15 @@ +# Helidon gRPC MP Example + +This examples shows a simple application written using Helidon gRPC MP API: + +- StringService: a gRPC service implementation that uses MP +- StringServiceClient: an interface from which a client proxy can be created to call StringService remote methods +- StringServiceTest: a sample test that starts a server and tests the client and server components +- application.yaml: configuration for server and client channels + +## Build and run + +```shell +mvn package +java -jar target/helidon-examples-microprofile-grpc.jar +``` \ No newline at end of file diff --git a/examples/microprofile/grpc/pom.xml b/examples/microprofile/grpc/pom.xml new file mode 100644 index 000000000..eadf8814a --- /dev/null +++ b/examples/microprofile/grpc/pom.xml @@ -0,0 +1,114 @@ + + + + 4.0.0 + + io.helidon.applications + helidon-mp + 4.1.0-SNAPSHOT + ../../../applications/mp/pom.xml + + io.helidon.examples.microprofile + helidon-examples-microprofile-grpc + Helidon Examples Microprofile gRPC + + + Microprofile example that uses gRPC + + + + + io.helidon.grpc + helidon-grpc-core + + + io.helidon.microprofile.grpc + helidon-microprofile-grpc-core + + + io.helidon.microprofile.grpc + helidon-microprofile-grpc-server + + + io.helidon.microprofile.grpc + helidon-microprofile-grpc-client + + + io.helidon.microprofile.testing + helidon-microprofile-testing-junit5 + test + + + io.helidon.webserver.testing.junit5 + helidon-webserver-testing-junit5-grpc + test + + + io.helidon.logging + helidon-logging-jul + runtime + + + + javax.annotation + javax.annotation-api + provided + true + + + + + + + + kr.motd.maven + os-maven-plugin + ${version.plugin.os} + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-libs + + + + + org.xolstice.maven.plugins + protobuf-maven-plugin + + + + compile + test-compile + + + + + + + \ No newline at end of file diff --git a/examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/StringService.java b/examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/StringService.java new file mode 100644 index 000000000..046793bd5 --- /dev/null +++ b/examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/StringService.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.examples.microprofile.grpc; + +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import io.helidon.grpc.api.Grpc; +import io.helidon.grpc.core.CollectingObserver; + +import io.grpc.stub.StreamObserver; +import jakarta.enterprise.context.ApplicationScoped; + +/** + * An implementation of a string service. + */ +@Grpc.GrpcService +@ApplicationScoped +public class StringService { + + /** + * Uppercase a string. + * + * @param request string message + * @return string message + */ + @Grpc.Unary("Upper") + public Strings.StringMessage upper(Strings.StringMessage request) { + return newMessage(request.getText().toUpperCase()); + } + + /** + * Lowercase a string. + * + * @param request string message + * @return string message + */ + @Grpc.Unary("Lower") + public Strings.StringMessage lower(Strings.StringMessage request) { + return newMessage(request.getText().toLowerCase()); + } + + /** + * Split a string using space delimiters. + * + * @param request string message + * @return stream of string messages + */ + @Grpc.ServerStreaming("Split") + public Stream split(Strings.StringMessage request) { + String[] parts = request.getText().split(" "); + return Stream.of(parts).map(this::newMessage); + } + + /** + * Join a stream of messages using spaces. + * + * @param observer stream of messages + * @return single message as a stream + */ + @Grpc.ClientStreaming("Join") + public StreamObserver join(StreamObserver observer) { + return CollectingObserver.create( + Collectors.joining(" "), + observer, + Strings.StringMessage::getText, + this::newMessage); + } + + private Strings.StringMessage newMessage(String text) { + return Strings.StringMessage.newBuilder().setText(text).build(); + } +} + diff --git a/examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/StringServiceClient.java b/examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/StringServiceClient.java new file mode 100644 index 000000000..b03ee3e90 --- /dev/null +++ b/examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/StringServiceClient.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.examples.microprofile.grpc; + +import java.util.stream.Stream; + +import io.helidon.grpc.api.Grpc; + +import io.grpc.stub.StreamObserver; + +/** + * A client for a {@link StringService}. + */ +@Grpc.GrpcService("StringService") +@Grpc.GrpcChannel("string-channel") // see application.yaml +public interface StringServiceClient { + + /** + * Uppercase a string. + * + * @param request string message + * @return string message + */ + @Grpc.Unary("Upper") + Strings.StringMessage upper(Strings.StringMessage request); + + /** + * Lowercase a string. + * + * @param request string message + * @return string message + */ + @Grpc.Unary("Lower") + Strings.StringMessage lower(Strings.StringMessage request); + + /** + * Split a string using space delimiters. + * + * @param request string message + * @return stream of string messages + */ + @Grpc.ServerStreaming("Split") + Stream split(Strings.StringMessage request); + + /** + * Join a stream of messages using spaces. + * + * @param observer stream of messages + * @return single message as a stream + */ + @Grpc.ClientStreaming("Join") + StreamObserver join(StreamObserver observer); +} + diff --git a/examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/package-info.java b/examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/package-info.java new file mode 100644 index 000000000..fbf691b80 --- /dev/null +++ b/examples/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/package-info.java @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.examples.microprofile.grpc; diff --git a/examples/microprofile/grpc/src/main/proto/strings.proto b/examples/microprofile/grpc/src/main/proto/strings.proto new file mode 100644 index 000000000..48c8bd30d --- /dev/null +++ b/examples/microprofile/grpc/src/main/proto/strings.proto @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +syntax = "proto3"; +option java_package = "io.helidon.examples.microprofile.grpc"; + +service StringService { + rpc Upper (StringMessage) returns (StringMessage) {} + rpc Lower (StringMessage) returns (StringMessage) {} + rpc Split (StringMessage) returns (stream StringMessage) {} + rpc Join (stream StringMessage) returns (StringMessage) {} +} + +message StringMessage { + string text = 1; +} diff --git a/examples/microprofile/grpc/src/main/resources/META-INF/beans.xml b/examples/microprofile/grpc/src/main/resources/META-INF/beans.xml new file mode 100644 index 000000000..69f490f1a --- /dev/null +++ b/examples/microprofile/grpc/src/main/resources/META-INF/beans.xml @@ -0,0 +1,25 @@ + + + + diff --git a/examples/microprofile/grpc/src/main/resources/application.yaml b/examples/microprofile/grpc/src/main/resources/application.yaml new file mode 100644 index 000000000..fdc488cfd --- /dev/null +++ b/examples/microprofile/grpc/src/main/resources/application.yaml @@ -0,0 +1,48 @@ +# +# Copyright (c) 2024 Oracle and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +server: + port: 0 + tls: + trust: + keystore: + passphrase: "password" + trust-store: true + resource: + resource-path: "server.p12" + private-key: + keystore: + passphrase: "password" + resource: + resource-path: "server.p12" + +grpc: + client: + channels: + - name: "string-channel" + port: 0 + tls: + trust: + keystore: + passphrase: "password" + trust-store: true + resource: + resource-path: "client.p12" + private-key: + keystore: + passphrase: "password" + resource: + resource-path: "client.p12" \ No newline at end of file diff --git a/examples/microprofile/grpc/src/main/resources/client.p12 b/examples/microprofile/grpc/src/main/resources/client.p12 new file mode 100644 index 0000000000000000000000000000000000000000..4eb3b8325cd0190163032aca86e1d4fc94856822 GIT binary patch literal 4181 zcmY+EWmFW5wuWIGKw^;Y8d`FO4r!R7kra{cRN$kVp>ybzA%_qK=@29&1VMz6MnqaE zk#3Inu6xf}=f_@qt>@kQ{rNx;WcmQy2M`1q5k4Vbta|J@2$ul21o7w^hgIE5dsUMBr)v#p-`Y6`%P3zDS600VN3FH3R`Xhdjn7`hWbloDoRH)IP6+WcmE9 z`35_>AJFE&UKlM8cz`qTw+lF;|4Y44{>C>)$b58Pwv%K0R(%IXj}~QHjD4NBVYZjc zF@t^Op?!4hS2W1WsUs*fH4rKzE`)r~ui`YVNmNGREmp`fmhZ-l6^5sfB`zPY40}#g zH)^o?@o9hBqDnY3fTdikVzhQYjJ@QFySfUF2zyQ%MJkSx&6yp$d%0u5zcM8tP1i^6 z%}M{O^-J(q-l}? zG;sYG<`hB67OAJ{Kn7v!iM%17ZGOW*_+6?mj_PPjWm!Ul8pMIbf!M^ zWgBVP1D^1uoR+3$()AEJwCJQVGFXYpP?{E!vJd6qcaPa-_wk2fi>CtS#t_Q=YzDhg z_T{E?`*O_~!8+lV;#JIN%WYb`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K$ZPl4%!zuON?jp_ux)7X81p9@7<=7o)muQ!`VU*(U_GMZtu2O?fDpn4;&s;Q(e zRGDC(^{49^Z4ZPC^eY6YNJjucoEOc70koBr&K)(r+s?@g^Se%SH`l9uWoVU9k8~!7 zSm(Da`qf`K>*l$GR3fX#>*?3KSLGipLS>5eZQT02fellR^9>>##$oNHE}<85p%~Q7 z%_(}1jZoEc^nl$(=_+s4AiZQK^f5uJuh zyx{G`iKeIyY%fRaL#e4KJl~qBe?H-g4xGk|wmuFpt&b6U)UaK7xbX7Trn9x!O|I0d zM0C&$Crky#52J>FMHwh5IKcoA3);!)%GXyMs9+@{1qF^WmEsdjJ%}afv*WKBoMu}7 U@+;F6i_J(6+nRj}N+2@-U-0Veh5!Hn literal 0 HcmV?d00001 diff --git a/examples/microprofile/grpc/src/test/java/io/helidon/examples/microprofile/grpc/StringServiceTest.java b/examples/microprofile/grpc/src/test/java/io/helidon/examples/microprofile/grpc/StringServiceTest.java new file mode 100644 index 000000000..b6e9410f1 --- /dev/null +++ b/examples/microprofile/grpc/src/test/java/io/helidon/examples/microprofile/grpc/StringServiceTest.java @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.examples.microprofile.grpc; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; + +import io.helidon.grpc.api.Grpc; +import io.helidon.microprofile.grpc.client.GrpcConfigurablePort; +import io.helidon.microprofile.testing.junit5.HelidonTest; + +import io.grpc.stub.StreamObserver; +import jakarta.inject.Inject; +import jakarta.ws.rs.client.WebTarget; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.hasSize; + +@HelidonTest +class StringServiceTest { + + @Inject + private WebTarget webTarget; + + @Inject + @Grpc.GrpcProxy + private StringServiceClient client; + + @BeforeEach + void updatePort() { + if (client instanceof GrpcConfigurablePort c) { + c.channelPort(webTarget.getUri().getPort()); + } + } + + @Test + void testUnaryUpper() { + Strings.StringMessage res = client.upper(newMessage("hello")); + assertThat(res.getText(), is("HELLO")); + } + + @Test + void testUnaryLower() { + Strings.StringMessage res = client.lower(newMessage("HELLO")); + assertThat(res.getText(), is("hello")); + } + + @Test + void testServerStreamingSplit() { + Stream stream = client.split(newMessage("hello world")); + List value = stream.toList(); + assertThat(value, hasSize(2)); + assertThat(value, contains(newMessage("hello"), newMessage("world"))); + } + + @Test + void testClientStreamingJoin() throws InterruptedException { + ListObserver response = new ListObserver<>(); + StreamObserver request = client.join(response); + request.onNext(newMessage("hello")); + request.onNext(newMessage("world")); + request.onCompleted(); + List value = response.value(); + assertThat(value.getFirst(), is(newMessage("hello world"))); + } + + /** + * Helper method to create a string message from a string. + * + * @param data the string + * @return the string message + */ + Strings.StringMessage newMessage(String data) { + return Strings.StringMessage.newBuilder().setText(data).build(); + } + + /** + * Helper class to collect a list of observed values. + * + * @param the type of values + */ + static class ListObserver implements StreamObserver { + private static final long TIMEOUT_SECONDS = 10; + + private List value = new ArrayList<>(); + private final CountDownLatch latch = new CountDownLatch(1); + + public List value() throws InterruptedException { + boolean b = latch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS); + assert b; + return value; + } + + @Override + public void onNext(T value) { + this.value.add(value); + } + + @Override + public void onError(Throwable t) { + value = null; + } + + @Override + public void onCompleted() { + latch.countDown(); + } + } +} + diff --git a/examples/microprofile/pom.xml b/examples/microprofile/pom.xml index a4ce7bcab..1f3716afc 100644 --- a/examples/microprofile/pom.xml +++ b/examples/microprofile/pom.xml @@ -52,5 +52,6 @@ http-status-count-mp lra telemetry + grpc From 623f979ce7dae6e119067d3b721334bdfd385e12 Mon Sep 17 00:00:00 2001 From: Santiago Pericas-Geertsen Date: Tue, 13 Aug 2024 11:07:30 -0400 Subject: [PATCH 2/2] Fixes relative path in pom.xml Signed-off-by: Santiago Pericas-Geertsen --- examples/microprofile/grpc/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/microprofile/grpc/pom.xml b/examples/microprofile/grpc/pom.xml index eadf8814a..fcd36e798 100644 --- a/examples/microprofile/grpc/pom.xml +++ b/examples/microprofile/grpc/pom.xml @@ -24,7 +24,7 @@ io.helidon.applications helidon-mp 4.1.0-SNAPSHOT - ../../../applications/mp/pom.xml + io.helidon.examples.microprofile helidon-examples-microprofile-grpc