-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New MP gRPC end-to-end example that uses gRPC proxy-based API (#65)
New MP gRPC end-to-end example that uses new gRPC proxy-based API. Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
- Loading branch information
Showing
13 changed files
with
564 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,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 | ||
``` |
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,114 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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. | ||
--> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.helidon.applications</groupId> | ||
<artifactId>helidon-mp</artifactId> | ||
<version>4.1.0-SNAPSHOT</version> | ||
<relativePath/> | ||
</parent> | ||
<groupId>io.helidon.examples.microprofile</groupId> | ||
<artifactId>helidon-examples-microprofile-grpc</artifactId> | ||
<name>Helidon Examples Microprofile gRPC</name> | ||
|
||
<description> | ||
Microprofile example that uses gRPC | ||
</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.helidon.grpc</groupId> | ||
<artifactId>helidon-grpc-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.microprofile.grpc</groupId> | ||
<artifactId>helidon-microprofile-grpc-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.microprofile.grpc</groupId> | ||
<artifactId>helidon-microprofile-grpc-server</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.microprofile.grpc</groupId> | ||
<artifactId>helidon-microprofile-grpc-client</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.microprofile.testing</groupId> | ||
<artifactId>helidon-microprofile-testing-junit5</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.webserver.testing.junit5</groupId> | ||
<artifactId>helidon-webserver-testing-junit5-grpc</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.logging</groupId> | ||
<artifactId>helidon-logging-jul</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<!-- | ||
Need to use this version to work around the same groupId/artifactId and | ||
different package names in Jakarta. This is needed for the Generated annotation. | ||
--> | ||
<groupId>javax.annotation</groupId> | ||
<artifactId>javax.annotation-api</artifactId> | ||
<scope>provided</scope> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<extensions> | ||
<extension> | ||
<groupId>kr.motd.maven</groupId> | ||
<artifactId>os-maven-plugin</artifactId> | ||
<version>${version.plugin.os}</version> | ||
</extension> | ||
</extensions> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-dependency-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>copy-libs</id> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.xolstice.maven.plugins</groupId> | ||
<artifactId>protobuf-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>compile</goal> | ||
<goal>test-compile</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
88 changes: 88 additions & 0 deletions
88
.../microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/StringService.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,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<Strings.StringMessage> 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<Strings.StringMessage> join(StreamObserver<Strings.StringMessage> 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(); | ||
} | ||
} | ||
|
68 changes: 68 additions & 0 deletions
68
...profile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/StringServiceClient.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,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<Strings.StringMessage> 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<Strings.StringMessage> join(StreamObserver<Strings.StringMessage> observer); | ||
} | ||
|
16 changes: 16 additions & 0 deletions
16
...s/microprofile/grpc/src/main/java/io/helidon/examples/microprofile/grpc/package-info.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,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; |
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,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; | ||
} |
25 changes: 25 additions & 0 deletions
25
examples/microprofile/grpc/src/main/resources/META-INF/beans.xml
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,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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. | ||
--> | ||
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee | ||
https://jakarta.ee/xml/ns/jakartaee/beans_3_0.xsd" | ||
version="3.0" | ||
bean-discovery-mode="annotated"> | ||
</beans> |
Oops, something went wrong.