forked from envoyproxy/java-control-plane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestMain.java
100 lines (85 loc) · 3.71 KB
/
TestMain.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package io.envoyproxy.controlplane.server;
import com.google.common.collect.ImmutableList;
import com.google.protobuf.Duration;
import io.envoyproxy.controlplane.cache.NodeGroup;
import io.envoyproxy.controlplane.cache.v2.SimpleCache;
import io.envoyproxy.controlplane.cache.v2.Snapshot;
import io.envoyproxy.envoy.api.v2.Cluster;
import io.envoyproxy.envoy.api.v2.Cluster.DiscoveryType;
import io.envoyproxy.envoy.api.v2.core.Address;
import io.envoyproxy.envoy.api.v2.core.Node;
import io.envoyproxy.envoy.api.v2.core.SocketAddress;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.netty.NettyServerBuilder;
import java.io.IOException;
public class TestMain {
private static final String GROUP = "key";
/**
* Example minimal xDS implementation using the java-control-plane lib. This example configures
* a DiscoveryServer with a v2 cache, but handles v2 or v3 requests from data planes.
*
* @param arg command-line args
*/
public static void main(String[] arg) throws IOException, InterruptedException {
SimpleCache<String> cache = new SimpleCache<>(new NodeGroup<String>() {
@Override public String hash(Node node) {
return GROUP;
}
@Override public String hash(io.envoyproxy.envoy.config.core.v3.Node node) {
return GROUP;
}
});
cache.setSnapshot(
GROUP,
Snapshot.create(
ImmutableList.of(
Cluster.newBuilder()
.setName("cluster0")
.setConnectTimeout(Duration.newBuilder().setSeconds(5))
.setType(DiscoveryType.STATIC)
.addHosts(Address.newBuilder()
.setSocketAddress(SocketAddress.newBuilder().setAddress("127.0.0.1").setPortValue(1234)))
.build()),
ImmutableList.of(),
ImmutableList.of(),
ImmutableList.of(),
ImmutableList.of(),
"1"));
V2DiscoveryServer discoveryServer = new V2DiscoveryServer(cache);
V3DiscoveryServer v3DiscoveryServer = new V3DiscoveryServer(cache);
ServerBuilder builder = NettyServerBuilder.forPort(12345)
.addService(discoveryServer.getAggregatedDiscoveryServiceImpl())
.addService(discoveryServer.getClusterDiscoveryServiceImpl())
.addService(discoveryServer.getEndpointDiscoveryServiceImpl())
.addService(discoveryServer.getListenerDiscoveryServiceImpl())
.addService(discoveryServer.getRouteDiscoveryServiceImpl())
.addService(v3DiscoveryServer.getAggregatedDiscoveryServiceImpl())
.addService(v3DiscoveryServer.getClusterDiscoveryServiceImpl())
.addService(v3DiscoveryServer.getEndpointDiscoveryServiceImpl())
.addService(v3DiscoveryServer.getListenerDiscoveryServiceImpl())
.addService(v3DiscoveryServer.getRouteDiscoveryServiceImpl());
Server server = builder.build();
server.start();
System.out.println("Server has started on port " + server.getPort());
Runtime.getRuntime().addShutdownHook(new Thread(server::shutdown));
Thread.sleep(10000);
cache.setSnapshot(
GROUP,
Snapshot.create(
ImmutableList.of(
Cluster.newBuilder()
.setName("cluster1")
.setConnectTimeout(Duration.newBuilder().setSeconds(5))
.setType(DiscoveryType.STATIC)
.addHosts(Address.newBuilder()
.setSocketAddress(SocketAddress.newBuilder().setAddress("127.0.0.1").setPortValue(1235)))
.build()),
ImmutableList.of(),
ImmutableList.of(),
ImmutableList.of(),
ImmutableList.of(),
"1"));
server.awaitTermination();
}
}