java - 实现一个行为类似于 nginx 的 Undertow 反向代理

标签 java spring reverse-proxy undertow

出于开发目的,并不是每个人都可以在他们的机器上安装 nginx(就像我们在 Windows 环境中的开发人员一样),但我们希望能够做一个行为类似于 nginx 的反向代理。

这是我们非常具体的案例:

我们希望从 http://0.0.0.0:8080 提供这两项服务

所以我们想这样映射它:

这样它的工作方式就像带有 url 重写反向代理的 nginx。

我查看了 Undertow 源代码和示例,甚至还有这个具体示例:Reverse Proxy Example ,但这是一个负载均衡器示例,我还没有找到涵盖我需要的任何示例。

此外,我知道 Undertow 能够做到这一点,因为我们知道我们可以通过 Undertow 组件配置配置 WildFly 来毫无问题地涵盖这种特定情况,但我们希望自己将其实现为本地开发的轻量级解决方案。

有谁知道这样做的例子吗?或者任何有足够信息来实现这个的文档?因为我还阅读了 Undertow 关于反向代理的文档,它根本没有帮助。

谢谢

最佳答案

这应该可以完成工作。

它是 Java8,因此某些部分可能无法在您的设置上运行。

您可以使用与the example 类似的方式启动它你在问题中提到过。

package com.company

import com.google.common.collect.ImmutableMap;
import io.undertow.client.ClientCallback;
import io.undertow.client.ClientConnection;
import io.undertow.client.UndertowClient;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.ServerConnection;
import io.undertow.server.handlers.proxy.ProxyCallback;
import io.undertow.server.handlers.proxy.ProxyClient;
import io.undertow.server.handlers.proxy.ProxyConnection;
import org.xnio.IoUtils;
import org.xnio.OptionMap;

import java.io.IOException;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Start the ReverseProxy with an ImmutableMap of matching endpoints and a default
 * 
 * Example:
 * mapping: ImmutableMap("api" -> "http://some-domain.com")
 * default: "http://default-domain.com"
 * 
 * Request 1: localhost:8080/foo -> http://default-domain.com/foo
 * Request 2: localhost:8080/api/bar -> http://some-domain.com/bar
 */

public class ReverseProxyClient implements ProxyClient {
    private static final ProxyTarget TARGET = new ProxyTarget() {};

    private final UndertowClient client;
    private final ImmutableMap<String, URI> mapping;
    private final URI defaultTarget;

    public ReverseProxyClient(ImmutableMap<String, URI> mapping, URI defaultTarget) {
        this.client = UndertowClient.getInstance();
        this.mapping = mapping;
        this.defaultTarget = defaultTarget;
    }

    @Override
    public ProxyTarget findTarget(HttpServerExchange exchange) {
        return TARGET;
    }

    @Override
    public void getConnection(ProxyTarget target, HttpServerExchange exchange, ProxyCallback<ProxyConnection> callback, long timeout, TimeUnit timeUnit) {
        URI targetUri = defaultTarget;

        Matcher matcher = Pattern.compile("^/(\\w+)(/.*)").matcher(exchange.getRequestURI());
        if (matcher.find()) {
            String firstUriSegment = matcher.group(1);
            String remaininguri = matcher.group(2);
            if (mapping.containsKey(firstUriSegment)) {
                // If the first uri segment is in the mapping, update the targetUri
                targetUri = mapping.get(firstUriSegment);
                // Strip the request uri from the part that is used to map upon.
                exchange.setRequestURI(remaininguri);
            }
        }

        client.connect(
            new ConnectNotifier(callback, exchange),
            targetUri,
            exchange.getIoThread(),
            exchange.getConnection().getByteBufferPool(),
            OptionMap.EMPTY);
    }

    private final class ConnectNotifier implements ClientCallback<ClientConnection> {
        private final ProxyCallback<ProxyConnection> callback;
        private final HttpServerExchange exchange;

        private ConnectNotifier(ProxyCallback<ProxyConnection> callback, HttpServerExchange exchange) {
            this.callback = callback;
            this.exchange = exchange;
        }

        @Override
        public void completed(final ClientConnection connection) {
            final ServerConnection serverConnection = exchange.getConnection();
            serverConnection.addCloseListener(serverConnection1 -> IoUtils.safeClose(connection));
            callback.completed(exchange, new ProxyConnection(connection, "/"));
        }

        @Override
        public void failed(IOException e) {
            callback.failed(exchange);
        }
    }
}

关于java - 实现一个行为类似于 nginx 的 Undertow 反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37278606/

相关文章:

java - 在 Spring 和 hibernate 中从数据库获取数据时,如何以正确的格式获取日期时间?

angularjs - Jackson + Spring web 在修改消息转换器后无法反序列化未引用的字符串

java - Spring SAML SSO - IDP 元数据不包含 SingleSignOnService

java - 使用 Integer 包装类创建了多少个对象?

java - 手动关闭应用程序并调用方法? [JavaFX]

java - 如何确定 Java Web 服务是 JAX-WS 还是 JAX-RPC

java - RESTful服务设计: Calling get all resource endpoint

Apache 反向代理配置更改

kubernetes - 外部身份验证后删除特使代理中的上游 header

ssl - nginx 反向代理,具有两种方式的 SSL 到 weblogic