Created
March 23, 2018 23:17
-
-
Save ianomad/c8e0ee0aa0bbb13f51ea46498e885e8c to your computer and use it in GitHub Desktop.
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
package test; | |
import com.google.common.collect.ImmutableList; | |
import com.google.protobuf.Duration; | |
import envoy.api.v2.Cds; | |
import envoy.api.v2.Eds; | |
import envoy.api.v2.Lds; | |
import envoy.api.v2.Rds; | |
import envoy.api.v2.core.AddressOuterClass; | |
import envoy.api.v2.endpoint.EndpointOuterClass; | |
import io.envoyproxy.controlplane.cache.SimpleCache; | |
import io.envoyproxy.controlplane.cache.Snapshot; | |
import io.envoyproxy.controlplane.server.DiscoveryServer; | |
import io.grpc.Server; | |
import io.grpc.ServerBuilder; | |
import io.grpc.netty.NettyServerBuilder; | |
import java.io.IOException; | |
import java.util.UUID; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
public class XDSService { | |
private static final String GROUP = "test"; | |
public static void main(String[] arg) throws IOException, InterruptedException { | |
SimpleCache cache = new SimpleCache(null, node -> GROUP); | |
cache.setSnapshot(GROUP, | |
Snapshot.create(ImmutableList.of(), ImmutableList.of(), ImmutableList.of(), | |
ImmutableList.of(), "1")); | |
DiscoveryServer discoveryServer = new DiscoveryServer(cache); | |
ServerBuilder builder = NettyServerBuilder.forPort(12345) | |
.addService(discoveryServer.getAggregatedDiscoveryServiceImpl()) | |
.addService(discoveryServer.getClusterDiscoveryServiceImpl()) | |
.addService(discoveryServer.getEndpointDiscoveryServiceImpl()) | |
.addService(discoveryServer.getListenerDiscoveryServiceImpl()) | |
.addService(discoveryServer.getRouteDiscoveryServiceImpl()); | |
Server server = builder.build(); | |
server.start(); | |
ExecutorService executorService = Executors.newFixedThreadPool(1); | |
executorService.submit(() -> { | |
int count = 1; | |
while (count++ > 0) { | |
Cds.Cluster cluster = | |
createCluster("my_app_cluster", "some_public_ip", 3000); | |
Eds.ClusterLoadAssignment endpoint = | |
createEndpoint("my_app_cluster", 8080); | |
String version = UUID.randomUUID().toString(); | |
ImmutableList<Cds.Cluster> clusters = ImmutableList.of(cluster); | |
ImmutableList<Eds.ClusterLoadAssignment> endpoints = ImmutableList.of(endpoint); | |
ImmutableList<Lds.Listener> listeners = ImmutableList.of(); | |
ImmutableList<Rds.RouteConfiguration> routes = ImmutableList.of(); | |
Snapshot snapshot = | |
Snapshot.create(clusters, endpoints, listeners, routes, version); | |
cache.setSnapshot(GROUP, snapshot); | |
System.out.println("Updated version: " + version); | |
try { | |
Thread.sleep(10000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
System.out.println("Server has started on port " + server.getPort()); | |
Runtime.getRuntime().addShutdownHook(new Thread(server::shutdown)); | |
Thread.sleep(10000); | |
server.awaitTermination(); | |
} | |
public static Eds.ClusterLoadAssignment createEndpoint(String clusterName, int port) { | |
return Eds.ClusterLoadAssignment.newBuilder() | |
.setClusterName(clusterName) | |
.addEndpoints(EndpointOuterClass.LocalityLbEndpoints.newBuilder() | |
.addLbEndpoints(EndpointOuterClass.LbEndpoint.newBuilder() | |
.setEndpoint(EndpointOuterClass.Endpoint.newBuilder() | |
.setAddress(AddressOuterClass.Address.newBuilder() | |
.setSocketAddress(AddressOuterClass.SocketAddress.newBuilder() | |
.setAddress("0.0.0.0") | |
.setPortValue(port) | |
.setProtocol(AddressOuterClass.SocketAddress.Protocol.TCP) | |
) | |
) | |
) | |
) | |
) | |
.build(); | |
} | |
private static Cds.Cluster createCluster(String clusterName, String host, int port) { | |
Cds.Cluster.Builder builder = Cds.Cluster.newBuilder() | |
.setName(clusterName) | |
.setConnectTimeout(Duration.newBuilder().setSeconds(5)) | |
.setType(Cds.Cluster.DiscoveryType.STATIC); | |
builder = builder.addHosts(AddressOuterClass.Address.newBuilder() | |
.setSocketAddress(AddressOuterClass.SocketAddress.newBuilder() | |
.setAddress(host) | |
.setPortValue(port) | |
) | |
); | |
return builder.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
java-control-plane: https://github.com/envoyproxy/java-control-plane