Skip to content
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

Question: How can I use two GRPC service instances? #109

Open
MichaelGiger opened this issue Mar 21, 2024 · 5 comments
Open

Question: How can I use two GRPC service instances? #109

MichaelGiger opened this issue Mar 21, 2024 · 5 comments

Comments

@MichaelGiger
Copy link

MichaelGiger commented Mar 21, 2024

@NgModule({
  providers: [
    { provide: GRPC_TEST_SERVICE_CLIENT_SETTINGS, useValue: { host: 'http://localhost:**8080**' } as GrpcWebClientSettings, **multi: true** },
    { provide: GRPC_TEST_SERVICE_CLIENT_SETTINGS, useValue: { host: 'http://localhost:**9000**' } as GrpcWebClientSettings, **multi: true** },
  ],
})
export class AppModule {}

How do I need to inject the TestServiceClient(s) in my class/service?

  constructor(@Inject(GRPC_TEST_SERVICE_CLIENT_SETTINGS) private testServiceClients: TestServiceClient[]) {
  }

I need the service instances, not the settings!

@smnbbrv
Copy link
Owner

smnbbrv commented Mar 22, 2024

May I ask you why would you do that? I'd be happy to help, but first it is unclear why you want to use the same client for different ports?

@MichaelGiger
Copy link
Author

The primary focus is on horizontal scaling and decoupling of services. Behind the ports, there are four identical Incident Services, which differ in their configurations – specifically regarding their respective data sources. Due to the significantly varying response times of each service, it is crucial that data can be independently collected and processed by each service.

@smnbbrv
Copy link
Owner

smnbbrv commented Mar 22, 2024

I would simply proceed with creating the services manually. The generated constructor signature for the service client is normally as follows

  constructor(
    settings: any,
    private handler: GrpcHandler
  ) {
    // ...
}

So, you can directly create one in your regular service / component. E.g.

class MyService {
  constructor(@Inject(GRPC_CLIENT_FACTORY) clientFactory: GrpcClientFactory<any>) {
    const services = [
       new TestServiceClient(settings1, clientFactory),
       new TestServiceClient(settings2, clientFactory),
    ];
  }
}

@ConnorLenahan
Copy link

Am I missing something? How are you able to get access to the GrpcHandler in a component? Do I need to inject the GRPC_INTERCEPTOR somehow?

@smnbbrv
Copy link
Owner

smnbbrv commented Jun 21, 2024

@ConnorLenahan please see example above. You inject clientFactory which in fact is a GrpcHandler.

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

No branches or pull requests

3 participants