Skip to content

Commit

Permalink
refactor bootstrap: Use the annotation @ConfigurationProperties inste…
Browse files Browse the repository at this point in the history
…ad of the @value in NettyTcpConfig (#2458)

* refactor bootstrap: modify @value to @ConfiguretionProperties in NettyTcpConfig

* fix: add setter method
  • Loading branch information
lahmXu authored Nov 25, 2021
1 parent 31304d8 commit 71310a5
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,6 +52,7 @@ public NettyReactiveWebServerFactory nettyReactiveWebServerFactory() {
* @return the netty tcp config
*/
@Bean
@ConfigurationProperties(prefix = "shenyu.netty.tcp")
public NettyTcpConfig nettyTcpConfig() {
return new NettyTcpConfig();
}
Expand All @@ -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()));
}
}
}
14 changes: 14 additions & 0 deletions shenyu-bootstrap/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 71310a5

Please # to comment.