Skip to content

Commit

Permalink
Merge pull request #17834 from mkouba/grpc-client-naming-cleanup
Browse files Browse the repository at this point in the history
gRPC - clarify naming for clients
  • Loading branch information
cescoffier authored Jun 10, 2021
2 parents 5e9422d + 69c25e8 commit b0b4550
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions docs/src/main/asciidoc/grpc-getting-started.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public class ExampleResource {
}
}
----
<1> Inject the service and configure its name. The service name is used in the application configuration. If not specified then the field name is used instead: `hello` in this particular case.
<1> Inject the service and configure its name. The name is used in the application configuration. If not specified then the field name is used instead: `hello` in this particular case.
<2> Use the generated service interface based on Mutiny API.
<3> Invoke the service.

Expand All @@ -277,7 +277,7 @@ In the `src/main/resources/application.properties` file, add the following prope
quarkus.grpc.clients.hello.host=localhost
----

- `hello` is the name of the service used in the `@GrpcClient` annotation.
- `hello` is the name used in the `@GrpcClient` annotation.
- `host` configures the service host (here it's localhost).

Then, open http://localhost:8080/hello/quarkus in a browser, and you should get `Hello quarkus`!
Expand Down
8 changes: 4 additions & 4 deletions docs/src/main/asciidoc/grpc-service-consumption.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class MyBean {
}
----
<1> A gRPC client injection point must be annotated with the `@GrpcClient` qualifier. This qualifier can be used to specify the service name that is used to configure the underlying gRPC client. For example, if you set it to `hello-service`, configuring the host of the service is done using the `quarkus.grpc.clients.**hello-service**.host`.
<2> If the service name is not specified via the `GrpcClient#value()` then the field name is used instead, e.g. `helloService` in this particular case.
<1> A gRPC client injection point must be annotated with the `@GrpcClient` qualifier. This qualifier can be used to specify the name that is used to configure the underlying gRPC client. For example, if you set it to `hello-service`, configuring the host of the service is done using the `quarkus.grpc.clients.**hello-service**.host`.
<2> If the name is not specified via the `GrpcClient#value()` then the field name is used instead, e.g. `helloService` in this particular case.

The stub class names are derived from the service name used in your `proto` file.
For example, if you use `Greeter` as a service name as in:
Expand Down Expand Up @@ -205,9 +205,9 @@ For each gRPC service you inject in your application, you can configure the foll

include::{generated-dir}/config/quarkus-grpc-config-group-config-grpc-client-configuration.adoc[opts=optional, leveloffset=+1]

The `service-name` is the name set in the `@GrpcClient` or derived from the injection point if not explicitly defined.
The `client-name` is the name set in the `@GrpcClient` or derived from the injection point if not explicitly defined.

The following examples uses _hello_ as service name.
The following examples uses _hello_ as the client name.
Don't forget to replace it with the name you used in in the `@GrpcClient` annotation.

=== Enabling TLS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

public final class GrpcClientBuildItem extends MultiBuildItem {

private final String serviceName;
private final String clientName;
private final Set<ClientInfo> clients;

public GrpcClientBuildItem(String name) {
this.serviceName = name;
this.clientName = name;
this.clients = new HashSet<>();
}

Expand All @@ -26,8 +26,8 @@ public void addClient(ClientInfo client) {
clients.add(client);
}

public String getServiceName() {
return serviceName;
public String getClientName() {
return clientName;
}

public static final class ClientInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void registerBeans(BuildProducer<AdditionalBeanBuildItem> beans) {

@BuildStep
void discoverInjectedGrpcServices(BeanDiscoveryFinishedBuildItem beanDiscovery,
BuildProducer<GrpcClientBuildItem> services,
BuildProducer<GrpcClientBuildItem> clients,
BuildProducer<FeatureBuildItem> features,
CombinedIndexBuildItem index) {

Expand Down Expand Up @@ -105,17 +105,17 @@ void discoverInjectedGrpcServices(BeanDiscoveryFinishedBuildItem beanDiscovery,
continue;
}

String serviceName;
AnnotationValue serviceNameValue = clientAnnotation.value();
if (serviceNameValue == null || serviceNameValue.asString().equals(GrpcClient.ELEMENT_NAME)) {
String clientName;
AnnotationValue clientNameValue = clientAnnotation.value();
if (clientNameValue == null || clientNameValue.asString().equals(GrpcClient.ELEMENT_NAME)) {
// Determine the service name from the annotated element
if (clientAnnotation.target().kind() == Kind.FIELD) {
serviceName = clientAnnotation.target().asField().name();
clientName = clientAnnotation.target().asField().name();
} else if (clientAnnotation.target().kind() == Kind.METHOD_PARAMETER) {
MethodParameterInfo param = clientAnnotation.target().asMethodParameter();
serviceName = param.method().parameterName(param.position());
if (serviceName == null) {
throw new DeploymentException("Unable to determine the service name from the parameter at position "
clientName = param.method().parameterName(param.position());
if (clientName == null) {
throw new DeploymentException("Unable to determine the client name from the parameter at position "
+ param.position()
+ " in method "
+ param.method().declaringClass().name() + "#" + param.method().name()
Expand All @@ -126,20 +126,20 @@ void discoverInjectedGrpcServices(BeanDiscoveryFinishedBuildItem beanDiscovery,
throw new IllegalStateException(clientAnnotation + " may not be declared at " + clientAnnotation.target());
}
} else {
serviceName = serviceNameValue.asString();
clientName = clientNameValue.asString();
}

if (serviceName.trim().isEmpty()) {
if (clientName.trim().isEmpty()) {
throw new DeploymentException(
"Invalid @GrpcClient `" + injectionPoint.getTargetInfo() + "` - service name cannot be empty");
"Invalid @GrpcClient `" + injectionPoint.getTargetInfo() + "` - client name cannot be empty");
}

GrpcClientBuildItem item;
if (items.containsKey(serviceName)) {
item = items.get(serviceName);
if (items.containsKey(clientName)) {
item = items.get(clientName);
} else {
item = new GrpcClientBuildItem(serviceName);
items.put(serviceName, item);
item = new GrpcClientBuildItem(clientName);
items.put(clientName, item);
}

Type injectionType = injectionPoint.getRequiredType();
Expand Down Expand Up @@ -178,18 +178,18 @@ void discoverInjectedGrpcServices(BeanDiscoveryFinishedBuildItem beanDiscovery,

if (!items.isEmpty()) {
for (GrpcClientBuildItem item : items.values()) {
services.produce(item);
LOGGER.debugf("Detected GrpcService associated with the '%s' configuration prefix", item.getServiceName());
clients.produce(item);
LOGGER.debugf("Detected GrpcService associated with the '%s' configuration prefix", item.getClientName());
}
features.produce(new FeatureBuildItem(GRPC_CLIENT));
}
}

@BuildStep
public void generateGrpcServicesProducers(List<GrpcClientBuildItem> clients,
public void generateGrpcClientProducers(List<GrpcClientBuildItem> clients,
BuildProducer<SyntheticBeanBuildItem> syntheticBeans) {

for (GrpcClientBuildItem svcClients : clients) {
for (GrpcClientBuildItem client : clients) {
// For every service we register:
// 1. the channel
// 2. the blocking stub - if needed
Expand All @@ -200,26 +200,26 @@ public void generateGrpcServicesProducers(List<GrpcClientBuildItem> clients,
// bean that provides the GrpcClientConfiguration for the specific service.

syntheticBeans.produce(SyntheticBeanBuildItem.configure(GrpcDotNames.CHANNEL)
.addQualifier().annotation(GrpcDotNames.GRPC_CLIENT).addValue("value", svcClients.getServiceName()).done()
.addQualifier().annotation(GrpcDotNames.GRPC_CLIENT).addValue("value", client.getClientName()).done()
.scope(Singleton.class)
.unremovable()
.creator(new Consumer<MethodCreator>() {
@Override
public void accept(MethodCreator mc) {
GrpcClientProcessor.this.generateChannelProducer(mc, svcClients);
GrpcClientProcessor.this.generateChannelProducer(mc, client);
}
})
.destroyer(Channels.ChannelDestroyer.class).done());

String svcName = svcClients.getServiceName();
for (ClientInfo client : svcClients.getClients()) {
syntheticBeans.produce(SyntheticBeanBuildItem.configure(client.className)
String svcName = client.getClientName();
for (ClientInfo clientInfo : client.getClients()) {
syntheticBeans.produce(SyntheticBeanBuildItem.configure(clientInfo.className)
.addQualifier().annotation(GrpcDotNames.GRPC_CLIENT).addValue("value", svcName).done()
.scope(Singleton.class)
.creator(new Consumer<MethodCreator>() {
@Override
public void accept(MethodCreator mc) {
GrpcClientProcessor.this.generateClientProducer(mc, svcName, client);
GrpcClientProcessor.this.generateClientProducer(mc, svcName, clientInfo);
}
}).done());
}
Expand Down Expand Up @@ -289,7 +289,7 @@ private DeploymentException invalidInjectionPoint(InjectionPointInfo injectionPo
}

private void generateChannelProducer(MethodCreator mc, GrpcClientBuildItem svc) {
ResultHandle name = mc.load(svc.getServiceName());
ResultHandle name = mc.load(svc.getClientName());
ResultHandle result = mc.invokeStaticMethod(CREATE_CHANNEL_METHOD, name);
mc.returnValue(result);
mc.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class GrpcConfiguration {
*/
@ConfigItem
@ConfigDocSection
@ConfigDocMapKey("service-name")
@ConfigDocMapKey("client-name")
public Map<String, GrpcClientConfiguration> clients;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
String ELEMENT_NAME = "<<element name>>";

/**
* The service name is used to configure the gRPC client, e.g. the location, TLS/SSL, etc.
* The name is used to configure the gRPC client, e.g. the location, TLS/SSL, etc.
*
* @return the service name
* @return the client name
*/
String value() default ELEMENT_NAME;

Expand Down

0 comments on commit b0b4550

Please sign in to comment.