-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix round robin load balancing with service discovery (#882)
GrpcNameResolverProvider maps individual ServiceInstances provided by the discovery client 1-to-1 to instances of EquivalentAddressGroup instead of creating a single EquivalentAddressGroup instance with a list of addresses, as the GRPC load balancing algorithms are designed to balance across these group instances. A test is added to verify that the load balancing works correctly. This resolves #818.
- Loading branch information
1 parent
f42121f
commit 6c93290
Showing
3 changed files
with
113 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...nt-runtime/src/test/groovy/io/micronaut/grpc/discovery/GrpcLoadBalancedServiceSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package io.micronaut.grpc.discovery | ||
|
||
import io.grpc.ManagedChannel | ||
import io.grpc.examples.helloworld.HelloReply | ||
import io.grpc.examples.helloworld.HelloRequest | ||
import io.grpc.examples.helloworld.MultiNodeGreeterGrpc | ||
import io.grpc.stub.StreamObserver | ||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.context.annotation.Factory | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.context.annotation.Value | ||
import io.micronaut.core.io.socket.SocketUtils | ||
import io.micronaut.grpc.annotation.GrpcChannel | ||
import io.micronaut.runtime.server.EmbeddedServer | ||
import jakarta.inject.Singleton | ||
import spock.lang.Specification | ||
|
||
class GrpcLoadBalancedServiceSpec extends Specification { | ||
|
||
void "test GRPC named service discovery with round robin load balancing"() { | ||
|
||
given: "A service is run on multiple servers" | ||
def port1 = SocketUtils.findAvailableTcpPort() | ||
def port2 = SocketUtils.findAvailableTcpPort() | ||
def port3 = SocketUtils.findAvailableTcpPort() | ||
|
||
EmbeddedServer server1 = ApplicationContext.run(EmbeddedServer, [ | ||
'spec.name': 'GrpcLoadBalancedServiceSpec-Server', | ||
'micronaut.application.name': 'greet', | ||
'grpc.server.port' : port1 | ||
]) | ||
|
||
EmbeddedServer server2 = ApplicationContext.run(EmbeddedServer, [ | ||
'spec.name': 'GrpcLoadBalancedServiceSpec-Server', | ||
'micronaut.application.name': 'greet', | ||
'grpc.server.port' : port2 | ||
]) | ||
|
||
EmbeddedServer server3 = ApplicationContext.run(EmbeddedServer, [ | ||
'spec.name': 'GrpcLoadBalancedServiceSpec-Server', | ||
'micronaut.application.name': 'greet', | ||
'grpc.server.port' : port3 | ||
]) | ||
|
||
and: 'then a client is run that declares the service' | ||
ApplicationContext client = ApplicationContext.run([ | ||
'spec.name': 'GrpcLoadBalancedServiceSpec-Client', | ||
(GrpcNameResolverProvider.ENABLED) : true, | ||
'grpc.channels.greet.plaintext' : true, | ||
'grpc.channels.greet.default-load-balancing-policy' : 'round_robin', | ||
'micronaut.http.services.greet.urls[0]': server1.URL.toString(), | ||
'micronaut.http.services.greet.urls[1]': server2.URL.toString(), | ||
'micronaut.http.services.greet.urls[2]': server3.URL.toString() | ||
]) | ||
|
||
when: 'the service is called many times' | ||
MultiNodeGreeterGrpc.MultiNodeGreeterFutureStub stub = client.getBean(MultiNodeGreeterGrpc.MultiNodeGreeterFutureStub) | ||
Set<String> results = new HashSet<>() | ||
for (int i=0; i<12; i++) { | ||
results.add(stub.sayHello(HelloRequest.newBuilder().setName("test").build()).get().message) | ||
} | ||
|
||
then: 'the calls are load balanced across the 3 different servers' | ||
results.size() == 3 | ||
|
||
cleanup: | ||
client.stop() | ||
server1.stop() | ||
server2.stop() | ||
server3.stop() | ||
} | ||
|
||
@Requires(property = "spec.name", value = "GrpcLoadBalancedServiceSpec-Client") | ||
@Factory | ||
static class Clients { | ||
@Singleton | ||
MultiNodeGreeterGrpc.MultiNodeGreeterFutureStub futureStub(@GrpcChannel("greet") ManagedChannel channel) { | ||
MultiNodeGreeterGrpc.newFutureStub( | ||
channel | ||
) | ||
} | ||
} | ||
|
||
@Singleton | ||
@Requires(property = "spec.name", value = "GrpcLoadBalancedServiceSpec-Server") | ||
static class MultiNodeGreeterImpl extends MultiNodeGreeterGrpc.MultiNodeGreeterImplBase { | ||
|
||
@Value('${grpc.server.port}') | ||
Integer port | ||
|
||
@Override | ||
void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) { | ||
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + request.getName() + " from " + port).build() | ||
responseObserver.onNext(reply) | ||
responseObserver.onCompleted() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters