From 71310a5c877324a4f56fd06fd6ffc18503c0eee0 Mon Sep 17 00:00:00 2001 From: lahm xu Date: Thu, 25 Nov 2021 15:24:58 +0800 Subject: [PATCH] refactor bootstrap: Use the annotation @ConfigurationProperties instead of the @Value in NettyTcpConfig (#2458) * refactor bootstrap: modify @Value to @ConfiguretionProperties in NettyTcpConfig * fix: add setter method --- .../configuration/NettyTcpConfig.java | 146 ++++++++++++++++-- .../ShenyuNettyWebServerFactory.java | 8 +- .../src/main/resources/application-local.yml | 14 ++ .../grpc/configuration/NettyTcpConfig.java | 146 ++++++++++++++++-- .../ShenyuNettyWebServerFactory.java | 8 +- .../src/main/resources/application-local.yml | 14 ++ 6 files changed, 304 insertions(+), 32 deletions(-) diff --git a/shenyu-bootstrap/src/main/java/org/apache/shenyu/bootstrap/configuration/NettyTcpConfig.java b/shenyu-bootstrap/src/main/java/org/apache/shenyu/bootstrap/configuration/NettyTcpConfig.java index c54842ed793c..4c1b3a7c09d4 100644 --- a/shenyu-bootstrap/src/main/java/org/apache/shenyu/bootstrap/configuration/NettyTcpConfig.java +++ b/shenyu-bootstrap/src/main/java/org/apache/shenyu/bootstrap/configuration/NettyTcpConfig.java @@ -17,43 +17,35 @@ package org.apache.shenyu.bootstrap.configuration; -import org.springframework.beans.factory.annotation.Value; - /** * The netty tcp config. */ public class NettyTcpConfig { - @Value("${netty.tcp.select.count:1}") private int selectCount; - @Value("${netty.tcp.worker.count:4}") private int workerCount; - @Value("${netty.tcp.connect_timeout_millis:10000}") private int connectTimeoutMillis; - @Value("${netty.tcp.write_buffer_high_water_mark:65536}") private int writeBufferHighWaterMark; - @Value("${netty.tcp.write_buffer_low_water_mark:32768}") private int writeBufferLowWaterMark; - @Value("${netty.tcp.so_keepalive:false}") + private int writeSpinCount; + + private boolean autoRead; + + private boolean tcpNodelay; + private boolean soKeepalive; - @Value("${netty.tcp.so_reuseaddr:false}") private boolean soReuseaddr; - @Value("${netty.tcp.so_linger:-1}") private int soLinger; - @Value("${netty.tcp.so_backlog:128}") private int soBacklog; - @Value("${netty.tcp.tcp_nodelay:true}") - private boolean tcpNodelay; - /** * get select count. * @@ -143,4 +135,130 @@ public int getSoBacklog() { public boolean isTcpNodelay() { return tcpNodelay; } + + /** + * get writeSpinCount. + * + * @return writeSpinCount + */ + public int getWriteSpinCount() { + return writeSpinCount; + } + + /** + * get autoRead. + * + * @return autoRead + */ + public boolean isAutoRead() { + return autoRead; + } + + /** + * set selectCount. + * + * @param selectCount select count + */ + public void setSelectCount(final int selectCount) { + this.selectCount = selectCount; + } + + /** + * set workerCount. + * + * @param workerCount worker count + */ + public void setWorkerCount(final int workerCount) { + this.workerCount = workerCount; + } + + /** + * set connectTimeoutMillis. + * + * @param connectTimeoutMillis connect timeout millis + */ + public void setConnectTimeoutMillis(final int connectTimeoutMillis) { + this.connectTimeoutMillis = connectTimeoutMillis; + } + + /** + * set writeBufferHighWaterMark. + * + * @param writeBufferHighWaterMark write buffer high water mark + */ + public void setWriteBufferHighWaterMark(final int writeBufferHighWaterMark) { + this.writeBufferHighWaterMark = writeBufferHighWaterMark; + } + + /** + * set writeBufferLowWaterMark. + * + * @param writeBufferLowWaterMark write buffer low water mark + */ + public void setWriteBufferLowWaterMark(final int writeBufferLowWaterMark) { + this.writeBufferLowWaterMark = writeBufferLowWaterMark; + } + + /** + * set writeSpinCount. + * + * @param writeSpinCount write spin count + */ + public void setWriteSpinCount(final int writeSpinCount) { + this.writeSpinCount = writeSpinCount; + } + + /** + * set autoRead. + * + * @param autoRead auto read + */ + public void setAutoRead(final boolean autoRead) { + this.autoRead = autoRead; + } + + /** + * set tcpNodelay. + * + * @param tcpNodelay tcp no delay + */ + public void setTcpNodelay(final boolean tcpNodelay) { + this.tcpNodelay = tcpNodelay; + } + + /** + * set soKeepalive. + * + * @param soKeepalive tcp keepalive + */ + public void setSoKeepalive(final boolean soKeepalive) { + this.soKeepalive = soKeepalive; + } + + /** + * ser setSoReuseaddr. + * + * @param soReuseaddr reuse addr + */ + public void setSoReuseaddr(final boolean soReuseaddr) { + this.soReuseaddr = soReuseaddr; + } + + /** + * set soLinger. + * + * @param soLinger linger + */ + public void setSoLinger(final int soLinger) { + this.soLinger = soLinger; + } + + /** + * set soBacklog. + * + * @param soBacklog tcp backlog + */ + public void setSoBacklog(final int soBacklog) { + this.soBacklog = soBacklog; + } } diff --git a/shenyu-bootstrap/src/main/java/org/apache/shenyu/bootstrap/configuration/ShenyuNettyWebServerFactory.java b/shenyu-bootstrap/src/main/java/org/apache/shenyu/bootstrap/configuration/ShenyuNettyWebServerFactory.java index ebab196d7514..6de39d2ff4dc 100644 --- a/shenyu-bootstrap/src/main/java/org/apache/shenyu/bootstrap/configuration/ShenyuNettyWebServerFactory.java +++ b/shenyu-bootstrap/src/main/java/org/apache/shenyu/bootstrap/configuration/ShenyuNettyWebServerFactory.java @@ -19,6 +19,7 @@ import io.netty.channel.ChannelOption; import io.netty.channel.WriteBufferWaterMark; +import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; import org.springframework.boot.web.embedded.netty.NettyServerCustomizer; import org.springframework.context.annotation.Bean; @@ -51,6 +52,7 @@ public NettyReactiveWebServerFactory nettyReactiveWebServerFactory() { * @return the netty tcp config */ @Bean + @ConfigurationProperties(prefix = "shenyu.netty.tcp") public NettyTcpConfig nettyTcpConfig() { return new NettyTcpConfig(); } @@ -73,10 +75,12 @@ public HttpServer apply(final HttpServer httpServer) { .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, nettyTcpConfig.getConnectTimeoutMillis()) .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(nettyTcpConfig.getWriteBufferLowWaterMark(), nettyTcpConfig.getWriteBufferHighWaterMark())) + .option(ChannelOption.WRITE_SPIN_COUNT, nettyTcpConfig.getWriteSpinCount()) + .option(ChannelOption.AUTO_READ, nettyTcpConfig.isAutoRead()) + .option(ChannelOption.TCP_NODELAY, nettyTcpConfig.isTcpNodelay()) .option(ChannelOption.SO_KEEPALIVE, nettyTcpConfig.isSoKeepalive()) .option(ChannelOption.SO_REUSEADDR, nettyTcpConfig.isSoReuseaddr()) - .option(ChannelOption.SO_LINGER, nettyTcpConfig.getSoLinger()) - .option(ChannelOption.TCP_NODELAY, nettyTcpConfig.isTcpNodelay())); + .option(ChannelOption.SO_LINGER, nettyTcpConfig.getSoLinger())); } } } diff --git a/shenyu-bootstrap/src/main/resources/application-local.yml b/shenyu-bootstrap/src/main/resources/application-local.yml index 00cef3c9c076..443c5358ee42 100644 --- a/shenyu-bootstrap/src/main/resources/application-local.yml +++ b/shenyu-bootstrap/src/main/resources/application-local.yml @@ -47,6 +47,20 @@ management: enabled: false shenyu: + netty: + tcp: + selectCount: 1 + workerCount: 4 + connectTimeoutMillis: 10000 + writeBufferHighWaterMark: 65536 + writeBufferLowWaterMark: 32768 + writeSpinCount: 16 + autoRead: true + tcpNodelay: true + soKeepalive: false + soReuseaddr: false + soLinger: -1 + soBacklog: 128 instance: enabled: false registerType: zookeeper #etcd #consul diff --git a/shenyu-integrated-test/shenyu-integrated-test-grpc/src/main/java/org/apache/shenyu/integrated/test/grpc/configuration/NettyTcpConfig.java b/shenyu-integrated-test/shenyu-integrated-test-grpc/src/main/java/org/apache/shenyu/integrated/test/grpc/configuration/NettyTcpConfig.java index 83bae45dc9b5..a0fa80fcf7cf 100644 --- a/shenyu-integrated-test/shenyu-integrated-test-grpc/src/main/java/org/apache/shenyu/integrated/test/grpc/configuration/NettyTcpConfig.java +++ b/shenyu-integrated-test/shenyu-integrated-test-grpc/src/main/java/org/apache/shenyu/integrated/test/grpc/configuration/NettyTcpConfig.java @@ -17,43 +17,35 @@ package org.apache.shenyu.integrated.test.grpc.configuration; -import org.springframework.beans.factory.annotation.Value; - /** * The netty tcp config. */ public class NettyTcpConfig { - @Value("${netty.tcp.select.count:1}") private int selectCount; - @Value("${netty.tcp.worker.count:4}") private int workerCount; - @Value("${netty.tcp.connect_timeout_millis:10000}") private int connectTimeoutMillis; - @Value("${netty.tcp.write_buffer_high_water_mark:65536}") private int writeBufferHighWaterMark; - @Value("${netty.tcp.write_buffer_low_water_mark:32768}") private int writeBufferLowWaterMark; - @Value("${netty.tcp.so_keepalive:false}") + private int writeSpinCount; + + private boolean autoRead; + + private boolean tcpNodelay; + private boolean soKeepalive; - @Value("${netty.tcp.so_reuseaddr:false}") private boolean soReuseaddr; - @Value("${netty.tcp.so_linger:-1}") private int soLinger; - @Value("${netty.tcp.so_backlog:128}") private int soBacklog; - @Value("${netty.tcp.tcp_nodelay:true}") - private boolean tcpNodelay; - /** * get select count. * @@ -143,4 +135,130 @@ public int getSoBacklog() { public boolean isTcpNodelay() { return tcpNodelay; } + + /** + * get writeSpinCount. + * + * @return writeSpinCount + */ + public int getWriteSpinCount() { + return writeSpinCount; + } + + /** + * get autoRead. + * + * @return autoRead + */ + public boolean isAutoRead() { + return autoRead; + } + + /** + * set selectCount. + * + * @param selectCount select count + */ + public void setSelectCount(final int selectCount) { + this.selectCount = selectCount; + } + + /** + * set workerCount. + * + * @param workerCount worker count + */ + public void setWorkerCount(final int workerCount) { + this.workerCount = workerCount; + } + + /** + * set connectTimeoutMillis. + * + * @param connectTimeoutMillis connect timeout millis + */ + public void setConnectTimeoutMillis(final int connectTimeoutMillis) { + this.connectTimeoutMillis = connectTimeoutMillis; + } + + /** + * set writeBufferHighWaterMark. + * + * @param writeBufferHighWaterMark write buffer high water mark + */ + public void setWriteBufferHighWaterMark(final int writeBufferHighWaterMark) { + this.writeBufferHighWaterMark = writeBufferHighWaterMark; + } + + /** + * set writeBufferLowWaterMark. + * + * @param writeBufferLowWaterMark write buffer low water mark + */ + public void setWriteBufferLowWaterMark(final int writeBufferLowWaterMark) { + this.writeBufferLowWaterMark = writeBufferLowWaterMark; + } + + /** + * set writeSpinCount. + * + * @param writeSpinCount write spin count + */ + public void setWriteSpinCount(final int writeSpinCount) { + this.writeSpinCount = writeSpinCount; + } + + /** + * set autoRead. + * + * @param autoRead auto read + */ + public void setAutoRead(final boolean autoRead) { + this.autoRead = autoRead; + } + + /** + * set tcpNodelay. + * + * @param tcpNodelay tcp no delay + */ + public void setTcpNodelay(final boolean tcpNodelay) { + this.tcpNodelay = tcpNodelay; + } + + /** + * set soKeepalive. + * + * @param soKeepalive tcp keepalive + */ + public void setSoKeepalive(final boolean soKeepalive) { + this.soKeepalive = soKeepalive; + } + + /** + * ser setSoReuseaddr. + * + * @param soReuseaddr reuse addr + */ + public void setSoReuseaddr(final boolean soReuseaddr) { + this.soReuseaddr = soReuseaddr; + } + + /** + * set soLinger. + * + * @param soLinger linger + */ + public void setSoLinger(final int soLinger) { + this.soLinger = soLinger; + } + + /** + * set soBacklog. + * + * @param soBacklog tcp backlog + */ + public void setSoBacklog(final int soBacklog) { + this.soBacklog = soBacklog; + } } diff --git a/shenyu-integrated-test/shenyu-integrated-test-grpc/src/main/java/org/apache/shenyu/integrated/test/grpc/configuration/ShenyuNettyWebServerFactory.java b/shenyu-integrated-test/shenyu-integrated-test-grpc/src/main/java/org/apache/shenyu/integrated/test/grpc/configuration/ShenyuNettyWebServerFactory.java index a1bba805de85..69975e884c11 100644 --- a/shenyu-integrated-test/shenyu-integrated-test-grpc/src/main/java/org/apache/shenyu/integrated/test/grpc/configuration/ShenyuNettyWebServerFactory.java +++ b/shenyu-integrated-test/shenyu-integrated-test-grpc/src/main/java/org/apache/shenyu/integrated/test/grpc/configuration/ShenyuNettyWebServerFactory.java @@ -19,6 +19,7 @@ import io.netty.channel.ChannelOption; import io.netty.channel.WriteBufferWaterMark; +import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; import org.springframework.boot.web.embedded.netty.NettyServerCustomizer; import org.springframework.context.annotation.Bean; @@ -51,6 +52,7 @@ public NettyReactiveWebServerFactory nettyReactiveWebServerFactory() { * @return the netty tcp config */ @Bean + @ConfigurationProperties(prefix = "shenyu.netty.tcp") public NettyTcpConfig nettyTcpConfig() { return new NettyTcpConfig(); } @@ -73,10 +75,12 @@ public HttpServer apply(final HttpServer httpServer) { .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, nettyTcpConfig.getConnectTimeoutMillis()) .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(nettyTcpConfig.getWriteBufferLowWaterMark(), nettyTcpConfig.getWriteBufferHighWaterMark())) + .option(ChannelOption.WRITE_SPIN_COUNT, nettyTcpConfig.getWriteSpinCount()) + .option(ChannelOption.AUTO_READ, nettyTcpConfig.isAutoRead()) + .option(ChannelOption.TCP_NODELAY, nettyTcpConfig.isTcpNodelay()) .option(ChannelOption.SO_KEEPALIVE, nettyTcpConfig.isSoKeepalive()) .option(ChannelOption.SO_REUSEADDR, nettyTcpConfig.isSoReuseaddr()) - .option(ChannelOption.SO_LINGER, nettyTcpConfig.getSoLinger()) - .option(ChannelOption.TCP_NODELAY, nettyTcpConfig.isTcpNodelay())); + .option(ChannelOption.SO_LINGER, nettyTcpConfig.getSoLinger())); } } } diff --git a/shenyu-integrated-test/shenyu-integrated-test-grpc/src/main/resources/application-local.yml b/shenyu-integrated-test/shenyu-integrated-test-grpc/src/main/resources/application-local.yml index ebc493af39fe..a628cc6d0aff 100644 --- a/shenyu-integrated-test/shenyu-integrated-test-grpc/src/main/resources/application-local.yml +++ b/shenyu-integrated-test/shenyu-integrated-test-grpc/src/main/resources/application-local.yml @@ -29,6 +29,20 @@ management: enabled: false shenyu: + netty: + tcp: + selectCount: 1 + workerCount: 4 + connectTimeoutMillis: 10000 + writeBufferHighWaterMark: 65536 + writeBufferLowWaterMark: 32768 + writeSpinCount: 16 + autoRead: true + tcpNodelay: true + soKeepalive: false + soReuseaddr: false + soLinger: -1 + soBacklog: 128 switchConfig: local: true cross: