Skip to content

add grpc client autowired #1309

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

add grpc client autowired #1309

wants to merge 5 commits into from

Conversation

seal90
Copy link
Contributor

@seal90 seal90 commented Apr 12, 2025

Description

add grpc client autowired @DaprGrpcClient

Issue reference

We strive to have all PR being opened based on an issue, where the problem or feature have been discussed prior to implementation.

Please reference the issue this PR will close: #1308

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • [✔] Code compiles correctly
  • Created/updated tests
  • Extended the documentation

@seal90 seal90 requested review from a team as code owners April 12, 2025 07:16
* Handlers Dapr gRPC client in spring boot.
* Searches for fields and methods in beans that are annotated with {@link DaprGrpcClient} and sets them.
*
* @author seal90 ([email protected])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seal90 we are not using author tags here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I delete it

@@ -21,6 +21,12 @@
<version>${project.parent.version}</version>
<optional>true</optional>
</dependency>
<dependency>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seal90 why do we need a new module?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my head a new module makes sense if we want to isolate the gRPC dependencies because we think they might clash with other dependencies on the client application.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The abilities possessed by DAPR:service invocation, publish/subscribe, workflow, statemanagement, bindings...
dapr-spring model: data(state), messageing, workflows
example page: https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples
I think we split the module by abilitie, so i create a new module.

public class DaprInvokeAutoConfiguration {

@Bean
@ConditionalOnProperty(name = "dapr.invoke.grpc.client.daprGrpcClient.enabled", havingValue = "true",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seal90 what's the use case for this? When will people enable this property?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if instead of having this, we have something like @EnableDaprWorkflows, but looking at this example: https://www.baeldung.com/spring-boot-grpc, there is no need to disable this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature is enabled by default. For users who only use the HTTP protocol, there will be no impact as they do not use @ DaprGrpcClient.
Due to the possibility that the matching logic initialized below may not be able to handle new API issues, such ashttps://github.com/grpc-ecosystem/grpc-spring/issues/1177. If the user is in a hurry to use the new API, they can close this area and implement this logic again on their own.

  private <T extends AbstractStub<T>> T createStub(final Class<T> stubClass, final Channel channel) {
    try {
      // Search for public static *Grpc#new*Stub(Channel)
      final Class<?> declaringClass = stubClass.getDeclaringClass();
      if (declaringClass != null) {
        for (final Method method : declaringClass.getMethods()) {
          final String name = method.getName();
          final int modifiers = method.getModifiers();
          final Parameter[] parameters = method.getParameters();
          if (name.startsWith("new") && name.endsWith("Stub")
              && Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)
              && method.getReturnType().isAssignableFrom(stubClass)
              && parameters.length == 1
              && Channel.class.equals(parameters[0].getType())) {
            return stubClass.cast(method.invoke(null, channel));
          }
        }
      }

      // Search for a public constructor *Stub(Channel)
      final Constructor<T> constructor = stubClass.getConstructor(Channel.class);
      return constructor.newInstance(channel);
    } catch (final Exception e) {
      throw new BeanInstantiationException(stubClass, "Failed to create gRPC client", e);
    }
  }

@RestController
public class HelloWorldController {

@DaprGrpcClient("producer-app")
Copy link
Contributor

@salaboy salaboy Apr 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seal90 ok.. I think I understand what is going on here..

Looking at this example: https://www.baeldung.com/spring-boot-grpc having it named @DaprGrpcClient makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea from https://github.com/grpc-ecosystem/grpc-spring.
At first, I wanted to expand its capabilities, but I found that it hasn't changed in a year, but this usage method is really useful.

@salaboy salaboy added this to the v1.15 milestone Apr 12, 2025
@salaboy
Copy link
Contributor

salaboy commented Apr 12, 2025

@seal90 can you please check the CI pipelines? we need to make sure that they don't fail with the new changes.
Spotbugs is not happy:

[INFO] Total bugs: 5
Error:  Medium: Public static io.dapr.examples.DaprExamplesProtos.getDescriptor() may expose internal representation by returning DaprExamplesProtos.descriptor [io.dapr.examples.DaprExamplesProtos] At DaprExamplesProtos.java:[line 1151] MS_EXPOSE_REP
Error:  Medium: io.dapr.examples.DaprExamplesProtos$HelloReply.getDefaultInstanceForType() may expose internal representation by returning DaprExamplesProtos$HelloReply.DEFAULT_INSTANCE [io.dapr.examples.DaprExamplesProtos$HelloReply] At DaprExamplesProtos.java:[line 1133] EI_EXPOSE_REP
Error:  Medium: Public static io.dapr.examples.DaprExamplesProtos$HelloReply.getDefaultInstance() may expose internal representation by returning DaprExamplesProtos$HelloReply.DEFAULT_INSTANCE [io.dapr.examples.DaprExamplesProtos$HelloReply] At DaprExamplesProtos.java:[line 1097] MS_EXPOSE_REP
Error:  Medium: io.dapr.examples.DaprExamplesProtos$HelloRequest.getDefaultInstanceForType() may expose internal representation by returning DaprExamplesProtos$HelloRequest.DEFAULT_INSTANCE [io.dapr.examples.DaprExamplesProtos$HelloRequest] At DaprExamplesProtos.java:[line 573] EI_EXPOSE_REP
Error:  Medium: Public static io.dapr.examples.DaprExamplesProtos$HelloRequest.getDefaultInstance() may expose internal representation by returning DaprExamplesProtos$HelloRequest.DEFAULT_INSTANCE [io.dapr.examples.DaprExamplesProtos$HelloRequest] At DaprExamplesProtos.java:[line 537] MS_EXPOSE_REP
[INFO] 

@seal90
Copy link
Contributor Author

seal90 commented Apr 12, 2025

@seal90 can you please check the CI pipelines? we need to make sure that they don't fail with the new changes. Spotbugs is not happy:

[INFO] Total bugs: 5
Error:  Medium: Public static io.dapr.examples.DaprExamplesProtos.getDescriptor() may expose internal representation by returning DaprExamplesProtos.descriptor [io.dapr.examples.DaprExamplesProtos] At DaprExamplesProtos.java:[line 1151] MS_EXPOSE_REP
Error:  Medium: io.dapr.examples.DaprExamplesProtos$HelloReply.getDefaultInstanceForType() may expose internal representation by returning DaprExamplesProtos$HelloReply.DEFAULT_INSTANCE [io.dapr.examples.DaprExamplesProtos$HelloReply] At DaprExamplesProtos.java:[line 1133] EI_EXPOSE_REP
Error:  Medium: Public static io.dapr.examples.DaprExamplesProtos$HelloReply.getDefaultInstance() may expose internal representation by returning DaprExamplesProtos$HelloReply.DEFAULT_INSTANCE [io.dapr.examples.DaprExamplesProtos$HelloReply] At DaprExamplesProtos.java:[line 1097] MS_EXPOSE_REP
Error:  Medium: io.dapr.examples.DaprExamplesProtos$HelloRequest.getDefaultInstanceForType() may expose internal representation by returning DaprExamplesProtos$HelloRequest.DEFAULT_INSTANCE [io.dapr.examples.DaprExamplesProtos$HelloRequest] At DaprExamplesProtos.java:[line 573] EI_EXPOSE_REP
Error:  Medium: Public static io.dapr.examples.DaprExamplesProtos$HelloRequest.getDefaultInstance() may expose internal representation by returning DaprExamplesProtos$HelloRequest.DEFAULT_INSTANCE [io.dapr.examples.DaprExamplesProtos$HelloRequest] At DaprExamplesProtos.java:[line 537] MS_EXPOSE_REP
[INFO] 

Okay, I'm working on it.

Copy link

codecov bot commented Apr 12, 2025

Codecov Report

Attention: Patch coverage is 30.76923% with 9 lines in your changes missing coverage. Please review.

Project coverage is 75.30%. Comparing base (d759c53) to head (e6b3b43).
Report is 125 commits behind head on master.

Files with missing lines Patch % Lines
...mples/producer/invoke/grpc/HelloWorldProducer.java 14.28% 6 Missing ⚠️
...les/consumer/invoke/grpc/HelloWorldController.java 25.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1309      +/-   ##
============================================
- Coverage     76.91%   75.30%   -1.62%     
- Complexity     1592     1620      +28     
============================================
  Files           145      196      +51     
  Lines          4843     5114     +271     
  Branches        562      555       -7     
============================================
+ Hits           3725     3851     +126     
- Misses          821      947     +126     
- Partials        297      316      +19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.


@Override
public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException {
processDaprGprcClients(bean);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the checkstyle working?

@artur-ciocanu
Copy link
Contributor

artur-ciocanu commented Apr 13, 2025

@seal90 to be honest I am not sure why we need this complexity. A GRPC stub is domain and application specific. If a user wants to create Spring bean for a GRPC stub, this is totally doable although with a little bit of boilerplate.

And to echo @salaboy's concerns we don't need yet another Maven module for GRPC invocation functionality, the infrastructure we already have is enough for that.

@salaboy
Copy link
Contributor

salaboy commented Apr 13, 2025

@artur-ciocanu i thought the same at the beginning but here , the grpc client is using the dapr channel to talk to the grpc stub.. at least that was my understanding. I might need to review this again.

If this is actually doing that, having a new module makes sense to isolate the grpc dependencies.. but let's double check before suggesting more changes in this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add suport GRPC client autowired by DaprGrpcClient like GrpcClient
4 participants