-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements the gRPC MP Client API. This API uses interfaces to create…
… 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
Showing
32 changed files
with
4,347 additions
and
1 deletion.
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
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
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,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> |
97 changes: 97 additions & 0 deletions
97
...rofile/grpc/client/src/main/java/io/helidon/microprofile/grpc/client/ChannelProducer.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,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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.