From 219358725eafcfac2728f8fd681e16a3ef90e6c3 Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Wed, 19 Jul 2023 22:17:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=202.1.0=201.=E5=AE=A2=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8C=87=E5=AE=9AIP=E5=92=8C=E7=AB=AF?= =?UTF-8?q?=E5=8F=A3=202.=E6=94=AF=E6=8C=81yaml=E3=80=81JVM=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=92=8C=E9=BB=98=E8=AE=A4=E9=85=8D=E7=BD=AE=E5=A4=9A?= =?UTF-8?q?=E7=A7=8D=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/client/netty/NettyChannel.java | 31 ++++++++++++++++--- .../core/config/EasyRetryProperties.java | 11 +++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/client/netty/NettyChannel.java b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/client/netty/NettyChannel.java index 5ecca028d..1a2579222 100644 --- a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/client/netty/NettyChannel.java +++ b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/client/netty/NettyChannel.java @@ -16,6 +16,7 @@ import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpVersion; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; import org.springframework.boot.autoconfigure.web.ServerProperties; import java.nio.charset.StandardCharsets; @@ -30,8 +31,17 @@ import java.util.Optional; @Slf4j public class NettyChannel { - private static final String HOST_ID = IdUtil.simpleUUID(); - private static final String HOST_IP = HostUtils.getIp(); + private NettyChannel() { + } + + private static final String HOST_ID = IdUtil.getSnowflake().nextIdStr(); + private static final int PORT; + private static final String HOST; + + static { + PORT = Integer.parseInt(System.getProperty("easy-retry.port", String.valueOf(8080))); + HOST = System.getProperty("easy-retry.host", HostUtils.getIp()); + } private static Channel CHANNEL; @@ -59,6 +69,19 @@ public class NettyChannel { HttpVersion.HTTP_1_1, method, url, Unpooled.wrappedBuffer(body.getBytes(StandardCharsets.UTF_8))); ServerProperties serverProperties = SpringContext.CONTEXT.getBean(ServerProperties.class); + EasyRetryProperties easyRetryProperties = SpringContext.CONTEXT.getBean(EasyRetryProperties.class); + + Integer port = easyRetryProperties.getPort(); + // 获取客户端指定的端口 + if (Objects.isNull(port)) { + port = Optional.ofNullable(serverProperties.getPort()).orElse(PORT); + } + + String host = easyRetryProperties.getHost(); + // 获取客户端指定的IP地址 + if (StringUtils.isBlank(host)) { + host = HOST; + } request.headers() .set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON) @@ -67,10 +90,10 @@ public class NettyChannel { //设置传递请求内容的长度 .set(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes()) .set(HeadersEnum.HOST_ID.getKey(), HOST_ID) - .set(HeadersEnum.HOST_IP.getKey(), HOST_IP) + .set(HeadersEnum.HOST_IP.getKey(), host) .set(HeadersEnum.GROUP_NAME.getKey(), EasyRetryProperties.getGroup()) .set(HeadersEnum.CONTEXT_PATH.getKey(), Optional.ofNullable(serverProperties.getServlet().getContextPath()).orElse("/")) - .set(HeadersEnum.HOST_PORT.getKey(), Optional.ofNullable(serverProperties.getPort()).orElse(8080)) + .set(HeadersEnum.HOST_PORT.getKey(), port) .set(HeadersEnum.VERSION.getKey(), GroupVersionCache.getVersion()) ; diff --git a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/config/EasyRetryProperties.java b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/config/EasyRetryProperties.java index 81dc6d539..69d3661d2 100644 --- a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/config/EasyRetryProperties.java +++ b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/config/EasyRetryProperties.java @@ -1,6 +1,7 @@ package com.aizuda.easy.retry.client.core.config; import com.aizuda.easy.retry.common.core.context.SpringContext; +import com.aizuda.easy.retry.common.core.util.HostUtils; import lombok.Data; import lombok.Getter; import lombok.Setter; @@ -24,6 +25,16 @@ public class EasyRetryProperties { */ private String group; + /** + * 指定客户端IP,默认取本地IP + */ + private String host; + + /** + * 指定客户端端口 + */ + private Integer port; + /** * 服务端配置 */