Skip to main content
版本:2.4.3

网关属性配置

本篇主要讲解如何在 Apache Shenyu 网关配置 ShenYu 的相关属性。

属性配置

shenyu:
netty:
tcp:
webServerFactoryEnabled: true
selectCount: 1
workerCount: 4
serverSocketChannel:
soRcvBuf: 87380
soBackLog: 128
soReuseAddr: true
connectTimeoutMillis: 30000
writeBufferHighWaterMark: 65536
writeBufferLowWaterMark: 32768
writeSpinCount: 16
autoRead: false
allocType: "pooled"
socketChannel:
soKeepAlive: false
soReuseAddr: false
soLinger: -1
tcpNoDelay: true
soRcvBuf: 87380
soSndBuf: 16384
ipTos: 0
allowHalfClosure: false
connectTimeoutMillis: 30000
writeBufferHighWaterMark: 65536
writeBufferLowWaterMark: 32768
writeSpinCount: 16
autoRead: false
allocType: "pooled"
instance:
enabled: false
registerType: zookeeper #etcd #consul
serverLists: localhost:2181 #http://localhost:2379 #localhost:8848
props:
# httpclient:
# strategy: webClient
# connectTimeout: 45000
# responseTimeout: 3000
# readerIdleTime: 3000
# writerIdleTime: 3000
# allIdleTime: 3000
# readTimeout: 3000
# writeTimeout: 3000
# wiretap: false
# keepAlive: false
# pool:
# type: ELASTIC
# name: proxy
# maxConnections: 16
# acquireTimeout: 45000
# maxIdleTime: 3000
# proxy:
# host:
# port:
# username:
# password:
# nonProxyHostsPattern:
# ssl:
# useInsecureTrustManager: false
# keyStoreType: PKCS12
# keyStorePath: classpath:keystore.p12
# keyStorePassword: 123456
# keyStoreProvider:
# keyPassword: 123456
# trustedX509Certificates:
# handshakeTimeout:
# closeNotifyFlushTimeout:
# closeNotifyReadTimeout:
# defaultConfigurationType:
sync:
websocket:
urls: ws://localhost:9095/websocket
allowOrigin: ws://localhost:9195
# zookeeper:
# url: localhost:2181
# sessionTimeout: 5000
# connectionTimeout: 2000
# http:
# url: http://localhost:9095
# nacos:
# url: localhost:8848
# namespace: 1c10d748-af86-43b9-8265-75f487d20c6c
# username:
# password:
# acm:
# enabled: false
# endpoint: acm.aliyun.com
# namespace:
# accessKey:
# secretKey:
# etcd:
# url: http://localhost:2379
# consul:
# url: http://localhost:8500
# waitTime: 1000
# watchDelay: 1000
cross:
enabled: true
allowedHeaders:
allowedMethods: "*"
allowedOrigin: "*"
allowedExpose: "*"
maxAge: "18000"
allowCredentials: true
switchConfig:
local: true
file:
enabled: true
maxSize : 10
exclude:
enabled: false
paths:
- /favicon.ico
extPlugin:
path:
enabled: true
threads: 1
scheduleTime: 300
scheduleDelay: 30
scheduler:
enabled: false
type: fixed
threads: 16
upstreamCheck:
enabled: false
timeout: 3000
healthyThreshold: 1
unhealthyThreshold: 1
interval: 5000
printEnabled: true
printInterval: 60000

属性详解

shenyu.NettyTcpProperties 配置

ShenYu reactor-netty 配置

NameTypeDefaultRequiredDescription
webServerFactoryEnabledBooleantrueNo是否开启自定义参数,true-开启,false-可以自行配置NettyReactiveWebServerFactory
selectCountInteger1NoNetty 选择器数
workerCountInteger4NoNetty 工作线程数
ServerSocketChannelConfig
soRcvBufInteger--NoSocket参数,TCP数据接收缓冲区大小,默认由系统决定
soBackLogInteger128NoSocket参数,服务端接受连接的队列长度
soReuseAddrBooleantrueNoSocket 参数,是否复用地址,reactor-netty中默认值为true
connectTimeoutMillisInteger30000NoNetty 参数,连接超时时间
writeBufferHighWaterMarkInteger65536NoNetty 参数,通道水位线上限
writeBufferLowWaterMarkInteger32768NoNetty 参数,通道水位线下限
writeSpinCountInteger16NoNetty参数,一个Loop写操作执行的最大次数
autoReadBooleanfalseNoNetty参数,自动读取,reactor-netty中默认值为false,且只能为false
allocTypeStringpooledNoNetty参数,ByteBuf的分配器
SocketChannelConfig
soKeepAliveBooleanfalseNoSocket 参数,是否启用心跳保活机制
soReuseAddrBooleantrueNoSocket 参数,是否复用地址,reactor-netty中默认值为true
soLingerInteger-1NoSocket 参数,关闭 Socket 的延迟时间
tcpNoDelayBooleantrueNoSocket 参数,是否启用 Nagle 算法
soRcvBufInteger--NoSocket参数,TCP数据接收缓冲区大小,默认由系统决定
soSndBufInteger--NoSocket参数,TCP数据发送缓冲区大小,默认由系统决定
ipTosInteger0NoIP参数,设置IP头部的Type-of-Service字段,用于描述IP包的优先级和QoS选项
allowHalfClosureBooleanfalseNoNetty参数,一个连接的远端关闭时本地端是否关闭
connectTimeoutMillisInteger30000NoNetty 参数,连接超时时间
writeBufferHighWaterMarkInteger65536NoNetty 参数,通道水位线上限
writeBufferLowWaterMarkInteger32768NoNetty 参数,通道水位线下限
writeSpinCountInteger16NoNetty参数,一个Loop写操作执行的最大次数
autoReadBooleanfalseNoNetty参数,自动读取,reactor-netty中默认值为false,且只能为false
allocTypeStringpooledNoNetty参数,ByteBuf的分配器
shenyu.instance 配置

这是 ShenYu 网关注册到注册中心的相关配置,注册中心配置请参考 注册中心配置

名称类型默认值是否必填说明
enabledbooleanfalse是否启动
registerTypeStringzookeeper使用哪个注册中心,目前支持zookeeper、etcd
serverListsStringlocalhost:2181注册中心的地址。若使用集群,用 , 分隔
props使用不同注册类型时,属性取值不同。
  • props配置

使用不同的注册中心时,属性取值不同。

当注册类型为 zookeeper 时,支持的属性配置如下:

名称类型默认值是否必填说明
sessionTimeoutint30000session超时时间(毫秒)
connectionTimeoutint3000连接超时时间(毫秒)

当注册类型为 etcd 时,支持的属性配置如下:

名称类型默认值是否必填说明
etcdTimeoutint30000etcd超时时间(毫秒)
etcdTTLint5租约生存时间(秒)
shenyu.httpclient 配置

这是 ShenYu 网关中代理Http及SpringCloud协议后,用于发送代理请求的HttpClient配置。

NameTypeDefaultRequiredDescription
strategyStringwebClientNoHttpClientPlugin实现策略(默认使用webClient):
- webClient:使用WebClientPlugin
- netty:使用NettyHttpClientPlugin
connectTimeoutint45000No连接超时时间 (毫秒),默认值为 45000
responseTimeoutint3000No结果超时时间 (毫秒),默认值为 3000
readerIdleTimeint3000No指定读空闲超时时间 (毫秒),默认值为 3000
writerIdleTimeint3000No指定写空闲超时时间 (毫秒),默认值为 3000
allIdleTimeint3000No指定读和写超时时间 (毫秒),默认值为 3000
readTimeoutint3000No读取超时 (毫秒),默认值为 3000
writeTimeoutint3000No输出超时 (millisecond),默认值为 3000
wiretapBooleanfalseNo启用 Netty HttpClient 的窃听调试,默认值为 false
keepAliveBooleanfalseNo启用或禁用请求的 Keep-Alive 支持,默认值为 false
poolHttpClient 连接池配置
proxyHttpClient 代理配置
sslHttpClient SSL配置
  • pool config

HttpClient连接池配置:

NameTypeDefaultRequiredDescription
typeStringELASTICNoHttpClient连接池类型,默认值为ELASTIC
- ELASTIC: 连接池可以按需缓存和增长。
- FIXED: 连接池缓存并重用,有固定的最大连接数。
- DISABLED: 连接池总是会创建一个新的连接。
nameStringproxyNo连接池映射名称,默认为proxy
maxConnectionsint2*可用处理器数,最小值为16No仅适用于 FIXED 类型,在现有连接上开始挂起获取之前的最大连接数。
默认值为可用处理器数*2。
(最小值为 16)
acquireTimeoutint45000No仅适用于 FIXED 类型,等待获取连接的最长时间(毫秒)。默认值为 45000
maxIdleTimeintNULLNoChannel 空闲多久关闭,如果为空,没有最大空闲关闭时间。
  • Proxy config

Netty HttpClient 代理的相关配置:

NameTypeDefaultRequiredDescription
hostStringnullNoNetty HttpClient 代理配置的主机名。
portStringnullNoNetty HttpClient 的代理配置端口。
usernameStringnullNoNetty HttpClient 代理配置的用户名。
passwordStringnullNoNetty HttpClient 代理配置的密码。
nonProxyHostsPatternStringnullNo直连的主机列表的正则表达式 (Java)。
  • SSL config

网关路由可以同时支持路由到http和https的后端服务,以下为SSL相关配置:

NameTypeDefaultRequiredDescription
useInsecureTrustManagerBooleanfalseNo是否信任所有下游证书,默认false
keyStoreTypeStringPKCS12NoSSL 证书类型。
keyStorePathStringNoSSL 证书路径,可以放在 classpath 下。
keyStorePasswordStringNoSSL 证书密码。
keyStoreProviderStringNoSSL 证书提供者。
keyPasswordStringNoSSL 证书Key的密码。
trustedX509CertificatesArrayNullNo配置自己的信任的证书列表。
handshakeTimeoutint10000NoSSL握手超时时间(毫秒),默认值为 10000
closeNotifyFlushTimeoutint3000NoSSL close_notify 刷新超时(毫秒)默认值为 3000
closeNotifyReadTimeoutint0NoSSL close_notify 读取超时(毫秒)默认值为 0
defaultConfigurationTypeStringTCPNoSslContextBuilder 的默认配置, 默认为 TCP.
- H2: SslProvider 将根据 OpenSsl.isAlpnSupported()、SslProvider.HTTP2_CIPHERS、ALPN 支持、HTTP/1.1 和 HTTP/2 支持进行设置
- TCP: SslProvider 将根据 OpenSsl.isAvailable() 设置
- NONE: 不会有默认配置
过滤器相关配置
  • shenyu.file 配置

文件过滤器的相关配置。

名称类型默认值是否必填说明
enabledBooleanfalse是否开启文件大小过滤
maxSizeInteger10No上传文件最大值(单位:MB
  • shenyu.cross 配置

跨域相关配置。

名称类型默认值是否必填说明
enabledBooleanfalse是否支持跨域请求
allowedHeadersStringx-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN, token, username, clientNo允许的Header头,多个请用 "," 分割。新的"allowedHeaders"会在默认值基础上,去除重复的追加到"Access-Control-Allow-Headers"。
allowedMethodsString"*"No允许的方法
allowedOriginString"*"No允许的Origin,当取值为""时,使用request.getHeaders().getOrigin()设置Access-Control-Allow-Origin
allowedExposeString"*"No允许的Expose
maxAgeString"18000"No最大年龄 (ms)
allowCredentialsBooleantrueNo允许认证
  • shenyu.exclude 配置

拒绝指定请求经过网关的相关配置

名称类型默认值是否必填说明
enabledBooleanfalse是否拒绝指定请求经过网关
pathsArray匹配该列表的请求不经过网关(支持路径匹配)
shenyu.sync 配置

网关和Admin端使用数据同步的相关配置。

使用websocket进行数据同步的属性配置如下:

名称类型默认值是否必填说明
urlsStringAdmin的websocket服务地址,多个地址用 , 分开 。

使用zookeeper进行数据同步的属性配置如下:

名称类型默认值是否必填说明
urlStringzookeeper的连接地址
sessionTimeoutintsession的超时时间(毫秒)
connectionTimeoutint连接超时时间(毫秒)

使用http长轮询进行数据同步的属性配置如下:

名称类型默认值是否必填说明
urlStringAdmin的服务地址

使用nacos进行数据同步的属性配置如下:

名称类型默认值是否必填说明
urlStringnacos连接地址
namespaceString命名空间
usernameString用户名
passwordString密码
acm阿里云ACM服务配置
  • acm配置
名称类型默认值是否必填说明
enabledbooleanfalse是否启用
endpointStringACM服务地址
namespaceStringnamespace
accessKeyStringaccessKey
secretKeyStringsecretKey

使用etcd进行数据同步的属性配置如下:

名称类型默认值是否必填说明
urlStringetcd连接地址

使用consul进行数据同步的属性配置如下:

名称类型默认值是否必填说明
urlStringconsul连接地址
waitTimeint请求consul服务拉取配置信息的超时时间(毫秒)
watchDelayint同步间隔(毫秒)
shenyu.extPlugin config

Apache ShenYu对于动态加载自定义插件的配置

NameTypeDefaultRequiredDescription
enabledBooleantrueNo是否开启动态加载自定义插件,默认开启
pathStringFalse动态加载自定义插件的路径,如果没配,默认为相对于当前网关路径下的 :/ext/lib,用户也可以使用-Dplugin-ext指定
threadsInteger1False动态加载自定义插件的线程数
scheduleTimeInteger300False动态加载自定义插件的间隔时间 ,单元:秒
scheduleDelayInteger30False网关启动多久后去动态加载,单元:秒
shenyu.scheduler config

Apache ShenYu 调度线程模型配置

NameTypeDefaultRequiredDescription
enabledBooleanfalseNo是否开启使用调度线程
typeStringfixedFalse调度线程池类型,默认为fixed,不配置或者其他则为弹性线程池
threadsIntegerMath.max((Runtime.getRuntime().availableProcessors() << 1) + 1, 16)False固定线程池类型时候的线程数量
shenyu.upstreamCheck config

Apache ShenYu动态检测upstream的配置

NameTypeDefaultRequiredDescription
enabledBooleanfalseNo是否检测
timeoutInteger3000False超时配置 (ms)
healthyThresholdInteger1False健康因子
unhealthyThresholdInteger1False不健康因子
intervalInteger5000False检测的调度间隔时间
printEnabledBooleantrueNo是否打印日志
printIntervalInteger60000False打印日志的间隔调度时间
shenyu.switchConfig config

Apache ShenYu开关配置

NameTypeDefaultRequiredDescription
localBooleantrueNo是否开启本地模式,如果开启,本地操作数据,默认开启