-
Notifications
You must be signed in to change notification settings - Fork 16
/
HelloWorldClientWithNameResolver.java
67 lines (55 loc) · 2.38 KB
/
HelloWorldClientWithNameResolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package io.shunters.grpc.component.grpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.util.RoundRobinLoadBalancerFactory;
import io.shunters.grpc.component.proto.helloworld.GreeterGrpc;
import io.shunters.grpc.component.proto.helloworld.HelloReply;
import io.shunters.grpc.component.proto.helloworld.HelloRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* Created by mykidong on 2018-01-10.
*/
public class HelloWorldClientWithNameResolver {
private static Logger log = LoggerFactory.getLogger(HelloWorldClientWithNameResolver.class);
private final ManagedChannel channel;
private final GreeterGrpc.GreeterBlockingStub blockingStub;
/**
* Consul NameResolver Usage.
*
*
* @param serviceName consul service name.
* @param consulHost consul agent host.
* @param consulPort consul agent port.
* @param ignoreConsul if true, consul is not used. instead, the static node list will be used.
* @param hostPorts the static node list, for instance, Arrays.asList("host1:port1", "host2:port2")
*/
public HelloWorldClientWithNameResolver(String serviceName,
String consulHost,
int consulPort,
boolean ignoreConsul,
List<String> hostPorts) {
String consulAddr = "consul://" + consulHost + ":" + consulPort;
int pauseInSeconds = 5;
channel = ManagedChannelBuilder
.forTarget(consulAddr)
.loadBalancerFactory(RoundRobinLoadBalancerFactory.getInstance())
.nameResolverFactory(new ConsulNameResolver.ConsulNameResolverProvider(serviceName, pauseInSeconds, ignoreConsul, hostPorts))
.usePlaintext(true)
.build();
blockingStub = GreeterGrpc.newBlockingStub(channel);
}
public void sayHello() {
try {
HelloRequest request = HelloRequest.newBuilder()
.setName("grpc load balancer")
.build();
HelloReply response = blockingStub.sayHello(request);
String message = response.getMessage();
log.info("message: [{}]", message);
} catch (Exception e) {
e.printStackTrace();
}
}
}