ITADN

Having trouble getting dynamic service clients from Micronaut

#10Closedkrickert 创建于 2023-10-18
K
krickertcommented
### Problem statement I'm trying to create a pipeline processor. The idea is this: If I were given a gRPC interface - I can create a gRPC server with that interface. Then I'd register this into Consul. After that registration happens, it would be available in the pipeline for document processing. Below is an architecture document that describes this workflow. ![image](https://github.com/krickert/search_indexer/assets/313612/28d9f294-a345-42a1-8863-a7794884fcca) Key point: All gRPC services use the same interface but each gRPC service box above represents a different implementation of the same interface. Take away: 1:1 for gRPC services registered in Consul. I suspect that this is entirely possible to accomplish but I'm not sure how we can expose this functionality in Micronaut due to it's implementation of the automated consul discovery - it _think_ the only way to do this is by allocating the annotation below. What seems to work now: I can exposes this sort of functionality by turning on auto discovery and adding this code to get that client: ``` @Singleton @Bean GreeterGrpc.GreeterStub greeterStub( @GrpcChannel("greeter") ManagedChannel channel) { return GreeterGrpc.newStub( channel ); } ``` I've tested the code above. It works fine. But I am trying to find a way to use the same code as above but instead of mentioning the service through the annotation, I'd rather pass a string into a service that matches the gRPC service registration name in consul. The above code doesn't work for me because anytime a data scientist adds a new service to consul, then a code change has to happen in order to support that gRPC service. What I'm coding now - I'd like to configure the discovery client so I can get the GrpcChannel's Managed Channel just by inputting Consul's service name that's registered. Because the channel only seems to be specified by the `@GrpcChannel` annotation, I can't figure out how to accomplish this without using that annotation. ### References https://github.com/micronaut-projects/micronaut-core/discussions/9961 Posted this to the above link as well, but this post is more detailed. ### Other thoughts From debugging the micronaut code, I can see that at one point the NettyChannelBuilder is called with only the service name for consul in it. The name in the annotation is successfully looked up in consul. `io.micronaut.grpc.channels.GrpcChannelBuilderFactory#managedChannelBuilder` is called after. This inputs a 0-size interceptor list and the service name we gave in the annotation. After this, a new NameResolver is created. This is what seems to configure consul. ![image](https://github.com/krickert/search_indexer/assets/313612/9927ccc9-9e6f-4612-ab00-2c430f9389ec) I'm add more notes as I go along...
关闭于 2023-10-22 3 条评论