删除冗余代码
This commit is contained in:
parent
4d295f38cf
commit
78f816ce3e
@ -3,8 +3,6 @@ package com.sf.vertx.handle;
|
|||||||
import io.vertx.circuitbreaker.CircuitBreaker;
|
import io.vertx.circuitbreaker.CircuitBreaker;
|
||||||
import io.vertx.codegen.annotations.VertxGen;
|
import io.vertx.codegen.annotations.VertxGen;
|
||||||
import io.vertx.core.Handler;
|
import io.vertx.core.Handler;
|
||||||
import io.vertx.core.Vertx;
|
|
||||||
import io.vertx.ext.web.Router;
|
|
||||||
import io.vertx.ext.web.RoutingContext;
|
import io.vertx.ext.web.RoutingContext;
|
||||||
import io.vertx.ext.web.client.WebClient;
|
import io.vertx.ext.web.client.WebClient;
|
||||||
import io.vertx.httpproxy.HttpProxy;
|
import io.vertx.httpproxy.HttpProxy;
|
||||||
@ -16,8 +14,8 @@ import io.vertx.httpproxy.HttpProxy;
|
|||||||
@VertxGen
|
@VertxGen
|
||||||
public interface ProxyHandler extends Handler<RoutingContext> {
|
public interface ProxyHandler extends Handler<RoutingContext> {
|
||||||
|
|
||||||
static ProxyHandler create(Vertx vertx,WebClient mainWebClient, HttpProxy httpProxy, CircuitBreaker breaker) {
|
static ProxyHandler create(WebClient mainWebClient, HttpProxy httpProxy, CircuitBreaker breaker) {
|
||||||
return new ProxyHandlerImpl(vertx,mainWebClient, httpProxy, breaker);
|
return new ProxyHandlerImpl(mainWebClient, httpProxy, breaker);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ProxyHandler create(HttpProxy httpProxy) {
|
static ProxyHandler create(HttpProxy httpProxy) {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.sf.vertx.handle;
|
package com.sf.vertx.handle;
|
||||||
|
|
||||||
import io.vertx.circuitbreaker.CircuitBreaker;
|
import io.vertx.circuitbreaker.CircuitBreaker;
|
||||||
import io.vertx.core.Vertx;
|
|
||||||
import io.vertx.ext.web.RoutingContext;
|
import io.vertx.ext.web.RoutingContext;
|
||||||
import io.vertx.ext.web.client.WebClient;
|
import io.vertx.ext.web.client.WebClient;
|
||||||
import io.vertx.httpproxy.HttpProxy;
|
import io.vertx.httpproxy.HttpProxy;
|
||||||
@ -12,13 +11,11 @@ import io.vertx.httpproxy.HttpProxy;
|
|||||||
public class ProxyHandlerImpl implements ProxyHandler {
|
public class ProxyHandlerImpl implements ProxyHandler {
|
||||||
|
|
||||||
private final HttpProxy httpProxy;
|
private final HttpProxy httpProxy;
|
||||||
private Vertx vertx;
|
|
||||||
private CircuitBreaker breaker;
|
private CircuitBreaker breaker;
|
||||||
private WebClient mainWebClient;
|
private WebClient mainWebClient;
|
||||||
|
|
||||||
public ProxyHandlerImpl(Vertx vertx,WebClient mainWebClient, HttpProxy httpProxy, CircuitBreaker breaker) {
|
public ProxyHandlerImpl(WebClient mainWebClient, HttpProxy httpProxy, CircuitBreaker breaker) {
|
||||||
this.httpProxy = httpProxy;
|
this.httpProxy = httpProxy;
|
||||||
this.vertx = vertx;
|
|
||||||
this.breaker = breaker;
|
this.breaker = breaker;
|
||||||
this.mainWebClient = mainWebClient;
|
this.mainWebClient = mainWebClient;
|
||||||
}
|
}
|
||||||
@ -34,7 +31,7 @@ public class ProxyHandlerImpl implements ProxyHandler {
|
|||||||
@Override
|
@Override
|
||||||
public void handle(RoutingContext ctx) {
|
public void handle(RoutingContext ctx) {
|
||||||
// TODO 改造了这个地方
|
// TODO 改造了这个地方
|
||||||
httpProxy.handle(mainWebClient, ctx, vertx, breaker);
|
httpProxy.handle(mainWebClient, ctx, breaker);
|
||||||
// 原始代码只有如下一句
|
// 原始代码只有如下一句
|
||||||
// httpProxy.handle(ctx.request());
|
// httpProxy.handle(ctx.request());
|
||||||
}
|
}
|
||||||
|
@ -25,9 +25,14 @@ import io.vertx.circuitbreaker.CircuitBreakerOptions;
|
|||||||
import io.vertx.core.Future;
|
import io.vertx.core.Future;
|
||||||
import io.vertx.core.Vertx;
|
import io.vertx.core.Vertx;
|
||||||
import io.vertx.core.VertxOptions;
|
import io.vertx.core.VertxOptions;
|
||||||
|
import io.vertx.core.buffer.Buffer;
|
||||||
import io.vertx.core.http.HttpClient;
|
import io.vertx.core.http.HttpClient;
|
||||||
|
import io.vertx.core.http.HttpMethod;
|
||||||
import io.vertx.core.http.HttpServer;
|
import io.vertx.core.http.HttpServer;
|
||||||
import io.vertx.core.http.HttpServerOptions;
|
import io.vertx.core.http.HttpServerOptions;
|
||||||
|
import io.vertx.core.http.HttpServerRequest;
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
|
import io.vertx.core.net.SocketAddress;
|
||||||
import io.vertx.ext.web.Router;
|
import io.vertx.ext.web.Router;
|
||||||
import io.vertx.ext.web.client.WebClient;
|
import io.vertx.ext.web.client.WebClient;
|
||||||
import io.vertx.httpproxy.Body;
|
import io.vertx.httpproxy.Body;
|
||||||
@ -82,14 +87,14 @@ public class DynamicBuildServer implements ApplicationRunner {
|
|||||||
|
|
||||||
Vertx VERTX = Vertx.vertx(vertxOptions);
|
Vertx VERTX = Vertx.vertx(vertxOptions);
|
||||||
|
|
||||||
CircuitBreakerOptions options = new CircuitBreakerOptions().setMaxFailures(2).setTimeout(1000)
|
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", VERTX,
|
||||||
.setFallbackOnFailure(true);
|
new CircuitBreakerOptions().setMaxFailures(5).setMaxRetries(5).setTimeout(2000)
|
||||||
|
.setFallbackOnFailure(true)
|
||||||
CircuitBreaker breaker = CircuitBreaker.create("my-circuit-breaker", VERTX, options).openHandler(v -> {
|
).openHandler(v -> {
|
||||||
System.out.println("Circuit opened");
|
log.info("Circuit opened");
|
||||||
}).closeHandler(v -> {
|
}).closeHandler(v -> {
|
||||||
System.out.println("Circuit closed");
|
log.info("Circuit closed");
|
||||||
});
|
}).retryPolicy(retryCount -> retryCount * 100L);
|
||||||
|
|
||||||
// 创建HTTP监听
|
// 创建HTTP监听
|
||||||
// 所有ip都能访问
|
// 所有ip都能访问
|
||||||
@ -143,7 +148,8 @@ public class DynamicBuildServer implements ApplicationRunner {
|
|||||||
String rateLimitModel = vertxConfig.getRateLimitModel() != null
|
String rateLimitModel = vertxConfig.getRateLimitModel() != null
|
||||||
&& StringUtils.equals(vertxConfig.getRateLimitModel(), "redis") ? "redis" : "local";
|
&& StringUtils.equals(vertxConfig.getRateLimitModel(), "redis") ? "redis" : "local";
|
||||||
mainHttpRouter.route().handler(RateLimitHandler.create(rateLimitModel)).handler(BodyHandler.create())
|
mainHttpRouter.route().handler(RateLimitHandler.create(rateLimitModel)).handler(BodyHandler.create())
|
||||||
.handler(ProxyHandler.create(VERTX,mainWebClient, proxy, breaker)).failureHandler(RestfulFailureHandler.create());
|
.handler(ProxyHandler.create(mainWebClient, proxy, breaker))
|
||||||
|
.failureHandler(RestfulFailureHandler.create());
|
||||||
// mainHttpRouter.route().handler(BodyHandler.create()).handler(ProxyHandler.create(mainWebClient,proxy));
|
// mainHttpRouter.route().handler(BodyHandler.create()).handler(ProxyHandler.create(mainWebClient,proxy));
|
||||||
|
|
||||||
// 服务健康检测重试
|
// 服务健康检测重试
|
||||||
@ -166,6 +172,18 @@ public class DynamicBuildServer implements ApplicationRunner {
|
|||||||
// }
|
// }
|
||||||
// });
|
// });
|
||||||
|
|
||||||
|
breaker.executeWithFallback(promise -> {
|
||||||
|
promise.complete("1");
|
||||||
|
}, v -> {
|
||||||
|
// Executed when the circuit is opened
|
||||||
|
log.info("Executed when the circuit is opened:{}", v);
|
||||||
|
return "3";
|
||||||
|
}, ar -> {
|
||||||
|
// Do something with the result
|
||||||
|
log.info("failed:{}, Result:{}", ar.failed(), ar.result());
|
||||||
|
if (StringUtils.equals(ar.result(), "1") == false) {
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadVertxOptions(VertxConfig vertxConfig, VertxOptions vertxOptions) {
|
private void loadVertxOptions(VertxConfig vertxConfig, VertxOptions vertxOptions) {
|
||||||
|
@ -150,7 +150,7 @@ public class AppConfigServiceImpl implements AppConfigService {
|
|||||||
node.setWeight(routeContent.getWeight() != null && routeContent.getWeight() > 0
|
node.setWeight(routeContent.getWeight() != null && routeContent.getWeight() > 0
|
||||||
? routeContent.getWeight()
|
? routeContent.getWeight()
|
||||||
: 0);
|
: 0);
|
||||||
node.setProtocol(sacService.getServerAddress().getProtocol());
|
node.setProtocol(routeContent.getServerAddress().getProtocol());
|
||||||
nodeList.add(node);
|
nodeList.add(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@ import io.vertx.codegen.annotations.GenIgnore;
|
|||||||
import io.vertx.codegen.annotations.VertxGen;
|
import io.vertx.codegen.annotations.VertxGen;
|
||||||
import io.vertx.core.Future;
|
import io.vertx.core.Future;
|
||||||
import io.vertx.core.Handler;
|
import io.vertx.core.Handler;
|
||||||
import io.vertx.core.Vertx;
|
|
||||||
import io.vertx.core.http.HttpClient;
|
import io.vertx.core.http.HttpClient;
|
||||||
import io.vertx.core.http.HttpClientRequest;
|
import io.vertx.core.http.HttpClientRequest;
|
||||||
import io.vertx.core.http.HttpServerRequest;
|
import io.vertx.core.http.HttpServerRequest;
|
||||||
@ -120,5 +119,5 @@ public interface HttpProxy extends Handler<HttpServerRequest> {
|
|||||||
*/
|
*/
|
||||||
void handle(HttpServerRequest request);
|
void handle(HttpServerRequest request);
|
||||||
|
|
||||||
void handle(WebClient mainWebClient, RoutingContext ctx, Vertx vertx, CircuitBreaker breaker);
|
void handle(WebClient mainWebClient, RoutingContext ctx, CircuitBreaker breaker);
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,6 @@ import com.sf.vertx.utils.ProxyTool;
|
|||||||
import io.vertx.circuitbreaker.CircuitBreaker;
|
import io.vertx.circuitbreaker.CircuitBreaker;
|
||||||
import io.vertx.core.Future;
|
import io.vertx.core.Future;
|
||||||
import io.vertx.core.Promise;
|
import io.vertx.core.Promise;
|
||||||
import io.vertx.core.Vertx;
|
|
||||||
import io.vertx.core.buffer.Buffer;
|
import io.vertx.core.buffer.Buffer;
|
||||||
import io.vertx.core.http.HttpClient;
|
import io.vertx.core.http.HttpClient;
|
||||||
import io.vertx.core.http.HttpClientRequest;
|
import io.vertx.core.http.HttpClientRequest;
|
||||||
@ -63,7 +62,6 @@ public class ReverseProxy implements HttpProxy {
|
|||||||
.failedFuture("No origin available");
|
.failedFuture("No origin available");
|
||||||
private final List<ProxyInterceptor> interceptors = new ArrayList<>();
|
private final List<ProxyInterceptor> interceptors = new ArrayList<>();
|
||||||
private RoutingContext ctx;
|
private RoutingContext ctx;
|
||||||
private Vertx vertx;
|
|
||||||
private CircuitBreaker breaker;
|
private CircuitBreaker breaker;
|
||||||
private WebClient mainWebClient;
|
private WebClient mainWebClient;
|
||||||
|
|
||||||
@ -91,10 +89,9 @@ public class ReverseProxy implements HttpProxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(WebClient mainWebClient, RoutingContext ctx, Vertx vertx, CircuitBreaker breaker) {
|
public void handle(WebClient mainWebClient, RoutingContext ctx, CircuitBreaker breaker) {
|
||||||
// TODO 改造了这个地方
|
// TODO 改造了这个地方
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
this.vertx = vertx;
|
|
||||||
this.breaker = breaker;
|
this.breaker = breaker;
|
||||||
this.mainWebClient = mainWebClient;
|
this.mainWebClient = mainWebClient;
|
||||||
handle(ctx.request());
|
handle(ctx.request());
|
||||||
@ -181,11 +178,11 @@ public class ReverseProxy implements HttpProxy {
|
|||||||
*/
|
*/
|
||||||
private void end(ProxyRequest proxyRequest, int sc) {
|
private void end(ProxyRequest proxyRequest, int sc) {
|
||||||
// TODO 处理反向代理返回结果
|
// TODO 处理反向代理返回结果
|
||||||
if(ProxyTool._ERROR.containsKey(sc)) {
|
if (ProxyTool._ERROR.containsKey(sc)) {
|
||||||
Buffer buffer = Buffer.buffer(ProxyTool._ERROR.get(sc));
|
Buffer buffer = Buffer.buffer(ProxyTool._ERROR.get(sc));
|
||||||
proxyRequest.response().release().setStatusCode(sc).putHeader("content-type", "application/json")
|
proxyRequest.response().release().setStatusCode(sc).putHeader("content-type", "application/json")
|
||||||
.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(buffer.length()))
|
.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(buffer.length())).setBody(Body.body(buffer))
|
||||||
.setBody(Body.body(buffer)).send();
|
.send();
|
||||||
} else {
|
} else {
|
||||||
// proxyRequest.response().release().setStatusCode(sc).putHeader(HttpHeaders.CONTENT_LENGTH, "0").setBody(null)
|
// proxyRequest.response().release().setStatusCode(sc).putHeader(HttpHeaders.CONTENT_LENGTH, "0").setBody(null)
|
||||||
// .send();
|
// .send();
|
||||||
@ -261,34 +258,37 @@ public class ReverseProxy implements HttpProxy {
|
|||||||
breaker.executeWithFallback(promise -> {
|
breaker.executeWithFallback(promise -> {
|
||||||
SocketAddress socketAddress = ProxyTool.resolveOriginAddress(proxyRequest.proxiedRequest());
|
SocketAddress socketAddress = ProxyTool.resolveOriginAddress(proxyRequest.proxiedRequest());
|
||||||
mainWebClient.post(socketAddress.port(), socketAddress.host(), proxyRequest.getURI())
|
mainWebClient.post(socketAddress.port(), socketAddress.host(), proxyRequest.getURI())
|
||||||
.putHeaders(proxyRequest.headers())
|
.putHeaders(proxyRequest.headers())
|
||||||
.sendJson(bodyDecrypt(ctx.getBodyAsString(), sacAppHeaderKey), h -> {
|
.sendJson(bodyDecrypt(ctx.getBodyAsString(), sacAppHeaderKey), h -> {
|
||||||
if (h.succeeded()) {
|
if (h.succeeded()) {
|
||||||
// 释放资源
|
log.info("begin date:{}", System.currentTimeMillis());
|
||||||
proxyRequest.release();
|
// 释放资源
|
||||||
JsonObject responseData = h.result().bodyAsJsonObject();
|
proxyRequest.release();
|
||||||
log.info("responseData:{}", responseData);
|
JsonObject responseData = h.result().bodyAsJsonObject();
|
||||||
// 加密
|
log.info("responseData:{}", responseData);
|
||||||
String dataStr = responseData.toString(); //bodyEncrypt(responseData.toString(), sacAppHeaderKey);
|
// 加密
|
||||||
log.info("aesEncrypt dataStr:{}", dataStr);
|
String dataStr = bodyEncrypt(responseData.toString(), sacAppHeaderKey);
|
||||||
Buffer buffer = Buffer.buffer(dataStr);
|
log.info("aesEncrypt dataStr:{}", dataStr);
|
||||||
ProxyResponse proxyResponse = proxyRequest.response().setStatusCode(200)
|
Buffer buffer = Buffer.buffer(dataStr);
|
||||||
.putHeader("content-type", "application/json").setBody(Body.body(buffer));
|
ProxyResponse proxyResponse = proxyRequest.response().setStatusCode(200)
|
||||||
p.complete(proxyResponse);
|
.putHeader("content-type", "application/json")
|
||||||
promise.complete("1");
|
.setBody(Body.body(buffer));
|
||||||
} else {
|
p.complete(proxyResponse);
|
||||||
log.info("error: {}", h.cause());
|
promise.complete("1");
|
||||||
promise.fail("2");
|
log.info("end date:{}", System.currentTimeMillis());
|
||||||
}
|
} else {
|
||||||
});
|
log.info("error: {}", h.cause());
|
||||||
|
promise.fail("2");
|
||||||
|
}
|
||||||
|
});
|
||||||
}, v -> {
|
}, v -> {
|
||||||
// Executed when the circuit is opened
|
// Executed when the circuit is opened
|
||||||
log.info("Executed when the circuit is opened:{}", v);
|
log.info("Executed when the circuit is opened:{}", v);
|
||||||
return "3";
|
return "3";
|
||||||
}, ar -> {
|
}, ar -> {
|
||||||
// Do something with the result
|
// Do something with the result
|
||||||
log.info("failed:{}, Result:{}", ar.failed(),ar.result());
|
log.info("failed:{}, Result:{}", ar.failed(), ar.result());
|
||||||
if(StringUtils.equals(ar.result(), "1") == false) {
|
if (StringUtils.equals(ar.result(), "1") == false) {
|
||||||
end(proxyRequest, 502);
|
end(proxyRequest, 502);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -312,6 +312,7 @@ public class ReverseProxy implements HttpProxy {
|
|||||||
end(proxyRequest, 502);
|
end(proxyRequest, 502);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return f.compose(a -> sendProxyRequest(proxyRequest, a));
|
return f.compose(a -> sendProxyRequest(proxyRequest, a));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user