Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New MP gRPC end-to-end example that uses gRPC proxy-based API #65

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/microprofile/grpc/README.md
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
```
114 changes: 114 additions & 0 deletions examples/microprofile/grpc/pom.xml
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>
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();
}
}

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);
}

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;
30 changes: 30 additions & 0 deletions examples/microprofile/grpc/src/main/proto/strings.proto
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 examples/microprofile/grpc/src/main/resources/META-INF/beans.xml
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>
Loading