grpc-ecosystem / grpc-spring

Spring Boot starter module for gRPC framework.
https://grpc-ecosystem.github.io/grpc-spring/
Apache License 2.0
3.53k stars 826 forks source link

How to autowire a GRPC stub #897

Closed muraliweb9 closed 1 year ago

muraliweb9 commented 1 year ago

Unable to initialise context

Initialise the context

Unable to initialise the Context

What's the problem? What's not working? What's missing and why do you need it?

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 2 of constructor in com.interview.carworkflowcloud.process.FinaliseBooking required a bean of type 'com.interview.proto.carrecords.service.RecordsServiceGrpc$RecordsServiceBlockingStub' that could not be found.

Action:

Consider defining a bean of type 'com.interview.proto.carrecords.service.RecordsServiceGrpc$RecordsServiceBlockingStub' in your configuration.

Process finished with exit code 1

The application's environment

Which versions do you use?

Bean configuration:

@Configuration
@GrpcClientBeans({
        @GrpcClientBean(
                clazz = RecordsServiceGrpc.RecordsServiceBlockingStub.class,
                beanName = "blockingStub",
                client = @GrpcClient("car-records-app"))
})
class GrpcStubConfig {
    @Bean
    public FinaliseBooking csBlockingStub(
            @Autowired ZeebeClientWrapper zeebeClient,
            @Autowired DiscoveryClient discoveryClient,
            @Autowired RecordsServiceGrpc.RecordsServiceBlockingStub stub) {
        return new FinaliseBooking(zeebeClient, discoveryClient, stub);
    }
}

spring factories workaround is added.

@Configuration
@ImportAutoConfiguration({
    net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration.class,
    net.devh.boot.grpc.client.autoconfigure.GrpcClientMetricAutoConfiguration.class,
    net.devh.boot.grpc.client.autoconfigure.GrpcClientHealthAutoConfiguration.class,
    net.devh.boot.grpc.client.autoconfigure.GrpcClientSecurityAutoConfiguration.class,
    net.devh.boot.grpc.client.autoconfigure.GrpcClientTraceAutoConfiguration.class,
    net.devh.boot.grpc.client.autoconfigure.GrpcDiscoveryClientAutoConfiguration.class,
    net.devh.boot.grpc.common.autoconfigure.GrpcCommonCodecAutoConfiguration.class,
    net.devh.boot.grpc.common.autoconfigure.GrpcCommonTraceAutoConfiguration.class
})
class GrpcConfig {}

application.yml

grpc:
  client:
    car-records-app:
      address: discovery:///car-records-app
      enableKeepAlive: true
      keepAliveWithoutCalls: true
      negotiationType: plaintext

spring:
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}:${random.value}}
      config:
        enabled: false

I have Consul running on localhost:8500 with a service named car-records-app registered to it.

From documentation https://yidongnan.github.io/grpc-spring-boot-starter/en/client/configuration.html I am thinking maybe I am missing a trick with the stub factory.

ST-DDT commented 1 year ago

This looks like this known bug to me:

https://github.com/yidongnan/grpc-spring-boot-starter/issues/633#issuecomment-1564928004

Workaround: Create the GrpcClientBeans in a different @Configuration class and let this config bean @Depend on that new config bean.

Please let me know if this solves the issue for you.

muraliweb9 commented 1 year ago

Thanks ST-DDT worked. Implemented as you described.

The GrpcClientBeans @Configuration

@Configuration(value="grpcStubConfig")
@GrpcClientBeans({
    @GrpcClientBean(
            clazz = RecordsServiceGrpc.RecordsServiceBlockingStub.class,
            beanName = "recordsServiceBlockingStub",
            client = @GrpcClient("car-records-app"))
})
class GrpcStubConfig {
}

The @DependsOn configuration @Configuration

@Configuration
@DependsOn("grpcStubConfig")
class GrpcStubConfig2 {
    @Bean
    public FinaliseBooking finaliseBooking(
            @Autowired ZeebeClientWrapper zeebeClient,
            @Autowired DiscoveryClient discoveryClient,
            @Autowired RecordsServiceGrpc.RecordsServiceBlockingStub stub) {
        return new FinaliseBooking(zeebeClient, discoveryClient, stub);
    }
}
ST-DDT commented 1 year ago

Thanks for letting me know. I will close this issue and use the other issue for tracking/updates.