Java源码示例:eu.rekawek.toxiproxy.Proxy

示例1
/**
 * Obtain a {@link ContainerProxy} instance for a specific hostname and port, which can be for any host
 * that is routable <b>from this {@link ToxiproxyContainer} instance</b> (e.g. on the same
 * Docker {@link Network} or on routable from the Docker host).
 *
 * <p><em>It is expected that {@link ToxiproxyContainer#getProxy(GenericContainer, int)} will be more
 * useful in most scenarios, but this method is present to allow use of Toxiproxy in front of containers
 * or external servers that are not managed by Testcontainers.</em></p>
 *
 * @param hostname hostname of target server to be proxied
 * @param port port number on the target server that should be proxied
 * @return a {@link ContainerProxy} instance
 */
public ContainerProxy getProxy(String hostname, int port) {
    String upstream = hostname + ":" + port;

    return proxies.computeIfAbsent(upstream, __ -> {
        try {
            final int toxiPort = nextPort.getAndIncrement();
            if (toxiPort > LAST_PROXIED_PORT) {
                throw new IllegalStateException("Maximum number of proxies exceeded");
            }

            final Proxy proxy = client.createProxy(upstream, "0.0.0.0:" + toxiPort, upstream);
            final int mappedPort = getMappedPort(toxiPort);
            return new ContainerProxy(proxy, getHost(), mappedPort, toxiPort);
        } catch (IOException e) {
            throw new RuntimeException("Proxy could not be created", e);
        }
    });
}