Skip to content

Commit

Permalink
Implements the gRPC MP Client API. This API uses interfaces to create…
Browse files Browse the repository at this point in the history
… client proxies. It introduces two new annotations, GrpcProxy and GrpcChannel (also defined as part of the Grpc namespace). Client proxies can be injected into CDI beans to access remote gRPC services.
  • Loading branch information
spericas committed Jul 23, 2024
1 parent 7c40eb8 commit 5fae6e0
Show file tree
Hide file tree
Showing 32 changed files with 4,347 additions and 1 deletion.
24 changes: 24 additions & 0 deletions grpc/api/src/main/java/io/helidon/grpc/api/Grpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,28 @@ public interface Grpc {
*/
Class<?> value();
}

/**
* An annotation that can be used to specify the name of a configured gRPC channel.
* channel to inject, or the name of the host to connect to, as described in
*/
@Target({TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RUNTIME)
@interface GrpcChannel {

/**
* The name of the configured channel.
*
* @return name of the channel
*/
String value();
}

/**
* An annotation used to mark an injection point for a gRPC service client proxy.
*/
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
@Retention(RUNTIME)
@interface GrpcProxy {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
*/

/**
* Core classes used by both the gRPC server API and gRPC client API.
* Core classes used by both the gRPC server and gRPC client.
*/
package io.helidon.grpc.core;
161 changes: 161 additions & 0 deletions microprofile/grpc/client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019, 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.microprofile.grpc</groupId>
<artifactId>helidon-microprofile-grpc-project</artifactId>
<version>4.1.0-SNAPSHOT</version>
</parent>

<artifactId>helidon-microprofile-grpc-client</artifactId>
<name>Helidon Microprofile gRPC Client</name>

<dependencies>
<dependency>
<groupId>io.helidon.grpc</groupId>
<artifactId>helidon-grpc-core</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.grpc</groupId>
<artifactId>helidon-grpc-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.grpc</groupId>
<artifactId>helidon-microprofile-grpc-core</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.tracing</groupId>
<artifactId>helidon-tracing</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient-grpc</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-tls</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.codegen</groupId>
<artifactId>helidon-codegen</artifactId>
</dependency>
<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-grpc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.grpc</groupId>
<artifactId>helidon-microprofile-grpc-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
<scope>test</scope>
</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>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</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 in grpc sources
-->
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</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-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.helidon.builder</groupId>
<artifactId>helidon-builder-processor</artifactId>
<version>${helidon.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-compile</goal>
<goal>test-compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2019, 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.microprofile.grpc.client;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

import io.helidon.config.Config;
import io.helidon.grpc.api.Grpc;

import io.grpc.Channel;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.enterprise.inject.spi.InjectionPoint;
import jakarta.inject.Inject;

/**
* A producer of gRPC {@link io.grpc.Channel Channels}.
*/
@ApplicationScoped
public class ChannelProducer {

private final GrpcChannelsProvider provider;
private final ReentrantLock lock = new ReentrantLock();
private final Map<String, Channel> channelMap = new HashMap<>();

/**
* Create a {@link ChannelProducer}.
*
* @param config the {@link io.helidon.config.Config} to use to configure
* the provided {@link io.grpc.Channel}s
*/
@Inject
ChannelProducer(Config config) {
provider = GrpcChannelsProvider.create(config.get("grpc"));
}

/**
* Produces a gRPC {@link io.grpc.Channel}.
*
* @param injectionPoint the injection point
* @return a gRPC {@link io.grpc.Channel}
*/
@Produces
@Grpc.GrpcChannel(value = GrpcChannelsProvider.DEFAULT_CHANNEL_NAME)
public Channel get(InjectionPoint injectionPoint) {
Grpc.GrpcChannel qualifier = injectionPoint.getQualifiers()
.stream()
.filter(q -> q.annotationType().equals(Grpc.GrpcChannel.class))
.map(q -> (Grpc.GrpcChannel) q)
.findFirst()
.orElse(null);

String name = (qualifier == null) ? GrpcChannelsProvider.DEFAULT_CHANNEL_NAME : qualifier.value();
return findChannel(name);
}

/**
* Produces the default gRPC {@link io.grpc.Channel}.
*
* @return the default gRPC {@link io.grpc.Channel}
*/
@Produces
public Channel getDefaultChannel() {
return findChannel(GrpcChannelsProvider.DEFAULT_CHANNEL_NAME);
}

/**
* Obtain the named {@link io.grpc.Channel}.
*
* @param name the channel name
* @return the named {@link io.grpc.Channel}
*/
public Channel findChannel(String name) {
try {
lock.lock();
return channelMap.computeIfAbsent(name, provider::channel);
} finally {
lock.unlock();
}
}
}
Loading

0 comments on commit 5fae6e0

Please sign in to comment.