diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/pom.xml b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/pom.xml deleted file mode 100644 index b8cc59451..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/pom.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - 4.0.0 - - camellia-http-accelerate-proxy-bootstrap - 1.2.30-SNAPSHOT - - - com.netease.nim - camellia-http-accelerate-proxy - 1.2.30-SNAPSHOT - - - - - com.netease.nim - camellia-http-accelerate-proxy-core - 1.2.30-SNAPSHOT - - - ch.qos.logback - logback-classic - 1.2.11 - - - io.netty.incubator - netty-incubator-codec-native-quic - ${netty-incubator-codec-native-quic.version} - osx-x86_64 - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.0.2 - - 1.8 - 1.8 - UTF-8 - - - - - diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/java/com/netease/nim/camellia/sidecar/proxy/bootstrap/Application.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/java/com/netease/nim/camellia/sidecar/proxy/bootstrap/Application.java deleted file mode 100644 index 4775e0155..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/java/com/netease/nim/camellia/sidecar/proxy/bootstrap/Application.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.netease.nim.camellia.sidecar.proxy.bootstrap; - -import com.netease.nim.camellia.http.accelerate.proxy.core.Bootstrap; - -public class Application { - - public static void main(String[] args) { - new Bootstrap().start(); - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/resources/logback.xml b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/resources/logback.xml deleted file mode 100644 index 7b31a6d14..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/resources/logback.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - %d %-5p %t %X %c{1}:%L - %m%n - - - - - - - - - - - \ No newline at end of file diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/resources/proxy.properties b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/resources/proxy.properties deleted file mode 100644 index 9c5211c8e..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/resources/proxy.properties +++ /dev/null @@ -1,16 +0,0 @@ - -## 面向本地nginx -http.accelerate.proxy.port=11800 - -## 面向远端的proxy -transport.tcp.server.port=11600 -transport.quic.server.port=11500 - -## 转发配置 -transport.route.config=transport_route.json - -## upstream配置 -upstream.route.config=upstream_route.json - -## 面向本地的console-server -console.port=11700 \ No newline at end of file diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/resources/transport_route.json b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/resources/transport_route.json deleted file mode 100644 index 40cc01cd6..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/resources/transport_route.json +++ /dev/null @@ -1,46 +0,0 @@ -{ - "servers": - [ - { - "server": "default", - "type": "tcp", - "connect": 3, - "addrs": - [ - "127.0.0.1:11600" - ] - }, - { - "server": "quic_server", - "type": "quic", - "connect": 1, - "addrs": - [ - "127.0.0.1:11500" - ] - } - ], - "routes": - [ - { - "type": "match_host", - "host": "api.netease.im", - "server": "quic_server" - }, - { - "type": "match_host", - "host": "lbs.netease.im", - "server": "tcp", - "backupServer": "quic_server" - }, - { - "type": "match_host", - "host": "www.baidu.com", - "server": "default" - }, - { - "type": "match_all", - "server": "default" - } - ] -} \ No newline at end of file diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/resources/upstream_route.json b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/resources/upstream_route.json deleted file mode 100644 index 6abd0d2d4..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-bootstrap/src/main/resources/upstream_route.json +++ /dev/null @@ -1,57 +0,0 @@ -{ - "upstreams": - [ - { - "upstream": "yunxin-api", - "type": "http", - "heartbeatUri": "/", - "heartbeatTimeout": 1000, - "addrs": - [ - "http://59.111.183.225:80" - ] - }, - { - "upstream": "yunxin-lbs", - "type": "http", - "heartbeatUri": "/", - "heartbeatTimeout": 1000, - "addrs": - [ - "http://59.111.211.9:80" - ] - }, - { - "upstream": "baidu", - "type": "http", - "heartbeatUri": "/index.html", - "heartbeatTimeout": 1000, - "addrs": - [ - "https://www.baidu.com" - ] - } - ], - "routes": - [ - { - "type": "match_host", - "host": "api.netease.im", - "upstream": "yunxin-api" - }, - { - "type": "match_host", - "host": "lbs.netease.im", - "upstream": "yunxin-lbs" - }, - { - "type": "match_host", - "host": "www.baidu.com", - "upstream": "baidu" - }, - { - "type": "match_all", - "upstream": "baidu" - } - ] -} \ No newline at end of file diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/pom.xml b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/pom.xml deleted file mode 100644 index f5b22811e..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/pom.xml +++ /dev/null @@ -1,75 +0,0 @@ - - - 4.0.0 - - camellia-http-accelerate-proxy-core - 1.2.30-SNAPSHOT - - - com.netease.nim - camellia-http-accelerate-proxy - 1.2.30-SNAPSHOT - - - - - com.netease.nim - camellia-codec - 1.2.30-SNAPSHOT - - - com.netease.nim - camellia-tools - 1.2.30-SNAPSHOT - - - com.netease.nim - camellia-http-console - 1.2.30-SNAPSHOT - - - io.netty - netty-all - ${netty.version} - - - com.squareup.okhttp3 - okhttp - ${okhttp4.version} - - - io.netty.incubator - netty-incubator-codec-native-quic - ${netty-incubator-codec-native-quic.version} - - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - ${java.version} - ${java.version} - UTF-8 - - - - org.apache.maven.plugins - maven-source-plugin - ${maven-source-plugin.version} - - - attach-sources - verify - - jar-no-fork - - - - - - - diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/Bootstrap.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/Bootstrap.java deleted file mode 100644 index 6b0de031c..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/Bootstrap.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core; - -import com.netease.nim.camellia.http.accelerate.proxy.core.console.ProxyConsoleServer; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.DefaultTransportRouter; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.ITransportRouter; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.DefaultUpstreamRouter; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.IUpstreamRouter; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.ITransportServer; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.quic.TransportQuicServer; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.tcp.TransportTcpServer; -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.proxy.IHttpAccelerateProxy; -import com.netease.nim.camellia.http.accelerate.proxy.core.proxy.HttpAccelerateProxy; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.concurrent.TimeUnit; - -/** - * - * - * 1、user - * ==https== - * 2、nginx - * ==http== - * 3、IHttpAccelerateProxy -- ITransportRouter -- ITransportClient - * ==tcp/quic== - * 4、ITransportServer -- IUpstreamRouter -- IUpstreamClient - * ==http== - * 5、nginx - * ==http== - * 6、后端 - * - *

- * - * 通过http-accelerate-proxy,来连接距离遥远的两个nginx,解决nginx_upstream不支持http1.0/1.1外其他协议导致的问题: - * 1)连接复用问题 - * 2)丢包问题 - * 3)业务复杂逻辑问题 - * - *

- * - * Created by caojiajun on 2023/7/6 - */ -public class Bootstrap { - - private static final Logger logger = LoggerFactory.getLogger(Bootstrap.class); - - /** - * 启动 - */ - public void start() { - try { - //1、配置 - DynamicConf.init(); - - //2、upstream路由 - IUpstreamRouter upstreamRouter = new DefaultUpstreamRouter(); - upstreamRouter.start(); - - //3、转发server - ITransportServer transportTcpServer = new TransportTcpServer(upstreamRouter); - transportTcpServer.start(); - - //3、转发server - ITransportServer transportQuicServer = new TransportQuicServer(upstreamRouter); - transportQuicServer.start(); - - //4、转发路由 - ITransportRouter transportRouter = new DefaultTransportRouter(); - transportRouter.start(); - - //5、proxy - IHttpAccelerateProxy proxy = new HttpAccelerateProxy(transportRouter); - proxy.start(); - - //6、console - ProxyConsoleServer consoleServer = new ProxyConsoleServer(); - consoleServer.start(transportRouter, upstreamRouter, proxy, transportTcpServer, transportQuicServer); - - logger.info("camellia http accelerate proxy start success!"); - } catch (Exception e) { - logger.error("camellia http accelerate proxy start error!", e); - try { - TimeUnit.SECONDS.sleep(1); - } catch (InterruptedException ex) { - throw new RuntimeException(ex); - } - System.exit(-1); - } - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/conf/ConfigurationUtil.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/conf/ConfigurationUtil.java deleted file mode 100644 index c5423de76..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/conf/ConfigurationUtil.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.conf; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.*; -import java.nio.charset.StandardCharsets; -import java.util.Map; -import java.util.Properties; - -/** - * - * Created by hzcaojiajun on 2018/7/18. - */ -public class ConfigurationUtil { - - private static final Logger logger = LoggerFactory.getLogger(ConfigurationUtil.class); - - public static Properties load(String path) { - try { - InputStream inputStream = ClassLoader.getSystemResourceAsStream(path); - if (inputStream == null) { - return null; - } - Properties prop = new Properties(); - prop.load(inputStream); - return prop; - } catch (Exception e) { - logger.error("load error", e); - return null; - } - } - - public static String getJsonString(String path) { - byte[] buffer = null; - FileInputStream fis = null; - ByteArrayOutputStream bos = null; - try { - File file = new File(path); - fis = new FileInputStream(file); - bos = new ByteArrayOutputStream(1024); - byte[] b = new byte[1024]; - int n; - while ((n = fis.read(b)) != -1) { - bos.write(b, 0, n); - } - buffer = bos.toByteArray(); - } catch (Exception e) { - logger.error(e.getMessage(), e); - } finally { - if (bos != null) { - try { - bos.close(); - } catch (IOException e) { - logger.error(e.getMessage(), e); - } - } - if (fis != null) { - try { - fis.close(); - } catch (IOException e) { - logger.error(e.getMessage(), e); - } - } - } - if (buffer == null) return null; - return new String(buffer, StandardCharsets.UTF_8); - } - - public static Properties loadByFilePath(String path) { - FileInputStream fis = null; - try { - File file = new File(path); - if (!file.exists()) { - logger.warn("{} not exists", path); - return null; - } - fis = new FileInputStream(path); - Properties prop = new Properties(); - prop.load(fis); - return prop; - } catch (Exception e) { - logger.error("loadByFilePath error", e); - return null; - } finally { - if (fis != null) { - try { - fis.close(); - } catch (IOException e) { - logger.error("close error", e); - } - } - } - } - - public static Integer getInteger(Map properties, String key, Integer defaultValue) { - try { - Object v = properties.get(key); - if (v == null) return defaultValue; - return Integer.parseInt(String.valueOf(v)); - } catch (Exception e) { - return defaultValue; - } - } - - public static Double getDouble(Map properties, String key, Double defaultValue) { - try { - Object v = properties.get(key); - if (v == null) return defaultValue; - return Double.parseDouble(String.valueOf(v)); - } catch (Exception e) { - return defaultValue; - } - } - - public static Long getLong(Map properties, String key, Long defaultValue) { - try { - Object v = properties.get(key); - if (v == null) return defaultValue; - return Long.parseLong(String.valueOf(v)); - } catch (Exception e) { - return defaultValue; - } - } - - public static Boolean getBoolean(Map properties, String key, Boolean defaultValue) { - try { - Object v = properties.get(key); - if (v == null) return defaultValue; - return Boolean.parseBoolean(String.valueOf(v)); - } catch (Exception e) { - return defaultValue; - } - } - - public static String get(Map properties, String key, String defaultValue) { - try { - Object v = properties.get(key); - if (v == null) return defaultValue; - return String.valueOf(v); - } catch (Exception e) { - return defaultValue; - } - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/conf/DynamicConf.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/conf/DynamicConf.java deleted file mode 100644 index af8fcc7a0..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/conf/DynamicConf.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.conf; - -import com.netease.nim.camellia.tools.executor.CamelliaThreadFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.*; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class DynamicConf { - - private static final Logger logger = LoggerFactory.getLogger(DynamicConf.class); - - private static Map conf = new HashMap<>(); - private static final AtomicBoolean hasInit = new AtomicBoolean(false); - - public static void init() { - if (hasInit.compareAndSet(false, true)) { - reload(); - Executors.newSingleThreadScheduledExecutor(new CamelliaThreadFactory("dynamic-conf")) - .scheduleAtFixedRate(DynamicConf::reload, 60, 60, TimeUnit.SECONDS); - } - } - - public static void reload() { - Properties props = ConfigurationUtil.load("proxy.properties"); - Map map = new HashMap<>(); - if (props != null) { - for (Map.Entry entry : props.entrySet()) { - map.put(entry.getKey().toString(), entry.getValue().toString()); - } - } - if (map.equals(conf)) { - if (logger.isDebugEnabled()) { - logger.debug("DynamicConf skip reload for conf not modify"); - } - } else { - conf = map; - logger.info("DynamicConf reload success."); - } - } - - public static int getInt(String key, int defaultValue) { - return ConfigurationUtil.getInteger(conf, key, defaultValue); - } - - public static long getLong(String key, long defaultValue) { - return ConfigurationUtil.getLong(conf, key, defaultValue); - } - - public static String getString(String key, String defaultValue) { - return ConfigurationUtil.get(conf, key, defaultValue); - } - - public static boolean getBoolean(String key, boolean defaultValue) { - return ConfigurationUtil.getBoolean(conf, key, defaultValue); - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/console/ConsoleService.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/console/ConsoleService.java deleted file mode 100644 index 6771aafbd..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/console/ConsoleService.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.console; - -import com.netease.nim.camellia.http.console.ConsoleApi; -import com.netease.nim.camellia.http.console.ConsoleResult; -import com.netease.nim.camellia.http.console.IConsoleService; - -import java.util.List; -import java.util.Map; - - -/** - * - * Created by caojiajun on 2019/11/28. - */ -public interface ConsoleService extends IConsoleService { - - @ConsoleApi(uri = "/status") - ConsoleResult status(); - - @ConsoleApi(uri = "/online") - ConsoleResult online(); - - @ConsoleApi(uri = "/offline") - ConsoleResult offline(); - - @ConsoleApi(uri = "/check") - ConsoleResult check(); - - @ConsoleApi(uri = "/monitor") - ConsoleResult monitor(); - - @ConsoleApi(uri = "/reload") - ConsoleResult reload(); -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/console/ConsoleServiceAdaptor.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/console/ConsoleServiceAdaptor.java deleted file mode 100644 index 2f1929aa2..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/console/ConsoleServiceAdaptor.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.console; - -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.monitor.ProxyMonitor; -import com.netease.nim.camellia.http.accelerate.proxy.core.proxy.IHttpAccelerateProxy; -import com.netease.nim.camellia.http.accelerate.proxy.core.status.ServerStartupStatus; -import com.netease.nim.camellia.http.accelerate.proxy.core.status.ServerStatus; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.ITransportServer; -import com.netease.nim.camellia.http.console.ConsoleResult; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.ITransportRouter; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.IUpstreamRouter; -import com.netease.nim.camellia.http.console.ConsoleUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; -import java.util.Map; - -public class ConsoleServiceAdaptor implements ConsoleService { - - private static final Logger logger = LoggerFactory.getLogger(ConsoleServiceAdaptor.class); - - private final ITransportRouter transportRouter; - private final IUpstreamRouter upstreamRouter; - private final ITransportServer[] transportServer; - private final IHttpAccelerateProxy proxy; - - public ConsoleServiceAdaptor(ITransportRouter transportRouter, IUpstreamRouter upstreamRouter, IHttpAccelerateProxy proxy, ITransportServer... transportServers) { - this.transportRouter = transportRouter; - this.upstreamRouter = upstreamRouter; - this.transportServer = transportServers; - this.proxy = proxy; - } - - @Override - public ConsoleResult status() { - boolean online = ServerStatus.getStatus() == ServerStatus.Status.ONLINE; - for (ITransportServer server : transportServer) { - if (server.getStatus() == ServerStartupStatus.FAIL) { - online = false; - } - } - if (proxy.getStatus() == ServerStartupStatus.FAIL) { - online = false; - } - if (logger.isDebugEnabled()) { - logger.debug("online = {}", online); - } - if (online) { - return ConsoleResult.success(ServerStatus.Status.ONLINE.name()); - } else { - return ConsoleResult.error(ServerStatus.Status.OFFLINE.name()); - } - } - - @Override - public ConsoleResult online() { - logger.info("online success"); - ServerStatus.setStatus(ServerStatus.Status.ONLINE); - return ConsoleResult.success(); - } - - @Override - public ConsoleResult offline() { - ServerStatus.setStatus(ServerStatus.Status.OFFLINE); - if (ServerStatus.isIdle()) { - logger.info("offline success"); - return ConsoleResult.success("is idle"); - } else { - logger.info("try offline, but not idle"); - return ConsoleResult.error("not idle"); - } - } - - @Override - public ConsoleResult check() { - return ConsoleResult.success(); - } - - @Override - public ConsoleResult monitor() { - return ConsoleResult.success(ProxyMonitor.getMonitorJson().toJSONString()); - } - - @Override - public ConsoleResult reload() { - DynamicConf.reload(); - transportRouter.reload(); - upstreamRouter.reload(); - return ConsoleResult.success(); - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/console/ProxyConsoleServer.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/console/ProxyConsoleServer.java deleted file mode 100644 index 6e5cc72ac..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/console/ProxyConsoleServer.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.console; - -import com.netease.nim.camellia.http.accelerate.proxy.core.proxy.IHttpAccelerateProxy; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.ITransportServer; -import com.netease.nim.camellia.http.console.CamelliaHttpConsoleConfig; -import com.netease.nim.camellia.http.console.CamelliaHttpConsoleServer; -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.ITransportRouter; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.IUpstreamRouter; - -/** - * Created by caojiajun on 2023/7/10 - */ -public class ProxyConsoleServer { - - public void start(ITransportRouter transportRouter, IUpstreamRouter upstreamRouter, IHttpAccelerateProxy proxy, ITransportServer... transportServers) { - CamelliaHttpConsoleConfig config = new CamelliaHttpConsoleConfig(); - config.setHost(DynamicConf.getString("console.host", "0.0.0.0")); - config.setPort(DynamicConf.getInt("console.port", 11700)); - config.setConsoleService(new ConsoleServiceAdaptor(transportRouter, upstreamRouter, proxy, transportServers)); - CamelliaHttpConsoleServer consoleServer = new CamelliaHttpConsoleServer(config); - consoleServer.start(); - } - -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/constants/Constants.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/constants/Constants.java deleted file mode 100644 index a332a563c..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/constants/Constants.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.constants; - -import io.netty.handler.codec.http.DefaultFullHttpResponse; -import io.netty.handler.codec.http.HttpResponseStatus; -import io.netty.handler.codec.http.HttpVersion; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class Constants { - - public static final DefaultFullHttpResponse BAD_GATEWAY = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_GATEWAY); - - public static final DefaultFullHttpResponse INTERNAL_SERVER_ERROR = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR); -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/ErrorReason.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/ErrorReason.java deleted file mode 100644 index 8f7fc4ba2..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/ErrorReason.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.context; - -/** - * Created by caojiajun on 2023/7/10 - */ -public enum ErrorReason { - - TRANSPORT_SERVER_ROUTE_FAIL(1), - UPSTREAM_SERVER_ROUTE_FAIL(2), - TRANSPORT_SERVER_SELECT_FAIL(3), - UPSTREAM_SERVER_SELECT_FAIL(4), - UPSTREAM_CONNECT_FAIL(5), - UPSTREAM_TIMEOUT(6), - UPSTREAM_ERROR(7), - UPSTREAM_NOT_2XX_CODE(8), - TCP_CLIENT_STOP(9), - ; - - private final int value; - - ErrorReason(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static ErrorReason getByValue(int value) { - for (ErrorReason reason : ErrorReason.values()) { - if (reason.value == value) { - return reason; - } - } - return null; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/LogBean.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/LogBean.java deleted file mode 100644 index c5674f254..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/LogBean.java +++ /dev/null @@ -1,144 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.context; - -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config.TransportServerType; - -/** - * Created by caojiajun on 2023/7/10 - */ -public class LogBean { - private String host; - private String path; - private String transportAddr; - private Long transportClientId; - private TransportServerType transportServerType; - private String upstreamAddr; - private Long startTime; - private Long transportServerSendTime; - private Long transportServerReceiveTime; - private Long upstreamSendTime; - private Long upstreamReplyTime; - private Long endTime; - private String traceId; - private ErrorReason errorReason; - private Integer code; - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public String getPath() { - return path; - } - - public void setPath(String path) { - this.path = path; - } - - public String getTransportAddr() { - return transportAddr; - } - - public void setTransportAddr(String transportAddr) { - this.transportAddr = transportAddr; - } - - public Long getTransportClientId() { - return transportClientId; - } - - public void setTransportClientId(Long transportClientId) { - this.transportClientId = transportClientId; - } - - public TransportServerType getTransportServerType() { - return transportServerType; - } - - public void setTransportServerType(TransportServerType transportServerType) { - this.transportServerType = transportServerType; - } - - public String getUpstreamAddr() { - return upstreamAddr; - } - - public void setUpstreamAddr(String upstreamAddr) { - this.upstreamAddr = upstreamAddr; - } - - public Long getStartTime() { - return startTime; - } - - public void setStartTime(Long startTime) { - this.startTime = startTime; - } - - public Long getTransportServerSendTime() { - return transportServerSendTime; - } - - public void setTransportServerSendTime(Long transportServerSendTime) { - this.transportServerSendTime = transportServerSendTime; - } - - public Long getTransportServerReceiveTime() { - return transportServerReceiveTime; - } - - public void setTransportServerReceiveTime(Long transportServerReceiveTime) { - this.transportServerReceiveTime = transportServerReceiveTime; - } - - public Long getUpstreamSendTime() { - return upstreamSendTime; - } - - public void setUpstreamSendTime(Long upstreamSendTime) { - this.upstreamSendTime = upstreamSendTime; - } - - public Long getUpstreamReplyTime() { - return upstreamReplyTime; - } - - public void setUpstreamReplyTime(Long upstreamReplyTime) { - this.upstreamReplyTime = upstreamReplyTime; - } - - public Long getEndTime() { - return endTime; - } - - public void setEndTime(Long endTime) { - this.endTime = endTime; - } - - public String getTraceId() { - return traceId; - } - - public void setTraceId(String traceId) { - this.traceId = traceId; - } - - public ErrorReason getErrorReason() { - return errorReason; - } - - public void setErrorReason(ErrorReason errorReason) { - this.errorReason = errorReason; - } - - public Integer getCode() { - return code; - } - - public void setCode(Integer code) { - this.code = code; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/LoggerUtils.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/LoggerUtils.java deleted file mode 100644 index 6018ebbf2..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/LoggerUtils.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.context; - -import com.alibaba.fastjson.JSONObject; -import com.netease.nim.camellia.http.accelerate.proxy.core.monitor.ProxyMonitor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by caojiajun on 2023/7/10 - */ -public class LoggerUtils { - - private static final Logger logger = LoggerFactory.getLogger("stats"); - - public static void logging(LogBean logBean) { - try { - JSONObject logJson = new JSONObject(true); - logJson.put("host", logBean.getHost()); - logJson.put("path", logBean.getPath()); - logJson.put("code", logBean.getCode()); - logJson.put("errorReason", logBean.getErrorReason()); - if (logBean.getErrorReason() != null) { - ProxyMonitor.updateError(logBean.getHost(), logBean.getErrorReason()); - } - if (logBean.getEndTime() != null && logBean.getStartTime() != null) { - long spendTime = logBean.getEndTime() - logBean.getStartTime(); - logJson.put("spendTime", spendTime); - ProxyMonitor.updateSpendTime(logBean.getHost(), logBean.getCode(), spendTime); - } - if (logBean.getTransportServerSendTime() != null && logBean.getTransportServerReceiveTime() != null) { - long transportSpendTime1 = logBean.getTransportServerReceiveTime() - logBean.getTransportServerSendTime(); - logJson.put("transportSpendTime1", transportSpendTime1); - ProxyMonitor.updateTransportSpendTime1(logBean.getHost(), logBean.getCode(), transportSpendTime1); - } - if (logBean.getUpstreamSendTime() != null && logBean.getUpstreamReplyTime() != null) { - long upstreamSpendTime = logBean.getUpstreamReplyTime() - logBean.getUpstreamSendTime(); - logJson.put("upstreamSpendTime", upstreamSpendTime); - ProxyMonitor.updateUpstreamSpendTime(logBean.getHost(), logBean.getCode(), upstreamSpendTime); - } - if (logBean.getEndTime() != null && logBean.getUpstreamReplyTime() != null) { - long transportSpendTime2 = logBean.getEndTime() - logBean.getUpstreamReplyTime(); - logJson.put("transportSpendTime2", transportSpendTime2); - ProxyMonitor.updateTransportSpendTime2(logBean.getHost(), logBean.getCode(), transportSpendTime2); - } - logJson.put("transportAddr", logBean.getTransportAddr()); - logJson.put("transportClientId", logBean.getTransportClientId()); - logJson.put("transportServerType", logBean.getTransportServerType()); - logJson.put("upstreamAddr", logBean.getUpstreamAddr()); - logJson.put("startTime", logBean.getStartTime()); - logJson.put("transportServerSendTime", logBean.getTransportServerSendTime()); - logJson.put("transportServerReceiveTime", logBean.getTransportServerReceiveTime()); - logJson.put("upstreamSendTime", logBean.getUpstreamSendTime()); - logJson.put("upstreamReplyTime", logBean.getUpstreamReplyTime()); - logJson.put("endTime", logBean.getEndTime()); - logJson.put("traceId", logBean.getTraceId()); - logger.info(logJson.toJSONString()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/ProxyRequest.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/ProxyRequest.java deleted file mode 100644 index f6d25be89..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/ProxyRequest.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.context; - -import io.netty.handler.codec.http.FullHttpRequest; - -/** - * Created by caojiajun on 2023/7/10 - */ -public class ProxyRequest { - private FullHttpRequest request; - private LogBean logBean; - - public ProxyRequest(FullHttpRequest request, LogBean logBean) { - this.request = request; - this.logBean = logBean; - } - - public FullHttpRequest getRequest() { - return request; - } - - public void setRequest(FullHttpRequest request) { - this.request = request; - } - - public LogBean getLogBean() { - return logBean; - } - - public void setLogBean(LogBean logBean) { - this.logBean = logBean; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/ProxyResponse.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/ProxyResponse.java deleted file mode 100644 index 7933a6fea..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/context/ProxyResponse.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.context; - -import io.netty.handler.codec.http.FullHttpResponse; - -/** - * Created by caojiajun on 2023/7/10 - */ -public class ProxyResponse { - - private FullHttpResponse response; - private LogBean logBean; - - public ProxyResponse(FullHttpResponse response, LogBean logBean) { - this.response = response; - this.logBean = logBean; - } - - public FullHttpResponse getResponse() { - return response; - } - - public void setResponse(FullHttpResponse response) { - this.response = response; - } - - public LogBean getLogBean() { - return logBean; - } - - public void setLogBean(LogBean logBean) { - this.logBean = logBean; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/monitor/ProxyMonitor.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/monitor/ProxyMonitor.java deleted file mode 100644 index cc9feec56..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/monitor/ProxyMonitor.java +++ /dev/null @@ -1,167 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.monitor; - -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ErrorReason; -import com.netease.nim.camellia.tools.executor.CamelliaThreadFactory; -import com.netease.nim.camellia.tools.statistic.CamelliaStatisticsManager; -import com.netease.nim.camellia.tools.statistic.CamelliaStatsData; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Map; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - -/** - * Created by caojiajun on 2023/7/10 - */ -public class ProxyMonitor { - - private static final Logger logger = LoggerFactory.getLogger(ProxyMonitor.class); - - private static final CamelliaStatisticsManager manager1 = new CamelliaStatisticsManager(); - private static final CamelliaStatisticsManager manager2 = new CamelliaStatisticsManager(); - private static final CamelliaStatisticsManager manager3 = new CamelliaStatisticsManager(); - private static final CamelliaStatisticsManager manager4 = new CamelliaStatisticsManager(); - private static final CamelliaStatisticsManager manager5 = new CamelliaStatisticsManager(); - - private static JSONObject monitorJson = new JSONObject(); - - static { - Executors.newSingleThreadScheduledExecutor(new CamelliaThreadFactory("proxy-monitor")) - .scheduleAtFixedRate(ProxyMonitor::calc, 60, 60, TimeUnit.SECONDS); - } - - public static void updateSpendTime(String host, int code, long spendTime) { - manager1.update(host + "|" + code, spendTime); - } - - public static void updateTransportSpendTime1(String host, int code, long transportSpendTime1) { - manager2.update(host + "|" + code, transportSpendTime1); - } - - public static void updateUpstreamSpendTime(String host, int code, long upstreamSpendTime) { - manager3.update(host + "|" + code, upstreamSpendTime); - } - - public static void updateTransportSpendTime2(String host, int code, long transportSpendTime2) { - manager4.update(host + "|" + code, transportSpendTime2); - } - - public static void updateError(String host, ErrorReason reason) { - manager5.update(host + "|" + reason.getValue(), 1); - } - - public static JSONObject getMonitorJson() { - return monitorJson; - } - - private static void calc() { - try { - Map dataMap1 = manager1.getStatsDataAndReset(); - Map dataMap2 = manager2.getStatsDataAndReset(); - Map dataMap3 = manager3.getStatsDataAndReset(); - Map dataMap4 = manager4.getStatsDataAndReset(); - Map dataMap5 = manager5.getStatsDataAndReset(); - - JSONObject monitorJson = new JSONObject(); - - JSONArray spendTime = new JSONArray(); - for (Map.Entry entry : dataMap1.entrySet()) { - JSONObject json = new JSONObject(); - CamelliaStatsData data = entry.getValue(); - String[] split = entry.getKey().split("\\|"); - json.put("host", split[0]); - json.put("code", split[1]); - json.put("count", data.getCount()); - json.put("avg", data.getAvg()); - json.put("max", data.getMax()); - json.put("p50", data.getP50()); - json.put("p75", data.getP75()); - json.put("p90", data.getP90()); - json.put("p95", data.getP95()); - json.put("p99", data.getP99()); - json.put("p999", data.getP999()); - spendTime.add(json); - } - monitorJson.put("spendStats", spendTime); - - JSONArray transportSpendTime1 = new JSONArray(); - for (Map.Entry entry : dataMap2.entrySet()) { - JSONObject json = new JSONObject(); - CamelliaStatsData data = entry.getValue(); - String[] split = entry.getKey().split("\\|"); - json.put("host", split[0]); - json.put("code", split[1]); - json.put("count", data.getCount()); - json.put("avg", data.getAvg()); - json.put("max", data.getMax()); - json.put("p50", data.getP50()); - json.put("p75", data.getP75()); - json.put("p90", data.getP90()); - json.put("p95", data.getP95()); - json.put("p99", data.getP99()); - json.put("p999", data.getP999()); - transportSpendTime1.add(json); - } - monitorJson.put("transportSpendStats", transportSpendTime1); - - - JSONArray upstreamSpendTime = new JSONArray(); - for (Map.Entry entry : dataMap3.entrySet()) { - JSONObject json = new JSONObject(); - CamelliaStatsData data = entry.getValue(); - String[] split = entry.getKey().split("\\|"); - json.put("host", split[0]); - json.put("code", split[1]); - json.put("count", data.getCount()); - json.put("avg", data.getAvg()); - json.put("max", data.getMax()); - json.put("p50", data.getP50()); - json.put("p75", data.getP75()); - json.put("p90", data.getP90()); - json.put("p95", data.getP95()); - json.put("p99", data.getP99()); - json.put("p999", data.getP999()); - upstreamSpendTime.add(json); - } - monitorJson.put("upstreamSpendTime", upstreamSpendTime); - - JSONArray transportSpendTime2 = new JSONArray(); - for (Map.Entry entry : dataMap4.entrySet()) { - JSONObject json = new JSONObject(); - CamelliaStatsData data = entry.getValue(); - String[] split = entry.getKey().split("\\|"); - json.put("host", split[0]); - json.put("code", split[1]); - json.put("count", data.getCount()); - json.put("avg", data.getAvg()); - json.put("max", data.getMax()); - json.put("p50", data.getP50()); - json.put("p75", data.getP75()); - json.put("p90", data.getP90()); - json.put("p95", data.getP95()); - json.put("p99", data.getP99()); - json.put("p999", data.getP999()); - transportSpendTime2.add(json); - } - monitorJson.put("transportSpend2Stats", transportSpendTime2); - - JSONArray errorReason = new JSONArray(); - for (Map.Entry entry : dataMap5.entrySet()) { - JSONObject json = new JSONObject(); - CamelliaStatsData data = entry.getValue(); - String[] split = entry.getKey().split("\\|"); - json.put("host", split[0]); - json.put("reason", ErrorReason.getByValue(Integer.parseInt(split[1]))); - json.put("count", data.getCount()); - errorReason.add(json); - } - monitorJson.put("errorReasonStats", errorReason); - ProxyMonitor.monitorJson = monitorJson; - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/proxy/HttpAccelerateProxy.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/proxy/HttpAccelerateProxy.java deleted file mode 100644 index 2dc5c0381..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/proxy/HttpAccelerateProxy.java +++ /dev/null @@ -1,183 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.proxy; - -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.constants.Constants; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.*; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.ITransportRouter; -import com.netease.nim.camellia.http.accelerate.proxy.core.status.ServerStartupStatus; -import com.netease.nim.camellia.http.accelerate.proxy.core.status.ServerStatus; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.ITransportClient; -import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.*; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.nio.NioServerSocketChannel; -import io.netty.handler.codec.http.*; -import io.netty.util.concurrent.DefaultThreadFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.UUID; -import java.util.concurrent.CompletableFuture; - -import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; -import static io.netty.handler.codec.http.HttpHeaderNames.TRANSFER_ENCODING; -import static io.netty.handler.codec.http.HttpHeaderValues.*; - -/** - * Created by caojiajun on 2023/7/6 - */ -public class HttpAccelerateProxy implements IHttpAccelerateProxy { - - private static final Logger logger = LoggerFactory.getLogger(HttpAccelerateProxy.class); - - private final ITransportRouter router; - private ServerStartupStatus status = ServerStartupStatus.INIT; - - public HttpAccelerateProxy(ITransportRouter router) { - this.router = router; - } - - @Override - public void start() { - String host = DynamicConf.getString("http.accelerate.proxy.host", "0.0.0.0"); - int port = DynamicConf.getInt("http.accelerate.proxy.port", 11800); - if (port <= 0) { - logger.warn("http accelerate proxy skip start"); - status = ServerStartupStatus.SKIP; - return; - } - try { - int bossThread = DynamicConf.getInt("http.accelerate.proxy.boss.thread", 1); - int workThread = DynamicConf.getInt("http.accelerate.proxy.work.thread", Runtime.getRuntime().availableProcessors()); - EventLoopGroup bossGroup = new NioEventLoopGroup(bossThread, new DefaultThreadFactory("http-accelerate-proxy-boss-group")); - EventLoopGroup workerGroup = new NioEventLoopGroup(workThread, new DefaultThreadFactory("http-accelerate-proxy-work-group")); - ServerBootstrap bootstrap = new ServerBootstrap(); - int soBacklog = DynamicConf.getInt("http.accelerate.proxy.so.backlog", 1024); - int soSndBuf = DynamicConf.getInt("http.accelerate.proxy.so.sndbuf", 10 * 1024 * 1024); - int soRcvBuf = DynamicConf.getInt("http.accelerate.proxy.so.rcvbuf", 10 * 1024 * 1024); - boolean tcpNoDelay = DynamicConf.getBoolean("http.accelerate.proxy.tcp.nodelay", true); - boolean soKeepalive = DynamicConf.getBoolean("http.accelerate.proxy.so.keepalive", true); - - int low = DynamicConf.getInt("http.accelerate.proxy.write.buffer.water.mark.low", 128*1024); - int high = DynamicConf.getInt("http.accelerate.proxy.write.buffer.water.mark.high", 512*1024); - - int maxContentLength = DynamicConf.getInt("http.accelerate.proxy.max.content.length", 20*1024*1024); - bootstrap.group(bossGroup, workerGroup) - .channel(NioServerSocketChannel.class) - .option(ChannelOption.SO_BACKLOG, soBacklog) - .childOption(ChannelOption.SO_SNDBUF, soSndBuf) - .childOption(ChannelOption.SO_RCVBUF, soRcvBuf) - .childOption(ChannelOption.TCP_NODELAY, tcpNoDelay) - .childOption(ChannelOption.SO_KEEPALIVE, soKeepalive) - .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(low, high)) - .childHandler(new ChannelInitializer() { - @Override - protected void initChannel(Channel channel) { - ChannelPipeline pipeline = channel.pipeline(); - pipeline.addLast(new HttpServerCodec()); - pipeline.addLast(new HttpObjectAggregator(maxContentLength)); - pipeline.addLast(new SimpleChannelInboundHandler() { - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { - logger.error(cause.getMessage(), cause); - ctx.close(); - } - - @Override - protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest httpRequest) { - CompletableFuture future; - ServerStatus.updateLastUseTime(); - LogBean logBean = new LogBean(); - final ProxyRequest proxyRequest = new ProxyRequest(httpRequest, logBean); - try { - logBean.setHost(httpRequest.headers().get("Host")); - logBean.setTraceId(UUID.randomUUID().toString().replace("-", "")); - QueryStringDecoder queryStringDecoder = new QueryStringDecoder(httpRequest.uri()); - logBean.setPath(queryStringDecoder.path()); - logBean.setStartTime(System.currentTimeMillis()); - ITransportClient client = router.select(proxyRequest); - if (client == null) { - client = router.selectBackup(proxyRequest); - } - if (client == null) { - future = new CompletableFuture<>(); - logBean.setErrorReason(ErrorReason.TRANSPORT_SERVER_ROUTE_FAIL); - future.complete(new ProxyResponse(Constants.BAD_GATEWAY, logBean)); - } else { - future = client.send(proxyRequest); - } - } catch (Exception e) { - future = new CompletableFuture<>(); - future.complete(new ProxyResponse(Constants.INTERNAL_SERVER_ERROR, logBean)); - logBean.setErrorReason(ErrorReason.TRANSPORT_SERVER_ROUTE_FAIL); - logger.error(e.getMessage(), e); - } - future.thenAccept(proxyResponse1 -> { - if (proxyResponse1.getResponse().status() == HttpResponseStatus.BAD_GATEWAY) { - //retry if 502 - ITransportClient client = router.selectBackup(proxyRequest); - if (client != null) { - proxyRequest.getLogBean().setErrorReason(null); - CompletableFuture future1 = client.send(proxyRequest); - future1.thenAccept(proxyResponse2 -> respResponse(ctx, httpRequest, proxyResponse2)); - return; - } - } - respResponse(ctx, httpRequest, proxyResponse1); - }); - } - }); - } - }); - bootstrap.bind(host, port).sync(); - logger.info("http accelerate proxy start success, host = {}, port = {}", host, port); - status = ServerStartupStatus.SUCCESS; - } catch (Exception e) { - status = ServerStartupStatus.FAIL; - logger.error("http accelerate proxy start error, host = {}, port = {}", host, port, e); - throw new IllegalStateException(e); - } - } - - private void respResponse(ChannelHandlerContext ctx, FullHttpRequest httpRequest, ProxyResponse proxyResponse) { - try { - FullHttpResponse response = proxyResponse.getResponse(); - proxyResponse.getLogBean().setCode(response.status().code()); - boolean keepAlive = HttpUtil.isKeepAlive(httpRequest); - boolean close = false; - if (keepAlive) { - if (!httpRequest.protocolVersion().isKeepAliveDefault()) { - response.headers().set(CONNECTION, CLOSE); - close = true; - } else { - response.headers().set(CONNECTION, KEEP_ALIVE); - } - } else { - response.headers().set(CONNECTION, CLOSE); - close = true; - } - response.headers().set(TRANSFER_ENCODING, CHUNKED); - ChannelFuture f = ctx.writeAndFlush(response); - if (close) { - f.addListener(ChannelFutureListener.CLOSE); - } - try { - int refCnt = httpRequest.refCnt(); - if (refCnt > 0) { - httpRequest.release(refCnt); - } - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - } finally { - proxyResponse.getLogBean().setEndTime(System.currentTimeMillis()); - LoggerUtils.logging(proxyResponse.getLogBean()); - } - } - - @Override - public ServerStartupStatus getStatus() { - return status; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/proxy/IHttpAccelerateProxy.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/proxy/IHttpAccelerateProxy.java deleted file mode 100644 index c1b74da94..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/proxy/IHttpAccelerateProxy.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.proxy; - -import com.netease.nim.camellia.http.accelerate.proxy.core.status.ServerStartupStatus; - -/** - * 接收来自nginx的请求 - * Created by caojiajun on 2023/7/6 - */ -public interface IHttpAccelerateProxy { - - void start(); - - ServerStartupStatus getStatus(); -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/DefaultTransportRouter.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/DefaultTransportRouter.java deleted file mode 100644 index 48f16471a..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/DefaultTransportRouter.java +++ /dev/null @@ -1,174 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.transport; - -import com.alibaba.fastjson.JSONObject; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyRequest; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config.*; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.ITransportClient; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.ServerAddr; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.AbstractTransportClients; -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.ConfigurationUtil; -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.quic.TransportQuicClients; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.tcp.TransportTcpClients; -import com.netease.nim.camellia.tools.executor.CamelliaThreadFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.net.URL; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - - -/** - * Created by caojiajun on 2023/7/7 - */ -public class DefaultTransportRouter implements ITransportRouter { - - private static final Logger logger = LoggerFactory.getLogger(DefaultTransportRouter.class); - - private final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new CamelliaThreadFactory("tcp-client-scheduler")); - - private final ConcurrentHashMap addrsMap = new ConcurrentHashMap<>(); - private final ConcurrentHashMap connectMap = new ConcurrentHashMap<>(); - private final ConcurrentHashMap serverMap = new ConcurrentHashMap<>(); - - private TransportRouterConfig routerConfig; - - @Override - public void start() { - reload(); - logger.info("default transport router start success"); - } - - @Override - public synchronized void reload() { - boolean disabled = DynamicConf.getBoolean("transport.route.config.disabled", false); - if (disabled) { - logger.warn("default transport router disabled"); - return; - } - String file = DynamicConf.getString("transport.route.config", "transport_route.json"); - URL resource = Thread.currentThread().getContextClassLoader().getResource(file); - if (resource == null) { - throw new IllegalStateException(file + " not exists"); - } - String path = resource.getPath(); - String jsonString = ConfigurationUtil.getJsonString(path); - TransportRouterConfig config = JSONObject.parseObject(jsonString, TransportRouterConfig.class); - if (config == null) { - logger.error("parse {} failed", file); - return; - } - Set toRemovedServer = new HashSet<>(serverMap.keySet()); - List servers = config.getServers(); - for (TransportServer server : servers) { - String name = server.getServer(); - toRemovedServer.remove(name); - TransportServerType type = server.getType(); - DefaultDynamicTcpAddrs addrs = addrsMap.get(name); - if (addrs == null) { - addrs = new DefaultDynamicTcpAddrs(ServerAddr.toAddrs(server.getAddrs())); - addrsMap.put(name, addrs); - } else { - addrs.updateAddrs(ServerAddr.toAddrs(server.getAddrs())); - } - DefaultConnectGetter connectGetter = connectMap.get(name); - if (connectGetter == null) { - connectGetter = new DefaultConnectGetter(server.getConnect()); - connectMap.put(name, connectGetter); - } else { - connectGetter.updateConnect(server.getConnect()); - } - if (type == TransportServerType.tcp) { - ITransportClient transportClients = serverMap.get(name); - if (transportClients == null) { - transportClients = new TransportTcpClients(addrs, connectGetter); - transportClients.start(); - serverMap.put(name, transportClients); - } - } else if (type == TransportServerType.quic) { - ITransportClient transportClients = serverMap.get(name); - if (transportClients == null) { - transportClients = new TransportQuicClients(addrs, connectGetter); - transportClients.start(); - serverMap.put(name, transportClients); - } - } else { - throw new IllegalArgumentException(type + " not support"); - } - } - if (!toRemovedServer.isEmpty()) { - for (String server : toRemovedServer) { - ITransportClient client = serverMap.get(server); - if (client != null) { - logger.warn("server = {} will close after 120 seconds", server); - scheduledExecutor.schedule(client::stop, 120, TimeUnit.SECONDS); - } - addrsMap.remove(server); - connectMap.remove(server); - } - } - this.routerConfig = config; - } - - @Override - public ITransportClient select(ProxyRequest request) { - return selectClient(request, false); - } - - @Override - public ITransportClient selectBackup(ProxyRequest request) { - return selectClient(request, true); - } - - public ITransportClient selectClient(ProxyRequest request, boolean backUp) { - try { - List routes = routerConfig.getRoutes(); - for (TransportRoute route : routes) { - String server = select0(request, route, backUp); - if (server == null) continue; - ITransportClient client = serverMap.get(server); - if (client != null) { - return client; - } - } - return null; - } catch (Exception e) { - logger.error(e.getMessage(), e); - return null; - } - } - - - private String select0(ProxyRequest request, TransportRoute route, boolean backUp) { - try { - TransportRouteType type = route.getType(); - if (type == TransportRouteType.match_all) { - if (backUp) { - return route.getBackupServer(); - } else { - return route.getServer(); - } - } - String host = request.getLogBean().getHost(); - if (type == TransportRouteType.match_host) { - if (route.getHost().equals(host)) { - if (backUp) { - return route.getBackupServer(); - } else { - return route.getServer(); - } - } - } - return null; - } catch (Exception e) { - logger.error(e.getMessage(), e); - return null; - } - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/ITransportRouter.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/ITransportRouter.java deleted file mode 100644 index 6bad6e769..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/ITransportRouter.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.transport; - -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyRequest; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.ITransportClient; - -/** - * Created by caojiajun on 2023/7/7 - */ -public interface ITransportRouter { - - void start(); - - void reload(); - - ITransportClient select(ProxyRequest request); - - ITransportClient selectBackup(ProxyRequest request); -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/DefaultConnectGetter.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/DefaultConnectGetter.java deleted file mode 100644 index f4ea0e299..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/DefaultConnectGetter.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config; - -import com.netease.nim.camellia.tools.base.DynamicValueGetter; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class DefaultConnectGetter implements DynamicValueGetter { - - private int connect; - - public DefaultConnectGetter(int connect) { - this.connect = connect; - } - - @Override - public Integer get() { - return connect; - } - - public void updateConnect(int connect) { - this.connect = connect; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/DefaultDynamicTcpAddrs.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/DefaultDynamicTcpAddrs.java deleted file mode 100644 index adfee0d04..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/DefaultDynamicTcpAddrs.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config; - -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.DynamicAddrs; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.ServerAddr; - -import java.util.List; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class DefaultDynamicTcpAddrs implements DynamicAddrs { - - private List addrs; - - public DefaultDynamicTcpAddrs(List addrs) { - this.addrs = addrs; - } - - @Override - public List getAddrs() { - return addrs; - } - - public void updateAddrs(List addrs) { - this.addrs = addrs; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportRoute.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportRoute.java deleted file mode 100644 index 5652c8fb0..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportRoute.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config; - -/** - * Created by caojiajun on 2023/7/10 - */ -public class TransportRoute { - - private TransportRouteType type; - private String host; - private String server; - private String backupServer; - - public TransportRouteType getType() { - return type; - } - - public void setType(TransportRouteType type) { - this.type = type; - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public String getServer() { - return server; - } - - public void setServer(String server) { - this.server = server; - } - - public String getBackupServer() { - return backupServer; - } - - public void setBackupServer(String backupServer) { - this.backupServer = backupServer; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportRouteType.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportRouteType.java deleted file mode 100644 index 3fb6e707c..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportRouteType.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config; - -/** - * Created by caojiajun on 2023/7/10 - */ -public enum TransportRouteType { - match_host, - match_all, - ; -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportRouterConfig.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportRouterConfig.java deleted file mode 100644 index 0412f395d..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportRouterConfig.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config; - - -import java.util.List; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class TransportRouterConfig { - private List servers; - private List routes; - - public List getServers() { - return servers; - } - - public void setServers(List servers) { - this.servers = servers; - } - - public List getRoutes() { - return routes; - } - - public void setRoutes(List routes) { - this.routes = routes; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportServer.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportServer.java deleted file mode 100644 index a2c118667..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportServer.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config; - -import java.util.List; - -/** - * Created by caojiajun on 2023/7/10 - */ -public class TransportServer { - private String server; - private TransportServerType type; - private List addrs; - private int connect = 1; - - public String getServer() { - return server; - } - - public void setServer(String server) { - this.server = server; - } - - public TransportServerType getType() { - return type; - } - - public void setType(TransportServerType type) { - this.type = type; - } - - public List getAddrs() { - return addrs; - } - - public void setAddrs(List addrs) { - this.addrs = addrs; - } - - public int getConnect() { - return connect; - } - - public void setConnect(int connect) { - this.connect = connect; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportServerType.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportServerType.java deleted file mode 100644 index 6296c2775..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/transport/config/TransportServerType.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config; - -/** - * Created by caojiajun on 2023/7/10 - */ -public enum TransportServerType { - - tcp(1), - quic(2), - ; - - private final int value; - - TransportServerType(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static TransportServerType getByValue(int value) { - for (TransportServerType type : TransportServerType.values()) { - if (type.value == value) { - return type; - } - } - return null; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/DefaultUpstreamRouter.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/DefaultUpstreamRouter.java deleted file mode 100644 index 43d41e995..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/DefaultUpstreamRouter.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream; - -import com.alibaba.fastjson.JSONObject; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyRequest; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.config.*; -import com.netease.nim.camellia.http.accelerate.proxy.core.upstream.IUpstreamClient; -import com.netease.nim.camellia.http.accelerate.proxy.core.upstream.OkHttpUpstreamClient; -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.ConfigurationUtil; -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.net.URL; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class DefaultUpstreamRouter implements IUpstreamRouter { - - private static final Logger logger = LoggerFactory.getLogger(DefaultUpstreamRouter.class); - private final ConcurrentHashMap addrsMap = new ConcurrentHashMap<>(); - private final ConcurrentHashMap heartbeatTimeoutMap = new ConcurrentHashMap<>(); - private final ConcurrentHashMap heartbeatUriMap = new ConcurrentHashMap<>(); - private final ConcurrentHashMap upstreamMap = new ConcurrentHashMap<>(); - - private UpstreamRouterConfig routerConfig; - - @Override - public void start() { - reload(); - logger.info("default upstream router start success"); - } - - @Override - public synchronized void reload() { - boolean disabled = DynamicConf.getBoolean("upstream.route.config.disabled", false); - if (disabled) { - logger.warn("default upstream router disabled"); - return; - } - String file = DynamicConf.getString("upstream.route.config", "upstream_route.json"); - URL resource = Thread.currentThread().getContextClassLoader().getResource(file); - if (resource == null) { - throw new IllegalStateException(file + " not exists"); - } - String path = resource.getPath(); - String jsonString = ConfigurationUtil.getJsonString(path); - UpstreamRouterConfig config = JSONObject.parseObject(jsonString, UpstreamRouterConfig.class); - if (config == null) { - logger.warn("{} parse failed", file); - return; - } - List upstreams = config.getUpstreams(); - for (Upstream upstream : upstreams) { - String name = upstream.getUpstream(); - UpstreamType type = upstream.getType(); - if (type == UpstreamType.http) { - DefaultDynamicUpstreamAddrs addrs = addrsMap.get(name); - if (addrs == null) { - addrs = new DefaultDynamicUpstreamAddrs(upstream.getAddrs()); - addrsMap.put(name, addrs); - } else { - addrs.updateAddrs(upstream.getAddrs()); - } - DefaultDynamicHeartbeatTimeoutGetter heartbeatTimeout = heartbeatTimeoutMap.get(name); - if (heartbeatTimeout == null) { - heartbeatTimeout = new DefaultDynamicHeartbeatTimeoutGetter(upstream.getHeartbeatTimeout()); - heartbeatTimeoutMap.put(name, heartbeatTimeout); - } else { - heartbeatTimeout.updateTimeout(upstream.getHeartbeatTimeout()); - } - DefaultDynamicUpstreamHealthUriGetter heartbeatUri = heartbeatUriMap.get(name); - if (heartbeatUri == null) { - heartbeatUri = new DefaultDynamicUpstreamHealthUriGetter(upstream.getHeartbeatUri()); - heartbeatUriMap.put(name, heartbeatUri); - } else { - heartbeatUri.updateHealthUri(upstream.getHeartbeatUri()); - } - IUpstreamClient upstreamClient = upstreamMap.get(name); - if (upstreamClient == null) { - upstreamClient = new OkHttpUpstreamClient(addrs, heartbeatUri, heartbeatTimeout); - upstreamMap.put(name, upstreamClient); - } - } else { - throw new IllegalArgumentException(type + " not support"); - } - } - this.routerConfig = config; - } - - @Override - public IUpstreamClient select(ProxyRequest request) { - List routes = routerConfig.getRoutes(); - for (UpstreamRoute route : routes) { - String upstream = select0(request, route); - if (upstream == null) continue; - IUpstreamClient client = upstreamMap.get(upstream); - if (client != null) { - return client; - } - } - return null; - } - - private String select0(ProxyRequest request, UpstreamRoute route) { - try { - UpstreamRouteType type = route.getType(); - if (type == UpstreamRouteType.match_all) { - return route.getUpstream(); - } - String host = request.getRequest().headers().get("Host"); - if (type == UpstreamRouteType.match_host) { - if (route.getHost().equals(host)) { - return route.getUpstream(); - } - } - return null; - } catch (Exception e) { - logger.error(e.getMessage(), e); - return null; - } - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/IUpstreamRouter.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/IUpstreamRouter.java deleted file mode 100644 index 7c820998b..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/IUpstreamRouter.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream; - -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyRequest; -import com.netease.nim.camellia.http.accelerate.proxy.core.upstream.IUpstreamClient; - -/** - * Created by caojiajun on 2023/7/7 - */ -public interface IUpstreamRouter { - - void start(); - - void reload(); - - IUpstreamClient select(ProxyRequest request); -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/DefaultDynamicHeartbeatTimeoutGetter.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/DefaultDynamicHeartbeatTimeoutGetter.java deleted file mode 100644 index f556518e6..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/DefaultDynamicHeartbeatTimeoutGetter.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.config; - -import com.netease.nim.camellia.tools.base.DynamicValueGetter; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class DefaultDynamicHeartbeatTimeoutGetter implements DynamicValueGetter { - - private long timeout; - - public DefaultDynamicHeartbeatTimeoutGetter(long timeout) { - this.timeout = timeout; - } - - @Override - public Long get() { - return timeout; - } - - public void updateTimeout(long timeout) { - this.timeout = timeout; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/DefaultDynamicUpstreamAddrs.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/DefaultDynamicUpstreamAddrs.java deleted file mode 100644 index 9816a0d00..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/DefaultDynamicUpstreamAddrs.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.config; - -import java.util.List; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class DefaultDynamicUpstreamAddrs implements DynamicUpstreamAddrs { - - private List addrs; - - public DefaultDynamicUpstreamAddrs(List addrs) { - this.addrs = addrs; - } - - @Override - public List getAddrs() { - return addrs; - } - - public void updateAddrs(List addrs) { - this.addrs = addrs; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/DefaultDynamicUpstreamHealthUriGetter.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/DefaultDynamicUpstreamHealthUriGetter.java deleted file mode 100644 index dd0abcc6c..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/DefaultDynamicUpstreamHealthUriGetter.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.config; - -import com.netease.nim.camellia.tools.base.DynamicValueGetter; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class DefaultDynamicUpstreamHealthUriGetter implements DynamicValueGetter { - - private String uri; - - public DefaultDynamicUpstreamHealthUriGetter(String uri) { - this.uri = uri; - } - - @Override - public String get() { - return uri; - } - - public void updateHealthUri(String uri) { - this.uri = uri; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/DynamicUpstreamAddrs.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/DynamicUpstreamAddrs.java deleted file mode 100644 index 7804e5d8c..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/DynamicUpstreamAddrs.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.config; - - -import java.util.List; - -/** - * Created by caojiajun on 2023/7/7 - */ -public interface DynamicUpstreamAddrs { - - List getAddrs(); - -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/Upstream.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/Upstream.java deleted file mode 100644 index 8a1fd95d2..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/Upstream.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.config; - -import java.util.List; - -/** - * Created by caojiajun on 2023/7/10 - */ -public class Upstream { - private String upstream; - private UpstreamType type; - private List addrs; - private String heartbeatUri; - private int heartbeatTimeout = 500; - - public String getUpstream() { - return upstream; - } - - public void setUpstream(String upstream) { - this.upstream = upstream; - } - - public UpstreamType getType() { - return type; - } - - public void setType(UpstreamType type) { - this.type = type; - } - - public List getAddrs() { - return addrs; - } - - public void setAddrs(List addrs) { - this.addrs = addrs; - } - - public String getHeartbeatUri() { - return heartbeatUri; - } - - public void setHeartbeatUri(String heartbeatUri) { - this.heartbeatUri = heartbeatUri; - } - - public int getHeartbeatTimeout() { - return heartbeatTimeout; - } - - public void setHeartbeatTimeout(int heartbeatTimeout) { - this.heartbeatTimeout = heartbeatTimeout; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/UpstreamRoute.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/UpstreamRoute.java deleted file mode 100644 index a7045a183..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/UpstreamRoute.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.config; - -/** - * Created by caojiajun on 2023/7/10 - */ -public class UpstreamRoute { - private UpstreamRouteType type; - private String host; - private String upstream; - public UpstreamRouteType getType() { - return type; - } - - public void setType(UpstreamRouteType type) { - this.type = type; - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public String getUpstream() { - return upstream; - } - - public void setUpstream(String upstream) { - this.upstream = upstream; - } - -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/UpstreamRouteType.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/UpstreamRouteType.java deleted file mode 100644 index 53dddc144..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/UpstreamRouteType.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.config; - -/** - * Created by caojiajun on 2023/7/10 - */ -public enum UpstreamRouteType { - match_host, - match_all, - ; -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/UpstreamRouterConfig.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/UpstreamRouterConfig.java deleted file mode 100644 index 38cd45d75..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/UpstreamRouterConfig.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.config; - -import java.util.List; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class UpstreamRouterConfig { - - private List upstreams; - private List routes; - - public List getUpstreams() { - return upstreams; - } - - public void setUpstreams(List upstreams) { - this.upstreams = upstreams; - } - - public List getRoutes() { - return routes; - } - - public void setRoutes(List routes) { - this.routes = routes; - } - -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/UpstreamType.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/UpstreamType.java deleted file mode 100644 index abf692f2c..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/route/upstream/config/UpstreamType.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.config; - -/** - * Created by caojiajun on 2023/7/10 - */ -public enum UpstreamType { - http, - ; -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/status/ServerStartupStatus.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/status/ServerStartupStatus.java deleted file mode 100644 index 84d8a4f09..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/status/ServerStartupStatus.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.status; - -/** - * Created by caojiajun on 2023/7/21 - */ -public enum ServerStartupStatus { - - INIT, - SUCCESS, - FAIL, - SKIP, - ; -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/status/ServerStatus.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/status/ServerStatus.java deleted file mode 100644 index 834258a1e..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/status/ServerStatus.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.status; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.HashSet; -import java.util.Set; - -/** - * - * Created by caojiajun on 2019/11/28. - */ -public class ServerStatus { - - private static final Logger logger = LoggerFactory.getLogger(ServerStatus.class); - - private static Status status = Status.ONLINE; - private static long lastUseTime = System.currentTimeMillis(); - private static final Set onlineCallbackSet = new HashSet<>(); - private static final Set offlineCallbackSet = new HashSet<>(); - - public enum Status { - ONLINE, - OFFLINE, - ; - } - - public static Status getStatus() { - return status; - } - - public static void setStatus(Status status) { - ServerStatus.status = status; - if (status == Status.ONLINE) { - invokeOnlineCallback(); - } else if (status == Status.OFFLINE) { - invokeOfflineCallback(); - } - } - - public static void updateLastUseTime() { - lastUseTime = TimeCache.currentMillis; - } - - public static boolean isIdle() { - return TimeCache.currentMillis - lastUseTime > 10*1000L; - } - - public static void invokeOnlineCallback() { - for (Runnable runnable : onlineCallbackSet) { - try { - runnable.run(); - } catch (Exception e) { - logger.error("online callback error", e); - } - } - } - - public static void invokeOfflineCallback() { - for (Runnable runnable : offlineCallbackSet) { - try { - runnable.run(); - } catch (Exception e) { - logger.error("offline callback error", e); - } - } - } - - public static synchronized void registerOnlineCallback(Runnable task) { - onlineCallbackSet.add(task); - } - - public static synchronized void registerOfflineCallback(Runnable task) { - offlineCallbackSet.add(task); - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/status/TimeCache.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/status/TimeCache.java deleted file mode 100644 index 656d1b56e..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/status/TimeCache.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.status; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.concurrent.TimeUnit; - -/** - * 高并发频繁调用{@link System#currentTimeMillis()}会有性能损耗,所以对于时间要求不太严格的场景可以使用 - * Created by caojiajun on 2020/11/5 - */ -public class TimeCache implements Runnable { - - private static final Logger logger = LoggerFactory.getLogger(TimeCache.class); - - /** - * 时间,单位是milliseconds,volatile保证可见性 - */ - public static volatile long currentMillis = System.currentTimeMillis(); - - static { - new Thread(new TimeCache()).start(); - } - - @Override - public void run() { - while (true) { - currentMillis = System.currentTimeMillis(); - try { - TimeUnit.MILLISECONDS.sleep(5); - } catch (InterruptedException e) { - logger.error(e.getMessage(), e); - } - } - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/AbstractClient.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/AbstractClient.java deleted file mode 100644 index 2cd1dd492..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/AbstractClient.java +++ /dev/null @@ -1,223 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport; - -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ErrorReason; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyRequest; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyResponse; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec.*; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.ServerAddr; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.Status; -import com.netease.nim.camellia.tools.executor.CamelliaThreadFactory; -import io.netty.handler.codec.http.DefaultFullHttpResponse; -import io.netty.handler.codec.http.FullHttpResponse; -import io.netty.handler.codec.http.HttpResponseStatus; -import io.netty.handler.codec.http.HttpVersion; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.*; -import java.util.concurrent.atomic.AtomicLong; - -/** - * Created by caojiajun on 2023/7/25 - */ -public abstract class AbstractClient implements Client { - - private static final Logger logger = LoggerFactory.getLogger(AbstractClient.class); - - private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() * 4, - new CamelliaThreadFactory("transport-client-heartbeat")); - private static final AtomicLong idGen = new AtomicLong(0); - - private final AtomicLong seqIdGen = new AtomicLong(); - - private final ConcurrentHashMap requestMap = new ConcurrentHashMap<>(); - private final ConcurrentHashMap> heartbeatMap = new ConcurrentHashMap<>(); - - private final long id; - private final ServerAddr addr; - private volatile Status status; - private ScheduledFuture scheduledFuture; - - public AbstractClient(ServerAddr addr) { - this.addr = addr; - this.id = idGen.incrementAndGet(); - } - - public int heartbeatIntervalSeconds() { - return DynamicConf.getInt("transport.client.heartbeat.interval.seconds", 10); - } - - public int heartbeatTimeoutSeconds() { - return DynamicConf.getInt("transport.client.heartbeat.timeout.seconds", 10); - } - - @Override - public void start() { - try { - start0(); - status = Status.ONLINE; - long startTime = System.currentTimeMillis(); - heartbeat(); - logger.info("transport client start success, type = {}, addr = {}, id = {}, heartbeatSpendMs = {}", - getType(), getAddr(), getId(), (System.currentTimeMillis() - startTime)); - int intervalSeconds = heartbeatIntervalSeconds(); - scheduledFuture = scheduler.scheduleAtFixedRate(this::heartbeat, intervalSeconds, intervalSeconds, TimeUnit.SECONDS); - } catch (Exception e) { - stop(); - logger.error("transport client start error, type = {}, addr = {}, id = {}", getType(), addr, id, e); - } - } - - public abstract void start0() throws Exception; - - @Override - public void send(ProxyRequest request, CompletableFuture future) { - ProxyPackHeader header = newHeader(ProxyPackCmd.REQUEST); - request.getLogBean().setTransportServerSendTime(System.currentTimeMillis()); - ProxyPack pack = ProxyPack.newPack(header, new RequestPack(request)); - requestMap.put(header.getSeqId(), new Request(future, request)); - send0(pack); - } - - public abstract void send0(ProxyPack proxyPack); - - public synchronized void stop() { - status = Status.INVALID; - try { - stop0(); - } catch (Exception e) { - logger.error("channel close error, type = {}, addr = {}, id = {}", getType(), getAddr(), getId(), e); - } - try { - if (scheduledFuture != null) { - scheduledFuture.cancel(false); - } - } catch (Exception e) { - logger.error("scheduledFuture cancel error, type = {}, addr = {}, id = {}", getType(), getAddr(), getId(), e); - } - Set set1 = new HashSet<>(requestMap.keySet()); - for (Long seqId : set1) { - Request request = requestMap.remove(seqId); - if (request != null) { - FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.GATEWAY_TIMEOUT); - request.request.getLogBean().setErrorReason(ErrorReason.TCP_CLIENT_STOP); - request.future.complete(new ProxyResponse(response, request.request.getLogBean())); - } - } - Set set2 = new HashSet<>(heartbeatMap.keySet()); - for (Long seqId : set2) { - CompletableFuture future = heartbeatMap.remove(seqId); - if (future != null) { - future.complete(false); - } - } - logger.info("TransportClient stopped, type = {}, addr = {}, id = {}", getType(), getAddr(), getId()); - } - - public abstract void stop0(); - - private static class Request { - CompletableFuture future; - ProxyRequest request; - - public Request(CompletableFuture future, ProxyRequest request) { - this.future = future; - this.request = request; - } - } - - protected final void onProxyPack(ProxyPack pack) { - ProxyPackHeader header = pack.getHeader(); - long seqId = header.getSeqId(); - if (header.getCmd() == ProxyPackCmd.HEARTBEAT) { - if (header.isAck()) { - HeartbeatAckPack ackPack = (HeartbeatAckPack) pack.getBody(); - CompletableFuture future = heartbeatMap.remove(seqId); - if (future != null) { - future.complete(ackPack.isOnline()); - } else { - logger.warn("unknown heartbeat seqId = {}", seqId); - } - } else { - logger.warn("illegal heartbeat pack"); - } - } else if (header.getCmd() == ProxyPackCmd.REQUEST) { - if (header.isAck()) { - RequestAckPack ackPack = (RequestAckPack) pack.getBody(); - Request request = requestMap.remove(seqId); - if (request != null) { - request.future.complete(ackPack.getProxyResponse()); - } else { - logger.warn("unknown request seqId = {}", seqId); - } - } else { - logger.warn("illegal request pack"); - } - } else { - logger.warn("unknown pack"); - } - } - - private void heartbeat() { - long startTime = System.currentTimeMillis(); - try { - if (status == Status.CLOSING) { - return; - } - boolean disabled = DynamicConf.getBoolean("transport.client.heartbeat.disabled", false); - if (disabled) { - return; - } - ProxyPackHeader header = newHeader(ProxyPackCmd.HEARTBEAT); - ProxyPack pack = ProxyPack.newPack(header, new HeartbeatPack()); - CompletableFuture future = new CompletableFuture<>(); - heartbeatMap.put(header.getSeqId(), future); - send0(pack); - int timeout = heartbeatTimeoutSeconds(); - Boolean online = future.get(timeout, TimeUnit.SECONDS); - if (status != Status.INVALID && status != Status.CLOSING) { - if (online != null && online) { - status = Status.ONLINE; - } else { - status = Status.OFFLINE; - } - } - } catch (Exception e) { - logger.error("heartbeat timeout, type = {}, addr = {}, id = {}, spendMs = {}", getType(), getAddr(), getId(), System.currentTimeMillis() - startTime); - status = Status.INVALID; - stop(); - } - } - - private ProxyPackHeader newHeader(ProxyPackCmd cmd) { - ProxyPackHeader header = new ProxyPackHeader(); - header.setCmd(cmd); - header.setSeqId(seqIdGen.incrementAndGet()); - return header; - } - - @Override - public long getId() { - return id; - } - - @Override - public Status getStatus() { - return status; - } - - @Override - public ServerAddr getAddr() { - return addr; - } - - @Override - public void setClosingStatus() { - if (status != Status.INVALID) { - status = Status.CLOSING; - } - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/AbstractTransportClients.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/AbstractTransportClients.java deleted file mode 100644 index 2a5fd3637..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/AbstractTransportClients.java +++ /dev/null @@ -1,199 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport; - -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ErrorReason; -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.constants.Constants; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyRequest; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyResponse; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config.TransportServerType; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.DynamicAddrs; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.Status; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.ServerAddr; -import com.netease.nim.camellia.tools.base.DynamicValueGetter; -import com.netease.nim.camellia.tools.executor.CamelliaThreadFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.*; -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * Created by caojiajun on 2023/7/6 - */ -public abstract class AbstractTransportClients implements ITransportClient { - - private static final Logger logger = LoggerFactory.getLogger(AbstractTransportClients.class); - - private final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new CamelliaThreadFactory("transport-client-scheduler")); - private final ThreadPoolExecutor executor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors(), - 0, TimeUnit.SECONDS, new LinkedBlockingDeque<>(10000), new CamelliaThreadFactory("transport-client-executor")); - - private final DynamicAddrs dynamicAddrs; - private final DynamicValueGetter connectCount; - private final AtomicBoolean refreshing = new AtomicBoolean(false); - private ScheduledFuture scheduledFuture; - //连接管理 - private final ConcurrentHashMap> map = new ConcurrentHashMap<>(); - //合法地址列表 - private CopyOnWriteArrayList validAddrs = new CopyOnWriteArrayList<>(); - - public AbstractTransportClients(DynamicAddrs dynamicAddrs, DynamicValueGetter connectCount) { - this.dynamicAddrs = dynamicAddrs; - this.connectCount = connectCount; - } - - public abstract Client initClinet(ServerAddr addr); - - public abstract TransportServerType transportServerType(); - - @Override - public void start() { - refresh(); - int intervalSeconds = DynamicConf.getInt("transport.client.refresh.interval.seconds", 30); - scheduledFuture = scheduledExecutor.scheduleAtFixedRate(this::refresh, intervalSeconds, intervalSeconds, TimeUnit.SECONDS); - } - - @Override - public void stop() { - if (scheduledFuture != null) { - scheduledFuture.cancel(false); - } - scheduledExecutor.shutdown(); - executor.shutdown(); - for (Map.Entry> entry : map.entrySet()) { - for (Client client : entry.getValue()) { - client.stop(); - } - } - } - - @Override - public CompletableFuture send(ProxyRequest proxyRequest) { - CompletableFuture future = new CompletableFuture<>(); - Client client1 = selectClient(); - if (client1 != null) { - proxyRequest.getLogBean().setTransportAddr(client1.getAddr().toString()); - proxyRequest.getLogBean().setTransportClientId(client1.getId()); - proxyRequest.getLogBean().setTransportServerType(transportServerType()); - client1.send(proxyRequest, future); - return future; - } - try { - proxyRequest.getRequest().content().retain(); - //走一下异步,确保不会阻塞 - executor.submit(() -> { - try { - refresh(); - Client client2 = selectClient(); - if (client2 != null) { - proxyRequest.getLogBean().setTransportAddr(client2.getAddr().toString()); - proxyRequest.getLogBean().setTransportClientId(client2.getId()); - proxyRequest.getLogBean().setTransportServerType(transportServerType()); - client2.send(proxyRequest, future); - } else { - proxyRequest.getLogBean().setErrorReason(ErrorReason.TRANSPORT_SERVER_SELECT_FAIL); - future.complete(new ProxyResponse(Constants.BAD_GATEWAY, proxyRequest.getLogBean())); - } - } catch (Exception e) { - proxyRequest.getLogBean().setErrorReason(ErrorReason.TRANSPORT_SERVER_SELECT_FAIL); - future.complete(new ProxyResponse(Constants.BAD_GATEWAY, proxyRequest.getLogBean())); - logger.error(e.getMessage(), e); - } - }); - } catch (Exception e) { - logger.error("send error, request = {}", proxyRequest.getRequest(), e); - proxyRequest.getLogBean().setErrorReason(ErrorReason.TRANSPORT_SERVER_SELECT_FAIL); - future.complete(new ProxyResponse(Constants.BAD_GATEWAY, proxyRequest.getLogBean())); - } - return future; - } - - private Client selectClient() { - int retry = 3; - while (retry-- > 0) { - try { - if (validAddrs.isEmpty()) { - return null; - } - int index = ThreadLocalRandom.current().nextInt(validAddrs.size()); - ServerAddr addr = validAddrs.get(index); - CopyOnWriteArrayList list = map.get(addr); - if (list.isEmpty()) { - validAddrs.remove(addr); - } - int index2 = ThreadLocalRandom.current().nextInt(list.size()); - Client client = list.get(index2); - if (client.getStatus() == Status.ONLINE) { - return client; - } - if (client.getStatus() == Status.OFFLINE) { - validAddrs.remove(addr); - } else if (client.getStatus() == Status.INVALID) { - list.removeIf(c -> c.getStatus() == Status.INVALID); - } - } catch (Exception e) { - logger.warn("select error, e = {}", e.toString()); - } - } - return null; - } - - private void refresh() { - if (refreshing.compareAndSet(false, true)) { - try { - List addrs = dynamicAddrs.getAddrs(); - CopyOnWriteArrayList validAddrs = new CopyOnWriteArrayList<>(); - for (ServerAddr addr : addrs) { - try { - CopyOnWriteArrayList list = map.get(addr); - if (list == null) { - list = new CopyOnWriteArrayList<>(); - map.put(addr, list); - } - list.removeIf(client -> client.getStatus() == Status.INVALID); - Integer count = connectCount.get(); - int diff = list.size() - count; - if (diff > 0) { - //remove - for (int i=count; i future; - if (client == null) { - future = new CompletableFuture<>(); - proxyRequest.getLogBean().setErrorReason(ErrorReason.UPSTREAM_SERVER_ROUTE_FAIL); - future.complete(new ProxyResponse(Constants.BAD_GATEWAY, proxyRequest.getLogBean())); - } else { - future = client.send(proxyRequest); - } - future.thenAccept(response -> { - try { - header.setAck(); - response.getLogBean().setCode(response.getResponse().status().code()); - ctx.channel().writeAndFlush(ProxyPack.newPack(header, new RequestAckPack(response)).encode(ctx.alloc())); - } finally { - LoggerUtils.logging(response.getLogBean()); - } - }); - } - } else { - logger.warn("unknown pack, seqId = {}", header.getSeqId()); - } - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/Client.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/Client.java deleted file mode 100644 index 6241b88b3..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/Client.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport; - -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyRequest; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyResponse; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config.TransportServerType; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.ServerAddr; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.Status; - -import java.util.concurrent.CompletableFuture; - -/** - * Created by caojiajun on 2023/7/25 - */ -public interface Client { - - TransportServerType getType(); - - void start(); - - void stop(); - - void send(ProxyRequest request, CompletableFuture future); - - long getId(); - - Status getStatus(); - - ServerAddr getAddr(); - - void setClosingStatus(); -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/ITransportClient.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/ITransportClient.java deleted file mode 100644 index 40effb91c..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/ITransportClient.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport; - -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyRequest; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyResponse; - -import java.util.concurrent.CompletableFuture; - -/** - * 把请求从本地sidecar-proxy转发给其他sidecar-proxy - * Created by caojiajun on 2023/7/6 - */ -public interface ITransportClient { - - - /** - * send the request to ISidCarProxyServer - * @param proxyRequest request - * @return response - */ - CompletableFuture send(ProxyRequest proxyRequest); - - void start(); - - void stop(); -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/ITransportServer.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/ITransportServer.java deleted file mode 100644 index 3ab455814..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/ITransportServer.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport; - -import com.netease.nim.camellia.http.accelerate.proxy.core.status.ServerStartupStatus; - -/** - * 接收来自其他sidecar-proxy的请求 - * Created by caojiajun on 2023/7/6 - */ -public interface ITransportServer { - - void start(); - - ServerStartupStatus getStatus(); -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/HeartbeatAckPack.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/HeartbeatAckPack.java deleted file mode 100644 index f38638ae5..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/HeartbeatAckPack.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec; - -import com.netease.nim.camellia.codec.Pack; -import com.netease.nim.camellia.codec.Props; -import com.netease.nim.camellia.codec.Unpack; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class HeartbeatAckPack extends ProxyPackBody { - - private Props props = new Props(); - - private static enum Tag { - status(1), - ; - - private final int value; - - Tag(int value) { - this.value = value; - } - } - - public HeartbeatAckPack(boolean online) { - props.putInteger(Tag.status.value, online ? 1 : 0); - } - - public HeartbeatAckPack() { - } - - public boolean isOnline() { - return props.getInteger(Tag.status.value) == 1; - } - - @Override - public void marshal(Pack pack) { - pack.putMarshallable(props); - } - - @Override - public void unmarshal(Unpack unpack) { - props = new Props(); - unpack.popMarshallable(props); - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/HeartbeatPack.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/HeartbeatPack.java deleted file mode 100644 index b8c04c29a..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/HeartbeatPack.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec; - -import com.netease.nim.camellia.codec.Pack; -import com.netease.nim.camellia.codec.Unpack; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class HeartbeatPack extends ProxyPackBody { - - @Override - public void marshal(Pack pack) { - - } - - @Override - public void unmarshal(Unpack unpack) { - - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/LogBeanTag.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/LogBeanTag.java deleted file mode 100644 index ea22b944e..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/LogBeanTag.java +++ /dev/null @@ -1,148 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec; - -import com.netease.nim.camellia.codec.Props; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ErrorReason; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.LogBean; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config.TransportServerType; - -/** - * Created by caojiajun on 2023/7/10 - */ -public enum LogBeanTag { - - host(1), - path(2), - traceId(3), - startTime(4), - transportServerSendTime(5), - transportServerReceiveTime(6), - upstreamSendTime(7), - upstreamReplyTime(8), - endTime(9), - transportAddr(10), - upstreamAddr(11), - errorReason(12), - code(13), - transportClientId(14), - transportServerType(15), - ; - - private final int value; - - LogBeanTag(int value) { - this.value = value; - } - - public int getValue() { - return value; - } - - public static LogBeanTag getByValue(int value) { - for (LogBeanTag tag : LogBeanTag.values()) { - if (tag.value == value) { - return tag; - } - } - return null; - } - - public static Props logBean(LogBean logBean) { - Props props = new Props(); - if (logBean.getTraceId() != null) { - props.putString(LogBeanTag.traceId.getValue(), logBean.getTraceId()); - } - if (logBean.getHost() != null) { - props.putString(LogBeanTag.host.getValue(), logBean.getHost()); - } - if (logBean.getPath() != null) { - props.putString(LogBeanTag.path.getValue(), logBean.getPath()); - } - if (logBean.getStartTime() != null) { - props.putLong(LogBeanTag.startTime.getValue(), logBean.getStartTime()); - } - if (logBean.getTransportServerSendTime() != null) { - props.putLong(LogBeanTag.transportServerSendTime.getValue(), logBean.getTransportServerSendTime()); - } - if (logBean.getTransportServerReceiveTime() != null) { - props.putLong(LogBeanTag.transportServerReceiveTime.getValue(), logBean.getTransportServerReceiveTime()); - } - if (logBean.getUpstreamSendTime() != null) { - props.putLong(LogBeanTag.upstreamSendTime.getValue(), logBean.getUpstreamSendTime()); - } - if (logBean.getUpstreamReplyTime() != null) { - props.putLong(LogBeanTag.upstreamReplyTime.getValue(), logBean.getUpstreamReplyTime()); - } - if (logBean.getEndTime() != null) { - props.putLong(LogBeanTag.endTime.getValue(), logBean.getEndTime()); - } - if (logBean.getTransportAddr() != null) { - props.putString(LogBeanTag.transportAddr.getValue(), logBean.getTransportAddr()); - } - if (logBean.getUpstreamAddr() != null) { - props.putString(LogBeanTag.upstreamAddr.getValue(), logBean.getUpstreamAddr()); - } - if (logBean.getErrorReason() != null) { - props.putInteger(LogBeanTag.errorReason.getValue(), logBean.getErrorReason().getValue()); - } - if (logBean.getCode() != null) { - props.putInteger(LogBeanTag.code.getValue(), logBean.getCode()); - } - if (logBean.getTransportClientId() != null) { - props.putLong(LogBeanTag.transportClientId.getValue(), logBean.getTransportClientId()); - } - if (logBean.getTransportServerType() != null) { - props.putInteger(LogBeanTag.transportServerType.getValue(), logBean.getTransportServerType().getValue()); - } - return props; - } - - public static LogBean parseProps(Props props) { - LogBean logBean = new LogBean(); - if (props.containsKey(LogBeanTag.traceId.getValue())) { - logBean.setTraceId(props.getString(LogBeanTag.traceId.getValue())); - } - if (props.containsKey(LogBeanTag.host.getValue())) { - logBean.setHost(props.getString(LogBeanTag.host.getValue())); - } - if (props.containsKey(LogBeanTag.path.getValue())) { - logBean.setPath(props.getString(LogBeanTag.path.getValue())); - } - if (props.containsKey(LogBeanTag.startTime.getValue())) { - logBean.setStartTime(props.getLong(LogBeanTag.startTime.getValue())); - } - if (props.containsKey(LogBeanTag.transportServerSendTime.getValue())) { - logBean.setTransportServerSendTime(props.getLong(LogBeanTag.transportServerSendTime.getValue())); - } - if (props.containsKey(LogBeanTag.transportServerReceiveTime.getValue())) { - logBean.setTransportServerReceiveTime(props.getLong(LogBeanTag.transportServerReceiveTime.getValue())); - } - if (props.containsKey(LogBeanTag.upstreamSendTime.getValue())) { - logBean.setUpstreamSendTime(props.getLong(LogBeanTag.upstreamSendTime.getValue())); - } - if (props.containsKey(LogBeanTag.upstreamReplyTime.getValue())) { - logBean.setUpstreamReplyTime(props.getLong(LogBeanTag.upstreamReplyTime.getValue())); - } - if (props.containsKey(LogBeanTag.endTime.getValue())) { - logBean.setEndTime(props.getLong(LogBeanTag.endTime.getValue())); - } - if (props.containsKey(LogBeanTag.transportAddr.getValue())) { - logBean.setTransportAddr(props.getString(LogBeanTag.transportAddr.getValue())); - } - if (props.containsKey(LogBeanTag.upstreamAddr.getValue())) { - logBean.setUpstreamAddr(props.getString(LogBeanTag.upstreamAddr.getValue())); - } - if (props.containsKey(LogBeanTag.errorReason.getValue())) { - logBean.setErrorReason(ErrorReason.getByValue(props.getInteger(LogBeanTag.errorReason.getValue()))); - } - if (props.containsKey(LogBeanTag.code.getValue())) { - logBean.setCode(props.getInteger(LogBeanTag.code.getValue())); - } - if (props.containsKey(LogBeanTag.transportClientId.getValue())) { - logBean.setTransportClientId(props.getLong(LogBeanTag.transportClientId.getValue())); - } - if (props.containsKey(LogBeanTag.transportServerType.getValue())) { - logBean.setTransportServerType(TransportServerType.getByValue(props.getInteger(LogBeanTag.transportServerType.getValue()))); - } - return logBean; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPack.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPack.java deleted file mode 100644 index 3f3281bda..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPack.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec; - -import com.netease.nim.camellia.codec.Pack; -import com.netease.nim.camellia.codec.Unpack; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufAllocator; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class ProxyPack { - - private ProxyPackHeader header; - private ProxyPackBody body; - - public ProxyPack(ProxyPackHeader header, ProxyPackBody body) { - this.header = header; - this.body = body; - } - - public ProxyPack() { - } - - public static ProxyPack newPack(ProxyPackHeader header, ProxyPackBody body) { - return new ProxyPack(header, body); - } - - public ProxyPackHeader getHeader() { - return header; - } - - public ProxyPackBody getBody() { - return body; - } - - public ByteBuf encode(ByteBufAllocator allocator) { - Pack pack = new Pack(allocator, 1024); - pack.putInt(0); - - pack.putMarshallable(header); - if (body != null) { - pack.putMarshallable(body); - } - - pack.getBuffer().capacity(pack.getBuffer().readableBytes()); - - pack.replaceInt(0, pack.size()); - - return pack.getBuffer(); - } - - public void decode(Unpack unpack) { - unpack.popInt(); - header = new ProxyPackHeader(); - unpack.popMarshallable(header); - ProxyPackCmd cmd = header.getCmd(); - if (header.isAck()) { - switch (cmd) { - case HEARTBEAT: - body = new HeartbeatAckPack(); - unpack.popMarshallable(body); - break; - case REQUEST: - body = new RequestAckPack(); - unpack.popMarshallable(body); - break; - default: - throw new IllegalArgumentException("unknown TcpPackCmd"); - } - } else { - switch (cmd) { - case HEARTBEAT: - body = new HeartbeatPack(); - unpack.popMarshallable(body); - break; - case REQUEST: - body = new RequestPack(); - unpack.popMarshallable(body); - break; - default: - throw new IllegalArgumentException("unknown TcpPackCmd"); - } - } - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPackBody.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPackBody.java deleted file mode 100644 index 8a8faf72f..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPackBody.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec; - -import com.netease.nim.camellia.codec.Marshallable; - -/** - * Created by caojiajun on 2023/7/7 - */ -public abstract class ProxyPackBody implements Marshallable { -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPackCmd.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPackCmd.java deleted file mode 100644 index 565846ac2..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPackCmd.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec; - -/** - * Created by caojiajun on 2023/7/7 - */ -public enum ProxyPackCmd { - - REQUEST((byte) 1), - HEARTBEAT((byte) 2), - ; - - private final byte value; - - ProxyPackCmd(byte value) { - this.value = value; - } - - public byte getValue() { - return value; - } - - public static ProxyPackCmd getByValue(byte value) { - for (ProxyPackCmd cmd : ProxyPackCmd.values()) { - if (cmd.value == value) { - return cmd; - } - } - return null; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPackDecoder.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPackDecoder.java deleted file mode 100644 index f7a4ae0cc..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPackDecoder.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec; - -import com.netease.nim.camellia.codec.Unpack; -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ByteToMessageDecoder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; - -/** - * Created by caojiajun on 2023/5/8 - */ -public class ProxyPackDecoder extends ByteToMessageDecoder { - - private static final Logger logger = LoggerFactory.getLogger(ProxyPackDecoder.class); - - public static String getName() { - return "ProxyPackDecoder"; - } - - @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List list) { - if (buf.readableBytes() > 4) { - int len = buf.getInt(buf.readerIndex()); - if (len > 40 * 1024 * 1024 || len < 0) { - ctx.channel().close(); - logger.error("incorrect size, channel = {}, size = {}, channel will force close", ctx.channel(), len); - return; - } - if (len > buf.readableBytes()) { - return; - } - ByteBuf byteBuf = buf.readBytes(len); - byte[] bytes = new byte[byteBuf.readableBytes()]; - byteBuf.readBytes(bytes); - byteBuf.release(); - Unpack unpack = new Unpack(bytes); - ProxyPack pack = new ProxyPack(); - pack.decode(unpack); - list.add(pack); - } - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPackHeader.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPackHeader.java deleted file mode 100644 index 24e8b91c6..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/ProxyPackHeader.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec; - -import com.netease.nim.camellia.codec.Marshallable; -import com.netease.nim.camellia.codec.Pack; -import com.netease.nim.camellia.codec.Props; -import com.netease.nim.camellia.codec.Unpack; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class ProxyPackHeader implements Marshallable { - - private ProxyPackCmd cmd; - private byte tag = 0; - private long seqId; - private Props context = new Props(); - - @Override - public void marshal(Pack pack) { - pack.putByte(cmd.getValue()); - pack.putByte(tag); - pack.putLong(seqId); - pack.putMarshallable(context); - } - - @Override - public void unmarshal(Unpack unpack) { - this.cmd = ProxyPackCmd.getByValue(unpack.popByte()); - this.tag = unpack.popByte(); - this.seqId = unpack.popLong(); - this.context = new Props(); - unpack.popMarshallable(context); - } - - public ProxyPackCmd getCmd() { - return cmd; - } - - public void setCmd(ProxyPackCmd cmd) { - this.cmd = cmd; - } - - public byte getTag() { - return tag; - } - - public void setTag(byte tag) { - this.tag = tag; - } - - public long getSeqId() { - return seqId; - } - - public void setSeqId(long seqId) { - this.seqId = seqId; - } - - public Props getContext() { - return context; - } - - public void setContext(Props context) { - this.context = context; - } - - public static enum Tag { - - DEFAULT((byte) 0), - ACK((byte) 1),//是否是响应包 - - ; - - private final byte value; - - Tag(byte value) { - this.value = value; - } - - public byte getValue() { - return value; - } - - public static Tag getByValue(int tagValue) { - for (Tag t : Tag.values()) { - if (t.getValue() == tagValue) { - return t; - } - } - return DEFAULT; - } - } - - public void setAck() { - tag |= Tag.ACK.getValue(); - } - - public boolean isAck() { - return 0 != (tag & Tag.ACK.getValue()); - } - -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/RequestAckPack.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/RequestAckPack.java deleted file mode 100644 index 682e81c7a..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/RequestAckPack.java +++ /dev/null @@ -1,130 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec; - -import com.netease.nim.camellia.codec.Pack; -import com.netease.nim.camellia.codec.Props; -import com.netease.nim.camellia.codec.StrStrMap; -import com.netease.nim.camellia.codec.Unpack; -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.LogBean; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyResponse; -import com.netease.nim.camellia.tools.compress.CamelliaCompressor; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.handler.codec.http.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Map; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class RequestAckPack extends ProxyPackBody { - - private static final Logger logger = LoggerFactory.getLogger(RequestAckPack.class); - private static final CamelliaCompressor compressor = new CamelliaCompressor(DynamicConf.getInt("response.content.compress.threshold", 1024)); - - private ProxyResponse proxyResponse; - - public RequestAckPack(ProxyResponse response) { - this.proxyResponse = response; - } - - public RequestAckPack() { - } - - public ProxyResponse getProxyResponse() { - return proxyResponse; - } - - @Override - public void marshal(Pack pack) { - - FullHttpResponse response = proxyResponse.getResponse(); - LogBean logBean = proxyResponse.getLogBean(); - Props props = LogBeanTag.logBean(logBean); - pack.putMarshallable(props); - - HttpVersion httpVersion = response.protocolVersion(); - pack.putVarstr(httpVersion.text()); - - HttpResponseStatus status = response.status(); - pack.putInt(status.code()); - HttpHeaders headers = response.headers(); - StrStrMap map1 = new StrStrMap(); - for (Map.Entry header : headers) { - map1.put(header.getKey(), header.getValue()); - } - pack.putMarshallable(map1); - - HttpHeaders trailingHeaders = response.trailingHeaders(); - StrStrMap map2 = new StrStrMap(); - for (Map.Entry header : trailingHeaders) { - map2.put(header.getKey(), header.getValue()); - } - pack.putMarshallable(map2); - - ByteBuf content = response.content(); - int readableBytes = content.readableBytes(); - if (readableBytes > 0) { - if (DynamicConf.getBoolean("response.content.compress.enable", true)) { - byte[] originalData = new byte[readableBytes]; - content.readBytes(originalData); - byte[] compressedData = compressor.compress(originalData); - pack.putInt(compressedData.length); - pack.putBuffer(Unpooled.wrappedBuffer(compressedData)); - if (originalData.length != compressedData.length) { - logger.info("response content compressed, original.len = {}, compressed.len = {}, host = {}, path = {}, traceId = {}", - originalData.length, compressedData.length, logBean.getHost(), logBean.getPath(), logBean.getTraceId()); - } - } else { - pack.putInt(readableBytes); - pack.putBuffer(content); - } - } else { - pack.putInt(0); - } - } - - @Override - public void unmarshal(Unpack unpack) { - - Props props = new Props(); - unpack.popMarshallable(props); - LogBean logBean = LogBeanTag.parseProps(props); - - HttpVersion httpVersion = HttpVersion.valueOf(unpack.popVarstr()); - - HttpResponseStatus httpResponseStatus = HttpResponseStatus.valueOf(unpack.popInt()); - - StrStrMap map1 = new StrStrMap(); - unpack.popMarshallable(map1); - HttpHeaders headers = new DefaultHttpHeaders(); - for (Map.Entry entry : map1.m_map.entrySet()) { - headers.set(entry.getKey(), entry.getValue()); - } - - StrStrMap map2 = new StrStrMap(); - unpack.popMarshallable(map2); - HttpHeaders trailingHeaders = new DefaultHttpHeaders(); - for (Map.Entry entry : map2.m_map.entrySet()) { - trailingHeaders.set(entry.getKey(), entry.getValue()); - } - - FullHttpResponse response; - int size = unpack.popInt(); - if (size > 0) { - byte[] raw = unpack.popFetch(size); - byte[] decompressed = compressor.decompress(raw); - ByteBuf buffer = Unpooled.wrappedBuffer(decompressed); - if (raw.length != decompressed.length) { - logger.info("response content decompressed, original.len = {}, decompressed.len = {}, host = {}, path = {}, traceId = {}", - raw.length, decompressed.length, logBean.getHost(), logBean.getPath(), logBean.getTraceId()); - } - response = new DefaultFullHttpResponse(httpVersion, httpResponseStatus, buffer, headers, trailingHeaders); - } else { - response = new DefaultFullHttpResponse(httpVersion, httpResponseStatus, Unpooled.buffer(0), headers, trailingHeaders); - } - this.proxyResponse = new ProxyResponse(response, logBean); - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/RequestPack.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/RequestPack.java deleted file mode 100644 index 2e5109cb4..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/codec/RequestPack.java +++ /dev/null @@ -1,128 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec; - -import com.netease.nim.camellia.codec.Pack; -import com.netease.nim.camellia.codec.Props; -import com.netease.nim.camellia.codec.StrStrMap; -import com.netease.nim.camellia.codec.Unpack; -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.LogBean; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyRequest; -import com.netease.nim.camellia.tools.compress.CamelliaCompressor; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.handler.codec.http.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Map; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class RequestPack extends ProxyPackBody { - - private static final Logger logger = LoggerFactory.getLogger(RequestPack.class); - private static final CamelliaCompressor compressor = new CamelliaCompressor(DynamicConf.getInt("request.content.compress.threshold", 1024)); - - private ProxyRequest proxyRequest; - - public RequestPack(ProxyRequest request) { - this.proxyRequest = request; - } - - public RequestPack() { - } - - public ProxyRequest getProxyRequest() { - return proxyRequest; - } - - @Override - public void marshal(Pack pack) { - - FullHttpRequest request = proxyRequest.getRequest(); - LogBean logBean = proxyRequest.getLogBean(); - Props props = LogBeanTag.logBean(logBean); - pack.putMarshallable(props); - - HttpVersion httpVersion = request.protocolVersion(); - pack.putVarstr(httpVersion.text()); - - pack.putVarstr(request.method().name()); - String uri = request.uri(); - pack.putVarstr(uri); - HttpHeaders headers = request.headers(); - StrStrMap map1 = new StrStrMap(); - for (Map.Entry header : headers) { - map1.put(header.getKey(), header.getValue()); - } - pack.putMarshallable(map1); - HttpHeaders trailingHeaders = request.trailingHeaders(); - StrStrMap map2 = new StrStrMap(); - for (Map.Entry header : trailingHeaders) { - map2.put(header.getKey(), header.getValue()); - } - pack.putMarshallable(map2); - ByteBuf content = request.content(); - int readableBytes = content.readableBytes(); - if (readableBytes > 0) { - if (DynamicConf.getBoolean("request.content.compress.enable", true)) { - byte[] originalData = new byte[readableBytes]; - content.readBytes(originalData); - byte[] compressedData = compressor.compress(originalData); - pack.putInt(compressedData.length); - pack.putBuffer(Unpooled.wrappedBuffer(compressedData)); - if (originalData.length != compressedData.length) { - logger.info("request content compressed, original.len = {}, compressed.len = {}, host = {}, path = {}, traceId = {}", - originalData.length, compressedData.length, logBean.getHost(), logBean.getPath(), logBean.getTraceId()); - } - } else { - pack.putInt(readableBytes); - pack.putBuffer(content); - } - } else { - pack.putInt(0); - } - } - - @Override - public void unmarshal(Unpack unpack) { - - Props props = new Props(); - unpack.popMarshallable(props); - LogBean logBean = LogBeanTag.parseProps(props); - - HttpVersion httpVersion = HttpVersion.valueOf(unpack.popVarstr()); - - HttpMethod httpMethod = HttpMethod.valueOf(unpack.popVarstr()); - String uri = unpack.popVarstr(); - StrStrMap map1 = new StrStrMap(); - unpack.popMarshallable(map1); - HttpHeaders headers = new DefaultHttpHeaders(); - for (Map.Entry entry : map1.m_map.entrySet()) { - headers.set(entry.getKey(), entry.getValue()); - } - - StrStrMap map2 = new StrStrMap(); - unpack.popMarshallable(map2); - HttpHeaders trailingHeaders = new DefaultHttpHeaders(); - for (Map.Entry entry : map2.m_map.entrySet()) { - trailingHeaders.set(entry.getKey(), entry.getValue()); - } - FullHttpRequest request; - int size = unpack.popInt(); - if (size > 0) { - byte[] raw = unpack.popFetch(size); - byte[] decompressed = compressor.decompress(raw); - ByteBuf buffer = Unpooled.wrappedBuffer(decompressed); - if (raw.length != decompressed.length) { - logger.info("request content decompressed, original.len = {}, decompressed.len = {}, host = {}, path = {}, traceId = {}", - raw.length, decompressed.length, logBean.getHost(), logBean.getPath(), logBean.getTraceId()); - } - request = new DefaultFullHttpRequest(httpVersion, httpMethod, uri, buffer, headers, trailingHeaders); - } else { - request = new DefaultFullHttpRequest(httpVersion, httpMethod, uri, Unpooled.buffer(0), headers, trailingHeaders); - } - this.proxyRequest = new ProxyRequest(request, logBean); - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/model/DynamicAddrs.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/model/DynamicAddrs.java deleted file mode 100644 index 5e8f2e150..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/model/DynamicAddrs.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.model; - -import java.util.List; - -/** - * Created by caojiajun on 2023/7/7 - */ -public interface DynamicAddrs { - - List getAddrs(); - -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/model/ServerAddr.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/model/ServerAddr.java deleted file mode 100644 index 6e223dc4e..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/model/ServerAddr.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.model; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class ServerAddr { - private final String host; - private final int port; - - public ServerAddr(String host, int port) { - this.host = host; - this.port = port; - } - - public String getHost() { - return host; - } - - public int getPort() { - return port; - } - - public static List toAddrs(List list) { - List addrs = new ArrayList<>(); - for (String str : list) { - addrs.add(toAddr(str)); - } - return addrs; - } - - public static ServerAddr toAddr(String str) { - String[] split = str.split(":"); - return new ServerAddr(split[0], Integer.parseInt(split[1])); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - ServerAddr tcpAddr = (ServerAddr) o; - return port == tcpAddr.port && Objects.equals(host, tcpAddr.host); - } - - @Override - public int hashCode() { - return Objects.hash(host, port); - } - - @Override - public String toString() { - return host + ":" + port; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/model/Status.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/model/Status.java deleted file mode 100644 index bc8fb7ab5..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/model/Status.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.model; - -/** - * Created by caojiajun on 2023/7/7 - */ -public enum Status { - - ONLINE, - OFFLINE, - INVALID, - CLOSING, - ; -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/quic/QuicClient.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/quic/QuicClient.java deleted file mode 100644 index 056aff2dd..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/quic/QuicClient.java +++ /dev/null @@ -1,176 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.quic; - -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config.TransportServerType; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.AbstractClient; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec.ProxyPack; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec.ProxyPackCmd; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec.ProxyPackDecoder; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.ServerAddr; -import com.netease.nim.camellia.tools.executor.CamelliaThreadFactory; -import io.netty.bootstrap.Bootstrap; -import io.netty.channel.*; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.nio.NioDatagramChannel; -import io.netty.handler.ssl.util.InsecureTrustManagerFactory; -import io.netty.incubator.codec.quic.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.net.InetSocketAddress; -import java.util.concurrent.TimeUnit; - - -/** - * Created by caojiajun on 2023/7/6 - */ -public class QuicClient extends AbstractClient { - - private static final Logger logger = LoggerFactory.getLogger(QuicClient.class); - - private static final NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), - new CamelliaThreadFactory("quic-client")); - - private Channel channel; - private QuicChannel quicChannel; - private QuicStreamChannel streamChannel; - private QuicStreamChannel heartbeatStreamChannel; - - public QuicClient(ServerAddr addr) { - super(addr); - } - - @Override - public void start0() throws Exception { - QuicSslContext context = QuicSslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE). - applicationProtocols("camellia").build(); - - long maxIdleTimeoutMillis = DynamicConf.getLong("transport.quic.client.max.idle.timeout.millis", 60 * 1000L); - long initialMaxData = DynamicConf.getLong("transport.quic.client.initial.max.data", 10000_0000L); - long initialMaxStreamDataBidiLocal = DynamicConf.getLong("transport.quic.client.initial.max.stream.data.bidirectional.local", 10000_0000L); - long initialMaxStreamDataBidiRemote = DynamicConf.getLong("transport.quic.client.initial.max.stream.data.bidirectional.remote", 10000_0000L); - long initialMaxStreamsBidirectional = DynamicConf.getLong("transport.quic.client.initial.max.streams.bidirectional", 10000L); - long initialMaxStreamsUnidirectional = DynamicConf.getLong("transport.quic.client.initial.max.streams.unidirectional", 10000L); - - String algorithm = DynamicConf.getString("transport.quic.client.congestion.control.algorithm", QuicCongestionControlAlgorithm.BBR.name()); - QuicCongestionControlAlgorithm congestionControlAlgorithm = null; - if (algorithm != null) { - for (QuicCongestionControlAlgorithm value : QuicCongestionControlAlgorithm.values()) { - if (value.name().equalsIgnoreCase(algorithm)) { - congestionControlAlgorithm = value; - } - } - } - - QuicClientCodecBuilder builder = new QuicClientCodecBuilder() - .sslContext(context) - .maxIdleTimeout(maxIdleTimeoutMillis, TimeUnit.MILLISECONDS) - .initialMaxData(initialMaxData) - .initialMaxStreamDataBidirectionalLocal(initialMaxStreamDataBidiLocal) - .initialMaxStreamDataBidirectionalRemote(initialMaxStreamDataBidiRemote) - .initialMaxStreamsBidirectional(initialMaxStreamsBidirectional) - .initialMaxStreamsUnidirectional(initialMaxStreamsUnidirectional); - if (congestionControlAlgorithm != null) { - builder.congestionControlAlgorithm(congestionControlAlgorithm); - } - ChannelHandler codec = builder.build(); - int connectTimeoutMillis = DynamicConf.getInt("transport.quic.client.connect.timeout.millis", 2000); - - Bootstrap bs = new Bootstrap(); - this.channel = bs.group(nioEventLoopGroup) - .channel(NioDatagramChannel.class) - .handler(codec) - .bind(0) - .sync() - .channel(); - - this.quicChannel = QuicChannel.newBootstrap(channel) - .remoteAddress(new InetSocketAddress(getAddr().getHost(), getAddr().getPort())) - .streamHandler(new ChannelInboundHandlerAdapter()) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMillis) - .connect() - .get(); - this.streamChannel = quicChannel.createStream(QuicStreamType.BIDIRECTIONAL, new QuicStreamChannelInitializer(this)).sync().getNow(); - this.heartbeatStreamChannel = quicChannel.createStream(QuicStreamType.BIDIRECTIONAL, new QuicStreamChannelInitializer(this)).sync().getNow(); - this.channel.closeFuture().addListener((ChannelFutureListener) channelFuture -> { - logger.warn("quic client closed, addr = {}, id = {}", getAddr(), getId(), channelFuture.cause()); - stop(); - }); - } - - - @Override - public void send0(ProxyPack proxyPack) { - if (proxyPack.getHeader().getCmd() == ProxyPackCmd.HEARTBEAT) { - heartbeatStreamChannel.writeAndFlush(proxyPack.encode(channel.alloc())); - } else { - streamChannel.writeAndFlush(proxyPack.encode(channel.alloc())); - } - } - - @Override - public void stop0() { - if (streamChannel != null) { - streamChannel.close(); - } - if (heartbeatStreamChannel != null) { - heartbeatStreamChannel.close(); - } - if (quicChannel != null) { - quicChannel.close(); - } - if (channel != null) { - channel.close(); - } - } - - @Override - public TransportServerType getType() { - return TransportServerType.quic; - } - - @Override - public int heartbeatIntervalSeconds() { - return DynamicConf.getInt("quic.client.heartbeat.interval.seconds", 10); - } - - @Override - public int heartbeatTimeoutSeconds() { - return DynamicConf.getInt("quic.client.heartbeat.timeout.seconds", 10); - } - - - private static class QuicStreamChannelInitializer extends ChannelInitializer { - - private final QuicClient quicClient; - - public QuicStreamChannelInitializer(QuicClient quicClient) { - this.quicClient = quicClient; - } - @Override - protected void initChannel(Channel channel) { - ChannelPipeline pipeLine = channel.pipeline(); - pipeLine.addLast(ProxyPackDecoder.getName(), new ProxyPackDecoder()); - pipeLine.addLast(new ChannelInboundHandlerAdapter() { - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) { - try { - if (msg instanceof ProxyPack) { - quicClient.onProxyPack((ProxyPack) msg); - } else { - logger.warn("unknown pack"); - } - } catch (Exception e) { - logger.error("pack error", e); - } - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - super.channelInactive(ctx); - logger.warn("quic stream closed, id = {}, addr = {}", quicClient.getId(), quicClient.getAddr()); - } - }); - } - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/quic/TransportQuicClients.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/quic/TransportQuicClients.java deleted file mode 100644 index a69bd1c08..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/quic/TransportQuicClients.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.quic; - -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config.TransportServerType; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.AbstractTransportClients; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.Client; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.DynamicAddrs; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.ServerAddr; -import com.netease.nim.camellia.tools.base.DynamicValueGetter; - -/** - * Created by caojiajun on 2023/7/25 - */ -public class TransportQuicClients extends AbstractTransportClients { - public TransportQuicClients(DynamicAddrs dynamicAddrs, DynamicValueGetter connectCount) { - super(dynamicAddrs, connectCount); - } - - @Override - public Client initClinet(ServerAddr addr) { - return new QuicClient(addr); - } - - @Override - public TransportServerType transportServerType() { - return TransportServerType.quic; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/quic/TransportQuicServer.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/quic/TransportQuicServer.java deleted file mode 100644 index 96bc4c5fa..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/quic/TransportQuicServer.java +++ /dev/null @@ -1,162 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.quic; - -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.IUpstreamRouter; -import com.netease.nim.camellia.http.accelerate.proxy.core.status.ServerStartupStatus; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.AbstractTransportServer; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec.ProxyPack; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec.ProxyPackDecoder; -import io.netty.bootstrap.Bootstrap; -import io.netty.channel.*; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.nio.NioDatagramChannel; -import io.netty.handler.ssl.util.SelfSignedCertificate; -import io.netty.incubator.codec.quic.*; -import io.netty.util.concurrent.DefaultThreadFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.net.URL; -import java.util.concurrent.TimeUnit; - -/** - * Created by caojiajun on 2023/7/6 - */ -public class TransportQuicServer extends AbstractTransportServer { - - private static final Logger logger = LoggerFactory.getLogger(TransportQuicServer.class); - - private ServerStartupStatus status = ServerStartupStatus.INIT; - - public TransportQuicServer(IUpstreamRouter router) { - super(router); - } - - @Override - public void start() { - String host = DynamicConf.getString("transport.quic.server.host", "0.0.0.0"); - int port = DynamicConf.getInt("transport.quic.server.port", 11500); - if (port <= 0) { - logger.warn("transport quic server skip start"); - status = ServerStartupStatus.SKIP; - return; - } - try { - boolean selfSignedEnable = DynamicConf.getBoolean("transport.quic.server.auto.self.signed.enable", true); - File privateKey; - File certificate; - if (selfSignedEnable) { - SelfSignedCertificate selfSignedCertificate = new SelfSignedCertificate(); - privateKey = selfSignedCertificate.privateKey(); - certificate = selfSignedCertificate.certificate(); - logger.info("privateKey file = {}", privateKey); - logger.info("certificate file = {}", certificate); - } else { - String privateKeyFile = DynamicConf.getString("transport.quic.server.private.key.file", "private_key.key"); - String certificateFile = DynamicConf.getString("transport.quic.server.certificate.file", "certificate.crt"); - URL url1 = TransportQuicServer.class.getClassLoader().getResource(privateKeyFile); - if (url1 == null) { - throw new IllegalArgumentException(privateKeyFile + " not exists"); - } - URL url2 = TransportQuicServer.class.getClassLoader().getResource(certificateFile); - if (url2 == null) { - throw new IllegalArgumentException(certificateFile + " not exists"); - } - privateKey = new File(url1.getPath()); - certificate = new File(url2.getPath()); - } - QuicSslContext context = QuicSslContextBuilder.forServer(privateKey, null, certificate) - .applicationProtocols("camellia").build(); - int workThread = DynamicConf.getInt("transport.quic.server.work.thread", Runtime.getRuntime().availableProcessors()); - - long maxIdleTimeoutMillis = DynamicConf.getLong("transport.quic.server.max.idle.timeout.millis", 120 * 1000L); - long initialMaxData = DynamicConf.getLong("transport.quic.server.initial.max.data", 10000_0000L); - long initialMaxStreamDataBidiLocal = DynamicConf.getLong("transport.quic.server.initial.max.stream.data.bidirectional.local", 10000_0000L); - long initialMaxStreamDataBidiRemote = DynamicConf.getLong("transport.quic.server.initial.max.stream.data.bidirectional.remote", 10000_0000L); - long initialMaxStreamsBidirectional = DynamicConf.getLong("transport.quic.server.initial.max.streams.bidirectional", 10000L); - long initialMaxStreamsUnidirectional = DynamicConf.getLong("transport.quic.server.initial.max.streams.unidirectional", 10000L); - - String algorithm = DynamicConf.getString("transport.quic.server.congestion.control.algorithm", QuicCongestionControlAlgorithm.BBR.name()); - QuicCongestionControlAlgorithm congestionControlAlgorithm = null; - if (algorithm != null) { - for (QuicCongestionControlAlgorithm value : QuicCongestionControlAlgorithm.values()) { - if (value.name().equalsIgnoreCase(algorithm)) { - congestionControlAlgorithm = value; - } - } - } - - NioEventLoopGroup group = new NioEventLoopGroup(workThread, new DefaultThreadFactory("transport-quic-server-work-group")); - QuicServerCodecBuilder builder = new QuicServerCodecBuilder().sslContext(context) - .maxIdleTimeout(maxIdleTimeoutMillis, TimeUnit.MILLISECONDS) - .initialMaxData(initialMaxData) - .initialMaxStreamDataBidirectionalLocal(initialMaxStreamDataBidiLocal) - .initialMaxStreamDataBidirectionalRemote(initialMaxStreamDataBidiRemote) - .initialMaxStreamsBidirectional(initialMaxStreamsBidirectional) - .initialMaxStreamsUnidirectional(initialMaxStreamsUnidirectional) - .congestionControlAlgorithm(congestionControlAlgorithm); - if (congestionControlAlgorithm != null) { - builder.congestionControlAlgorithm(congestionControlAlgorithm); - } - ChannelHandler codec = builder - .tokenHandler(InsecureQuicTokenHandler.INSTANCE) - .handler(new ChannelInboundHandlerAdapter() { - @Override - public boolean isSharable() { - return true; - } - }) - .streamHandler(new ChannelInitializer() { - @Override - protected void initChannel(QuicStreamChannel ch) { - ChannelPipeline pipeline = ch.pipeline(); - pipeline.addLast(ProxyPackDecoder.getName(), new ProxyPackDecoder()); // IN - pipeline.addLast(new ChannelInboundHandlerAdapter() { - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) { - try { - if (msg instanceof ProxyPack) { - onProxyPack(ctx, (ProxyPack) msg); - } else { - logger.warn("unknown pack"); - } - } catch (Exception e) { - logger.error("pack error", e); - } - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - super.channelActive(ctx); - logger.info("new quic client connection, channel = {}", ctx.channel()); - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - super.channelInactive(ctx); - logger.info("quic client connection disconnect, channel = {}", ctx.channel()); - } - }); - } - }).build(); - Bootstrap bs = new Bootstrap(); - bs.group(group) - .channel(NioDatagramChannel.class) - .handler(codec) - .bind(host, port) - .sync(); - logger.info("transport quic server start success, host = {}, port = {}", host, port); - status = ServerStartupStatus.SUCCESS; - } catch (Exception e) { - status = ServerStartupStatus.FAIL; - logger.error("transport quic server start error, host = {}, port = {}", host, port, e); - throw new IllegalStateException(e); - } - } - - @Override - public ServerStartupStatus getStatus() { - return status; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/tcp/TcpClient.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/tcp/TcpClient.java deleted file mode 100644 index b985fb11a..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/tcp/TcpClient.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.tcp; - -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config.TransportServerType; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.AbstractClient; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec.ProxyPack; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec.ProxyPackDecoder; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.ServerAddr; -import com.netease.nim.camellia.tools.executor.CamelliaThreadFactory; -import io.netty.bootstrap.Bootstrap; -import io.netty.channel.*; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.nio.NioSocketChannel; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -/** - * Created by caojiajun on 2023/7/7 - */ -public class TcpClient extends AbstractClient { - - private static final Logger logger = LoggerFactory.getLogger(TcpClient.class); - private static final NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), - new CamelliaThreadFactory("tcp-client")); - - private Channel channel; - - public TcpClient(ServerAddr addr) { - super(addr); - } - - @Override - public TransportServerType getType() { - return TransportServerType.tcp; - } - - public void start0() throws Exception { - boolean tcpNoDelay = DynamicConf.getBoolean("tcp.client.tcp.no.delay", true); - boolean soTcpKeepAlive = DynamicConf.getBoolean("tcp.client.so.keep.alive", true); - int soRcvBuf = DynamicConf.getInt("tcp.client.so.rcvbuf", 10*1024*1024); - int soSndBuf = DynamicConf.getInt("tcp.client.so.sndbuf", 10*1024*1024); - int connectTimeoutMillis = DynamicConf.getInt("tcp.client.connect.timeout.millis", 2000); - int low = DynamicConf.getInt("tcp.client.write.buffer.water.mark.low", 128*1024); - int high = DynamicConf.getInt("tcp.client.write.buffer.water.mark.high", 512*1024); - Bootstrap bootstrap = new Bootstrap() - .group(nioEventLoopGroup) - .channel(NioSocketChannel.class) - .option(ChannelOption.TCP_NODELAY, tcpNoDelay) - .option(ChannelOption.SO_KEEPALIVE, soTcpKeepAlive) - .option(ChannelOption.SO_RCVBUF, soRcvBuf) - .option(ChannelOption.SO_SNDBUF, soSndBuf) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMillis) - .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(low, high)) - .handler(new ChannelInitializer() { - @Override - protected void initChannel(Channel channel) { - ChannelPipeline pipeLine = channel.pipeline(); - pipeLine.addLast(ProxyPackDecoder.getName(), new ProxyPackDecoder()); - pipeLine.addLast(new ChannelInboundHandlerAdapter() { - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) { - try { - if (msg instanceof ProxyPack) { - onProxyPack((ProxyPack) msg); - } else { - logger.warn("unknown pack"); - } - } catch (Exception e) { - logger.error("pack error", e); - } - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - logger.warn("tcp connection closed, id = {}, addr = {}", getId(), getAddr()); - super.channelInactive(ctx); - } - }); - } - }); - ServerAddr addr = getAddr(); - ChannelFuture f = bootstrap.connect(addr.getHost(), addr.getPort()).sync(); - channel = f.channel(); - channel.closeFuture().addListener((ChannelFutureListener) channelFuture -> { - logger.warn("tcp client closed, addr = {}, id = {}", getAddr(), getId(), channelFuture.cause()); - stop(); - }); - } - - public void send0(ProxyPack pack) { - channel.writeAndFlush(pack.encode(channel.alloc())); - } - - public void stop0() { - try { - if (channel != null) { - channel.close(); - } - } catch (Exception e) { - logger.error("channel close error, addr = {}, id = {}", getAddr(), getId(), e); - } - logger.info("TcpClient stopped, addr = {}, id = {}", getAddr(), getId()); - } - - @Override - public int heartbeatIntervalSeconds() { - return DynamicConf.getInt("tcp.client.heartbeat.interval.seconds", 10); - } - - @Override - public int heartbeatTimeoutSeconds() { - return DynamicConf.getInt("tcp.client.heartbeat.timeout.seconds", 10); - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/tcp/TransportTcpClients.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/tcp/TransportTcpClients.java deleted file mode 100644 index a49224f70..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/tcp/TransportTcpClients.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.tcp; - -import com.netease.nim.camellia.http.accelerate.proxy.core.route.transport.config.TransportServerType; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.Client; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.AbstractTransportClients; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.DynamicAddrs; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.model.ServerAddr; -import com.netease.nim.camellia.tools.base.DynamicValueGetter; - -/** - * Created by caojiajun on 2023/7/25 - */ -public class TransportTcpClients extends AbstractTransportClients { - public TransportTcpClients(DynamicAddrs dynamicAddrs, DynamicValueGetter connectCount) { - super(dynamicAddrs, connectCount); - } - - @Override - public Client initClinet(ServerAddr addr) { - return new TcpClient(addr); - } - - @Override - public TransportServerType transportServerType() { - return TransportServerType.tcp; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/tcp/TransportTcpServer.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/tcp/TransportTcpServer.java deleted file mode 100644 index e3a89aeb6..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/transport/tcp/TransportTcpServer.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.transport.tcp; - -import com.netease.nim.camellia.http.accelerate.proxy.core.status.ServerStartupStatus; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.AbstractTransportServer; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec.ProxyPack; -import com.netease.nim.camellia.http.accelerate.proxy.core.transport.codec.ProxyPackDecoder; -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.IUpstreamRouter; -import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.*; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.nio.NioServerSocketChannel; -import io.netty.util.concurrent.DefaultThreadFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Created by caojiajun on 2023/7/7 - */ -public class TransportTcpServer extends AbstractTransportServer { - - private static final Logger logger = LoggerFactory.getLogger(TransportTcpServer.class); - - private ServerStartupStatus status = ServerStartupStatus.INIT; - - public TransportTcpServer(IUpstreamRouter router) { - super(router); - } - - @Override - public void start() { - String host = DynamicConf.getString("transport.tcp.server.host", "0.0.0.0"); - int port = DynamicConf.getInt("transport.tcp.server.port", 11600); - if (port <= 0) { - logger.warn("transport tcp server skip start"); - status = ServerStartupStatus.SKIP; - return; - } - try { - int bossThread = DynamicConf.getInt("transport.tcp.server.boss.thread", 1); - int workThread = DynamicConf.getInt("transport.tcp.server.work.thread", Runtime.getRuntime().availableProcessors()); - EventLoopGroup bossGroup = new NioEventLoopGroup(bossThread, new DefaultThreadFactory("transport-tcp-server-boss-group")); - EventLoopGroup workerGroup = new NioEventLoopGroup(workThread, new DefaultThreadFactory("transport-tcp-server-work-group")); - ServerBootstrap bootstrap = new ServerBootstrap(); - int soBacklog = DynamicConf.getInt("transport.tcp.server.so.backlog", 1024); - int soSndBuf = DynamicConf.getInt("transport.tcp.server.so.sndbuf", 10 * 1024 * 1024); - int soRcvBuf = DynamicConf.getInt("transport.tcp.server.so.rcvbuf", 10 * 1024 * 1024); - boolean tcpNoDelay = DynamicConf.getBoolean("transport.tcp.server.tcp.no.delay", true); - boolean soKeepalive = DynamicConf.getBoolean("transport.tcp.server.so.keep.alive", true); - - int low = DynamicConf.getInt("transport.tcp.server.write.buffer.water.mark.low", 128*1024); - int high = DynamicConf.getInt("transport.tcp.server.write.buffer.water.mark.high", 512*1024); - - bootstrap.group(bossGroup, workerGroup) - .channel(NioServerSocketChannel.class) - .option(ChannelOption.SO_BACKLOG, soBacklog) - .childOption(ChannelOption.SO_SNDBUF, soSndBuf) - .childOption(ChannelOption.SO_RCVBUF, soRcvBuf) - .childOption(ChannelOption.TCP_NODELAY, tcpNoDelay) - .childOption(ChannelOption.SO_KEEPALIVE, soKeepalive) - .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(low, high)) - .childHandler(new ChannelInitializer() { - @Override - protected void initChannel(Channel channel) { - ChannelPipeline pipeline = channel.pipeline(); - pipeline.addLast(ProxyPackDecoder.getName(), new ProxyPackDecoder()); - pipeline.addLast(new ChannelInboundHandlerAdapter() { - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) { - try { - if (msg instanceof ProxyPack) { - onProxyPack(ctx, (ProxyPack) msg); - } else { - logger.warn("unknown pack"); - } - } catch (Exception e) { - logger.error("pack error", e); - } - } - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - super.channelActive(ctx); - logger.info("new tcp client connection, channel = {}", ctx.channel()); - } - - @Override - public void channelInactive(ChannelHandlerContext ctx) throws Exception { - super.channelInactive(ctx); - logger.info("tcp client connection disconnect, channel = {}", ctx.channel()); - } - }); - } - }); - bootstrap.bind(port).sync(); - logger.info("transport tcp server start success, host = {}, port = {}", host, port); - status = ServerStartupStatus.SUCCESS; - } catch (Exception e) { - status = ServerStartupStatus.FAIL; - logger.error("transport tcp server start error, host = {}, port = {}", host, port, e); - throw new IllegalStateException(e); - } - } - - @Override - public ServerStartupStatus getStatus() { - return status; - } -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/upstream/IUpstreamClient.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/upstream/IUpstreamClient.java deleted file mode 100644 index 8eeffdc8b..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/upstream/IUpstreamClient.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.upstream; - -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyRequest; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyResponse; - -import java.util.concurrent.CompletableFuture; - -/** - * 请求发给后端 - * Created by caojiajun on 2023/7/6 - */ -public interface IUpstreamClient { - - /** - * send the request to nginx - * @param request request - * @return response - */ - CompletableFuture send(ProxyRequest request); - -} diff --git a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/upstream/OkHttpUpstreamClient.java b/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/upstream/OkHttpUpstreamClient.java deleted file mode 100644 index 54dee658e..000000000 --- a/camellia-http-accelerate-proxy/camellia-http-accelerate-proxy-core/src/main/java/com/netease/nim/camellia/http/accelerate/proxy/core/upstream/OkHttpUpstreamClient.java +++ /dev/null @@ -1,193 +0,0 @@ -package com.netease.nim.camellia.http.accelerate.proxy.core.upstream; - -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ErrorReason; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.LogBean; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyRequest; -import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyResponse; -import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf; -import com.netease.nim.camellia.http.accelerate.proxy.core.constants.Constants; -import com.netease.nim.camellia.http.accelerate.proxy.core.route.upstream.config.DynamicUpstreamAddrs; -import com.netease.nim.camellia.tools.base.DynamicValueGetter; -import com.netease.nim.camellia.tools.executor.CamelliaThreadFactory; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.handler.codec.http.*; -import okhttp3.*; -import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.net.ConnectException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.*; - -/** - * Created by caojiajun on 2023/7/6 - */ -public class OkHttpUpstreamClient implements IUpstreamClient { - - private static final Logger logger = LoggerFactory.getLogger(OkHttpUpstreamClient.class); - - private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), - new CamelliaThreadFactory("okhttp-upstream-client-heartbeat")); - - private final OkHttpClient okHttpClient; - private final DynamicUpstreamAddrs dynamicUpstreamAddrs; - private final DynamicValueGetter heartbeatUri; - private final DynamicValueGetter heartbeatTimeout; - - private List validAddrs = new ArrayList<>(); - - public OkHttpUpstreamClient(DynamicUpstreamAddrs dynamicUpstreamAddrs, - DynamicValueGetter heartbeatUri, - DynamicValueGetter heartbeatTimeout) { - this.dynamicUpstreamAddrs = dynamicUpstreamAddrs; - this.heartbeatUri = heartbeatUri; - this.heartbeatTimeout = heartbeatTimeout; - int maxRequests = DynamicConf.getInt("okhttp.max.requests", 4096); - int maxRequestsPerHost = DynamicConf.getInt("okhttp.max.requests.per.host", 2048); - Dispatcher dispatcher = new Dispatcher(); - dispatcher.setMaxRequests(maxRequests); - dispatcher.setMaxRequestsPerHost(maxRequestsPerHost); - int maxIdleConnections = DynamicConf.getInt("okhttp.max.idle.connections", 256); - int keepAliveSeconds = DynamicConf.getInt("okhttp.keep.alive.seconds", 3); - int connectTimeoutMillis = DynamicConf.getInt("okhttp.connect.timeout.millis", 60 * 1000); - int readTimeoutMillis = DynamicConf.getInt("okhttp.read.timeout.millis", 60 * 1000); - int writeTimeoutMillis = DynamicConf.getInt("okhttp.write.timeout.millis", 60 * 1000); - int callTimeoutMillis = DynamicConf.getInt("okhttp.call.timeout.millis", 60 * 1000); - this.okHttpClient = new OkHttpClient.Builder() - .connectionPool(new ConnectionPool(maxIdleConnections, keepAliveSeconds, TimeUnit.SECONDS)) - .connectTimeout(connectTimeoutMillis, TimeUnit.MILLISECONDS) - .readTimeout(readTimeoutMillis, TimeUnit.MILLISECONDS) - .writeTimeout(writeTimeoutMillis, TimeUnit.MILLISECONDS) - .callTimeout(callTimeoutMillis, TimeUnit.MILLISECONDS) - .dispatcher(dispatcher) - .build(); - check(); - int intervalSeconds = DynamicConf.getInt("okhttp.health.check.interval.seconds", 5); - scheduler.scheduleAtFixedRate(this::check, intervalSeconds, intervalSeconds, TimeUnit.SECONDS); - } - - private synchronized void check() { - try { - String uri = heartbeatUri.get(); - List validAddrs = new ArrayList<>(); - List addrs = dynamicUpstreamAddrs.getAddrs(); - Long timeout = heartbeatTimeout.get(); - OkHttpClient client = okHttpClient.newBuilder() - .connectTimeout(timeout, TimeUnit.MILLISECONDS) - .readTimeout(timeout, TimeUnit.MILLISECONDS) - .writeTimeout(timeout, TimeUnit.MILLISECONDS) - .callTimeout(timeout, TimeUnit.MILLISECONDS) - .build(); - for (String addr : addrs) { - if (uri == null) { - validAddrs.add(addr); - continue; - } - Request request = new Request.Builder().url(addr + uri).get().build(); - try (Response response = client.newCall(request).execute()) { - String string = response.body().string(); - if (response.isSuccessful()) { - validAddrs.add(addr); - if (logger.isDebugEnabled()) { - logger.debug("heartbeat success, addr = {}, uri = {}, code = {}, response = {}", - addr, uri, response.code(), string); - } - } else { - if (logger.isWarnEnabled()) { - logger.warn("heartbeat fail, addr = {}, uri = {}, code = {}, response = {}", - addr, uri, response.code(), string); - } - } - } catch (Exception e) { - if (logger.isErrorEnabled()) { - logger.error("heartbeat error, addr = {}, uri = {}", addr, uri, e); - } - } - } - this.validAddrs = validAddrs; - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - } - - @Override - public CompletableFuture send(ProxyRequest proxyRequest) { - CompletableFuture future = new CompletableFuture<>(); - try { - if (validAddrs.isEmpty()) { - proxyRequest.getLogBean().setErrorReason(ErrorReason.UPSTREAM_SERVER_SELECT_FAIL); - future.complete(new ProxyResponse(Constants.BAD_GATEWAY, proxyRequest.getLogBean())); - return future; - } - FullHttpRequest request = proxyRequest.getRequest(); - int index = ThreadLocalRandom.current().nextInt(validAddrs.size()); - String upstreamAddr = validAddrs.get(index); - proxyRequest.getLogBean().setUpstreamAddr(upstreamAddr); - String target = upstreamAddr + request.uri(); - Request.Builder builder = new Request.Builder().url(target); - for (Map.Entry header : request.headers()) { - if (header.getKey().equalsIgnoreCase("content-length")) continue; - builder.addHeader(header.getKey(), header.getValue()); - } - HttpMethod method = request.method(); - RequestBody body; - if (method == HttpMethod.GET || method == HttpMethod.HEAD) { - body = null; - } else { - byte[] requestBody = request.content().array(); - body = RequestBody.create(requestBody); - } - builder.method(method.name(), body); - proxyRequest.getLogBean().setUpstreamSendTime(System.currentTimeMillis()); - okHttpClient.newCall(builder.build()).enqueue(new Callback() { - @Override - public void onFailure(@NotNull Call call, @NotNull IOException e) { - proxyRequest.getLogBean().setUpstreamReplyTime(System.currentTimeMillis()); - DefaultFullHttpResponse response; - if (e instanceof ConnectException) { - proxyRequest.getLogBean().setErrorReason(ErrorReason.UPSTREAM_CONNECT_FAIL); - response = new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.BAD_GATEWAY); - } else { - proxyRequest.getLogBean().setErrorReason(ErrorReason.UPSTREAM_TIMEOUT); - response = new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.GATEWAY_TIMEOUT); - } - future.complete(new ProxyResponse(response, proxyRequest.getLogBean())); - LogBean logBean = proxyRequest.getLogBean(); - logger.error("upstream error, host = {}, path = {}, traceId = {}", - logBean.getHost(), logBean.getPath(), logBean.getTraceId(), e); - } - - @Override - public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { - proxyRequest.getLogBean().setUpstreamReplyTime(System.currentTimeMillis()); - Headers headers = response.headers(); - HttpHeaders httpHeaders = new DefaultHttpHeaders(); - for (String name : headers.names()) { - String value = headers.get(name); - httpHeaders.set(name, value); - } - ByteBuf byteBuf = Unpooled.wrappedBuffer(response.body().bytes()); - int code = response.code(); - if (!(code >= 200 && code <= 299)) { - proxyRequest.getLogBean().setErrorReason(ErrorReason.UPSTREAM_NOT_2XX_CODE); - } - DefaultFullHttpResponse rep = new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.valueOf(code), byteBuf, httpHeaders, new DefaultHttpHeaders()); - future.complete(new ProxyResponse(rep, proxyRequest.getLogBean())); - } - }); - return future; - } catch (Exception e) { - proxyRequest.getLogBean().setErrorReason(ErrorReason.UPSTREAM_ERROR); - future.complete(new ProxyResponse(Constants.BAD_GATEWAY, proxyRequest.getLogBean())); - LogBean logBean = proxyRequest.getLogBean(); - logger.error("upstream send error, host = {}, path = {}, traceId = {}", - logBean.getHost(), logBean.getPath(), logBean.getTraceId(), e); - return future; - } - } -} diff --git a/camellia-http-accelerate-proxy/pom.xml b/camellia-http-accelerate-proxy/pom.xml deleted file mode 100644 index c7d5028b5..000000000 --- a/camellia-http-accelerate-proxy/pom.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - 4.0.0 - - camellia-http-accelerate-proxy - 1.2.30-SNAPSHOT - - pom - - - com.netease.nim - camellia - 1.2.30-SNAPSHOT - - - - camellia-http-accelerate-proxy-core - camellia-http-accelerate-proxy-bootstrap - - - diff --git a/pom.xml b/pom.xml index f8430e5f8..fb9edcfd7 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,6 @@ camellia-redis-proxy camellia-id-gen camellia-mq-isolation - camellia-http-accelerate-proxy camellia-delay-queue camellia-tools camellia-zk diff --git a/update-en.md b/update-en.md index ba6f0cf34..fae27b30d 100644 --- a/update-en.md +++ b/update-en.md @@ -9,6 +9,7 @@ * camellia-redis-proxy,plugin, MultiWriteProxyPlugin, rename config key `multi.write.func.className` to `multi.write.func.class.name` * camellia-redis-proxy,kv, optimize lru cache clear on slot change * camellia-redis-proxy,kv, optimize ZSetIndexLRUCache slot calc +* remove camellia-http-accelerate-proxy ### fix * camellia-redis-proxy, cluster, optimize cluster-mode-2 in some corner case diff --git a/update-zh.md b/update-zh.md index 4a56b1100..a84a4dd1d 100644 --- a/update-zh.md +++ b/update-zh.md @@ -9,6 +9,7 @@ * camellia-redis-proxy,plugin模块,MultiWriteProxyPlugin,配置项名称从 `multi.write.func.className` 修改为 `multi.write.func.class.name` * camellia-redis-proxy,kv模块,优化了节点上下线时,lru缓存的清理逻辑 * camellia-redis-proxy,kv模块,优化了ZSetIndexLRUCache的slot计算 +* camellia-http-accelerate-proxy,移除了本模块 ### fix * camellia-redis-proxy, cluster模块,优化并修复了cluster-mode-2的一些边界场景