Skip to main content
版本:2.7.0.3

网关属性配置

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

属性配置

shenyu:
selectorMatchCache:
## selector L1 cache
cache:
enabled: false
initialCapacity: 10000 # initial capacity in cache
maximumSize: 10000 # max size in cache
## selector L2 cache, use trie as L2 cache
trie:
enabled: false
cacheSize: 128 # the number of plug-ins
matchMode: antPathMatch
ruleMatchCache:
## rule L1 cache
cache:
enabled: true
initialCapacity: 10000 # initial capacity in cache
maximumSize: 65536 # max size in cache
## rule L2 cache, use trie as L2 cache
trie:
enabled: false
cacheSize: 1024 # the number of selectors
matchMode: antPathMatch
netty:
http:
webServerFactoryEnabled: true
selectCount: 1
workerCount: 4
accessLog: false
serverSocketChannel:
soRcvBuf: 87380
soBackLog: 128
soReuseAddr: true
connectTimeoutMillis: 30000
writeBufferHighWaterMark: 65536
writeBufferLowWaterMark: 32768
writeSpinCount: 16
autoRead: false
allocType: "pooled"
messageSizeEstimator: 8
singleEventExecutorPerGroup: true
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"
messageSizeEstimator: 8
singleEventExecutorPerGroup: true
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: "*"
allowedAnyOrigin: false
allowedOrigin:
# format : schema://prefix spacer domain
# Access-Control-Allow-Origin: "http://a.apache.org,http://b.apache.org"
spacer: "."
domain: apache.org
prefixes:
- a # a.apache.org
- b # b.apache.org
origins:
- c.apache.org
- d.apache.org
- http://e.apache.org
originRegex: ^http(|s)://(.*\.|)abc.com$
allowedExpose: ""
maxAge: "18000"
allowCredentials: true
switchConfig:
local: true
file:
enabled: true
maxSize : 10
exclude:
enabled: false
paths:
- /favicon.ico
fallback:
enabled: false
paths:
- /fallback/hystrix
- /fallback/resilience4j
health:
enabled: false
paths:
- /actuator/health
- /health_check
alert:
enabled: true
admins: localhost:9095
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
ribbon:
serverListRefreshInterval: 10000
metrics:
enabled: false
name : prometheus
host: 127.0.0.1
port: 8090
jmxConfig:
props:
jvm_enabled: true
sharedPool:
enable: true
prefix: "shenyu-shared"
corePoolSize: 200
maximumPoolSize: 2000
keepAliveTime: 60000
maxWorkQueueMemory: 1073741824 # 1GB
maxFreeMemory: 268435456 # 256MB

属性详解

shenyu.matchCache 配置
  • 选择器匹配缓存配置
字段类型默认值是否必填说明
enabledBooleanfalseNo是否开启选择器缓存
initialCapacityInteger10000No选择器缓存初始化容量
maximumSizeInteger10000No选择器缓存最大数量
  • 选择器前缀树缓存配置
字段类型默认值是否必填说明
enabledBooleanfalseNo是否开启选择器前缀树缓存
cacheSizeInteger512No前缀树缓存大小
matchModeStringantPathMatchYesshenyu路径匹配模式,shenyu支持两种匹配模式: antPathMatch and pathPattern
  • 规则匹配缓存配置
字段类型默认值是否必填说明
enabledBooleanfalseNo是否开启选择器缓存
initialCapacityInteger10000No规则缓存初始化容量
maximumSizeInteger10000No规则缓存最大数量
  • 规则前缀树缓存配置
字段类型默认值是否必填说明
enabledBooleanfalseNo是否开启规则前缀树缓存
cacheSizeInteger512No前缀树缓存大小
matchModeStringantPathMatchYesshenyu路径匹配模式,shenyu支持两种匹配模式: antPathMatch and pathPattern

shenyu默认开启L1和L2缓存, shenyu前缀树支持两种匹配模式,我们非常建议您使用pathPattern作为默认的匹配模式。

pathPattern: org.springframework.web.util.pattern.PathPatternParser antPathMatch: org.springframework.util.AntPathMatcher

当您将matchRestful标记为true时,我们建议将所有缓存标记为false,以避免匹配冲突。

shenyu.NettyTcpProperties 配置

Apache ShenYu reactor-netty 配置

名称类型默认值是否必填说明
webServerFactoryEnabledBooleantrue是否开启自定义参数,true-开启,false-可以自行配置NettyReactiveWebServerFactory
selectCountInteger1Netty 选择器数
workerCountInteger4Netty 工作线程数
accessLogBooleanfalsenetty request parameters.
ServerSocketChannelConfig
soRcvBufInteger--Socket参数,TCP数据接收缓冲区大小,默认由系统决定
soBackLogInteger128Socket参数,服务端接受连接的队列长度
soReuseAddrBooleantrueSocket 参数,是否复用地址,reactor-netty中默认值为true
connectTimeoutMillisInteger30000Netty 参数,连接超时时间
writeBufferHighWaterMarkInteger65536Netty 参数,通道水位线上限
writeBufferLowWaterMarkInteger32768Netty 参数,通道水位线下限
writeSpinCountInteger16Netty参数,一个Loop写操作执行的最大次数
autoReadBooleanfalseNetty参数,自动读取,reactor-netty中默认值为false,且只能为false
allocTypeStringpooledNetty参数,ByteBuf的分配器
messageSizeEstimatorInteger8Netty参数, 消息大小估算器, 用于估算ByteBuf,ByteBufHolder和FileRegion大小
singleEventExecutorPerGroupBooleantrueNetty参数, 单线程执行ChannelPipeline中的事件
SocketChannelConfig
soKeepAliveBooleanfalseSocket 参数,是否启用心跳保活机制
soReuseAddrBooleantrueSocket 参数,是否复用地址,reactor-netty中默认值为true
soLingerInteger-1Socket 参数,关闭 Socket 的延迟时间
tcpNoDelayBooleantrueSocket 参数,是否启用 Nagle 算法
soRcvBufInteger--Socket参数,TCP数据接收缓冲区大小,默认由系统决定
soSndBufInteger--Socket参数,TCP数据发送缓冲区大小,默认由系统决定
ipTosInteger0IP参数,设置IP头部的Type-of-Service字段,用于描述IP包的优先级和QoS选项
allowHalfClosureBooleanfalseNetty参数,一个连接的远端关闭时本地端是否关闭
connectTimeoutMillisInteger30000Netty 参数,连接超时时间
writeBufferHighWaterMarkInteger65536Netty 参数,通道水位线上限
writeBufferLowWaterMarkInteger32768Netty 参数,通道水位线下限
writeSpinCountInteger16Netty参数,一个Loop写操作执行的最大次数
autoReadBooleanfalseNetty参数,自动读取,reactor-netty中默认值为false,且只能为false
allocTypeStringpooledNetty参数,ByteBuf的分配器
messageSizeEstimatorInteger8Netty参数, 消息大小估算器, 用于估算ByteBuf,ByteBufHolder和FileRegion大小
singleEventExecutorPerGroupBooleantrueNetty参数, 单线程执行ChannelPipeline中的事件
shenyu.register 配置

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

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

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

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

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

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

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

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

名称类型默认值是否必填说明
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连接池配置

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

Netty HttpClient 代理的相关配置

名称类型默认值是否必填说明
hostStringNullNoNetty HttpClient 代理配置的主机名。
portStringNullNoNetty HttpClient 的代理配置端口。
usernameStringNullNoNetty HttpClient 代理配置的用户名。
passwordStringNullNoNetty HttpClient 代理配置的密码。
nonProxyHostsPatternStringNullNo直连的主机列表的正则表达式 (Java)。
  • SSL 配置

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

名称类型默认值是否必填说明
useInsecureTrustManagerBooleanfalse是否信任所有下游证书,默认false
keyStoreTypeStringPKCS12SSL 证书类型。
keyStorePathStringSSL 证书路径,可以放在 classpath 下。
keyStorePasswordStringSSL 证书密码。
keyStoreProviderStringSSL 证书提供者。
keyPasswordStringSSL 证书Key的密码。
trustedX509CertificatesArrayNull配置自己的信任的证书列表。
handshakeTimeoutint10000SSL握手超时时间(毫秒),默认值为 10000
closeNotifyFlushTimeoutint3000SSL close_notify 刷新超时(毫秒)默认值为 3000
closeNotifyReadTimeoutint0SSL close_notify 读取超时(毫秒)默认值为 0
defaultConfigurationTypeStringTCPSslContextBuilder 的默认配置, 默认为 TCP.
- H2: SslProvider 将根据 OpenSsl.isAlpnSupported()、SslProvider.HTTP2_CIPHERS、ALPN 支持、HTTP/1.1 和 HTTP/2 支持进行设置
- TCP: SslProvider 将根据 OpenSsl.isAvailable() 设置
- NONE: 不会有默认配置
过滤器相关配置
  • shenyu.file 配置

文件过滤器的相关配置

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

跨域相关配置

名称类型默认值是否必填说明
enabledBooleanfalse是否支持跨域请求
allowedHeadersStringx-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN, token, username, client允许的Header头,多个请用 "," 分割。新的"allowedHeaders"会在默认值基础上,去除重复的追加到"Access-Control-Allow-Headers"。
allowedMethodsString"*"允许的方法
allowedAnyOriginBooleanfalse是否允许任意Origin,为true时直接将Access-Control-Allow-Origin设置值与Origin相同,即request.getHeaders().getOrigin(),同时丢弃allowedOrigin配置
allowedOriginAllowedOriginConfig-设置允许的请求来源
spacerString"."设置允许访问的子域名,需要搭配 domain、prefixes 一起使用
domainString设置允许访问的子域名,需要搭配 spacer、prefixes 一起使用
prefixesSet设置允许访问的子域名,需要搭配 spacer、domain 一起使用
originsSetnull设置允许访问的域名,可单独使用
originRegexString设置允许正则匹配的域名访问,可单独使用
allowedExposeString""允许的Expose
maxAgeString"18000"最大年龄 (ms)
allowCredentialsBooleantrue允许认证
  • shenyu.exclude 配置

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

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

熔断响应的相关配置

名称类型默认值是否必填说明
enabledBooleanfalse是否开启熔断响应
pathsArray服务熔断请求的地址
  • shenyu.health 配置

服务健康状态的相关配置

名称类型默认值是否必填说明
enabledBooleanfalse是否开启健康检测
pathsArray"/actuator/health""/health_check"设置服务健康监测路径
  • shenyu.local 配置

本地转发相关配置

名称类型默认值是否必填说明
enabledBooleanfalse是否开启本地转发
sha512KeyString秘钥,根据秘钥判断是否需要本地转发
shenyu.switchConfig 配置

ShenYu开关配置

名称类型默认值是否必填说明
localBooleantrue是否开启本地模式,如果开启,本地操作数据,默认开启
shenyu.sync 配置

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

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

名称类型默认值是否必填说明
urlsStringAdmin的websocket服务地址,多个地址用 , 分开 。
allowOriginString设置允许的 origins, 多个参数以;分隔

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

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

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

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

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

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

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

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

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

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

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

名称类型默认值是否必填说明
enabledBooleantrue是否开启动态加载自定义插件,默认开启
pathString动态加载自定义插件的路径,如果没配,默认为相对于当前网关路径下的 :/ext/lib,用户也可以使用-Dplugin-ext指定
threadsInteger1动态加载自定义插件的线程数
scheduleTimeInteger300动态加载自定义插件的间隔时间 ,单元:秒
scheduleDelayInteger30网关启动多久后去动态加载,单元:秒
shenyu.scheduler 配置

Apache ShenYu 调度线程模型配置

名称类型默认值是否必填说明
enabledBooleanfalse是否开启使用调度线程
typeStringfixed调度线程池类型,默认为fixed,不配置或者其他则为弹性线程池
threadsIntegerMath.max((Runtime.getRuntime().availableProcessors() << 1) + 1, 16)固定线程池类型时候的线程数量
shenyu.upstreamCheck 配置

Apache ShenYu 动态检测upstream的配置

名称类型默认值是否必填说明
enabledBooleanfalse是否检测
timeoutInteger3000超时配置 (ms)
healthyThresholdInteger1健康因子
unhealthyThresholdInteger1不健康因子
intervalInteger5000检测的调度间隔时间
printEnabledBooleantrue是否打印日志
printIntervalInteger60000打印日志的间隔调度时间
shenyu.ribbon 配置

Apache ShenYu 轮询间隔配置

名称类型默认值是否必填说明
serverListRefreshIntervalInteger10000调整刷新时间间隔参数,参考com.netflix.client.config.CommonClientConfigKey#ServerListRefreshInterval
shenyu.metrics 配置

Apache ShenYu Metrics 配置,网关用来监控自身运行状态

名称类型默认值是否必填说明
enabledBooleanfalse是否开启metrics,true 表示开启
nameString名称
hostString网关服务对采集服务暴露的ip
portInteger网关服务对采集服务暴露的端口
jmxConfigStringjmx配置
props-属性值
jvm_enabledBoolean开启jvm的监控指标
shenyu.sharedPool 配置

Apache ShenYu 共享线程池配置

名称类型默认值是否必填说明
enabledBooleanfalse是否开启共享线程池
prefixString"shenyu-shared"线程池名称前缀
corePoolSizeInteger200线程池核心线程数
maximumPoolSizeIntegerInteger.MAX_VALUE线程池最大线程数
keepAliveTimeLong60000L多余的空闲线程keepAlive时间,单位毫秒
maxWorkQueueMemoryLong当前JVM最大可用内存的80%最大使用内存,单位字节
maxFreeMemoryInteger最大剩余内存,单位字节
shenyu.alert 配置

Apache ShenYu 网关告警通知配置

NameTypeDefaultRequiredDescription
enabledBooleanfalse是否开启告警消息通知
adminsString"localhost:9095"ShenYu Admin 服务地址列表