grpc-ecosystem / grpc-spring

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

A question about Grpc load balancing and long connections #1032

Open JanYork opened 6 months ago

JanYork commented 6 months ago

When I used Nacos+Grpc to perform multiple service load balancing tests, a question arose: the Grpc client initiated a request for the first time, and Grpc service A1 responded to the request. However, the GRPC client initiated a request for the second time, and Grpc service A2 responded. , the interval between the two requests is only 1.5s, but the responding servers are different. However, the bottom layer of Grpc uses netty for long-term connection communication, but two requests from the same client within 1.5 seconds are responded to by different servers. Does this mean that the Grpc client and Grpc server have long connections in a load-balanced environment? advantages will be lost?

ST-DDT commented 6 months ago

I think the default load balancing policy is round robin. You can overwrite that with the following config https://github.com/grpc-ecosystem/grpc-spring/blob/f72eda3b6434f5737786bb66f373e8f58ce7a56e/grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java#L147

Should we use a different default? 🤔

JanYork commented 6 months ago

I think the default load balancing policy is round robin. You can overwrite that with the following config

https://github.com/grpc-ecosystem/grpc-spring/blob/f72eda3b6434f5737786bb66f373e8f58ce7a56e/grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/config/GrpcChannelProperties.java#L147

Should we use a different default? 🤔

Thank you, but if I rewrite the load balancing algorithm, what kind of load balancing algorithm can retain the advantages of Netty's long connection?

ST-DDT commented 6 months ago

Pick first is pretty close, but it will (by default) only ever pick the first server in the list, instead of a random one. There now is an option to shuffle it first, but I'm not sure how to configure that.

Maybe copy the file and just enable it by default.