Rau*_*l G 5 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,因此某些部分可能无法在您的设置中运行。
您可以以与您在问题中提到的示例类似的方式启动它。
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据 M. Deinum 的评论建议,我将使用 Zuul Spring Boot 组件,而不是尝试使用 Undertow 来完成此操作,因为它更适合此任务。
这是执行此操作的教程的链接:
https://spring.io/guides/gs/routing-and-filtering/
希望这对其他人有帮助,因为这是一个很常见的情况,而且我不知道 Spring Boot 上的 Zuul。