网关属性配置
本篇主要讲解如何在 Apache Shenyu 网关配置 ShenYu 的相关属性。
属性配置
shenyu:
# 下列匹配缓存配置仅适用于当前开发线;ShenYu 2.7.1 及更早版本使用的是已移除的前缀树 L2 缓存
selectorMatchCache:
## 选择器按路径的有界匹配缓存
cache:
enabled: true
initialCapacity: 10000 # initial capacity in cache
maximumSize: 10000 # max size in cache
ttl: 60000 # match result lifetime in milliseconds, must be positive
ruleMatchCache:
## 规则按路径的有界匹配缓存
cache:
enabled: true
initialCapacity: 10000 # initial capacity in cache
maximumSize: 10000 # max size in cache
ttl: 60000 # match result lifetime in milliseconds, must be positive
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.selectorMatchCache.cache 与 shenyu.ruleMatchCache.cache 配置
shenyu.selectorMatchCache.cache 与 shenyu.ruleMatchCache.cache 绑定到 MatchCacheConfig,分别为选择器匹配和规则匹配提供按路径的有界缓存。当前实现中不存在单独的 shenyu.matchCache 前缀,也不存在前缀树(trie)L2 缓存。
本节描述当前开发线(对照 2.7.3-SNAPSHOT 验证)。ShenYu 2.7.1 及更早版本提供的是基于前缀树的 L2 缓存;该版本实际支持的配置请见 2.7.1 版本页。
下表为 Java 模型默认值;具体的发行版样例可能覆盖这些值。
| 字段 | 类型 | 默认值 | 是否必填 | 说明 |
|---|---|---|---|---|
| enabled | Boolean | true | No | 是否在选择器/规则匹配时查询按路径的有界匹配缓存。只有候选集合满足"仅由路径决定"的条件时才会写入缓存条目(见下文) |
| initialCapacity | int | 10000 | No | 匹配缓存初始化容量 |
| maximumSize | long | 10000 | No | 匹配缓存最大条目数 |
| ttl | long | 60000 | No | 匹配结果存活时间,单位为毫秒,必须为正数;非正值会在属性绑定时抛出 Match cache ttl must be positive |
哪些结果会被缓存
缓存条目以 (插件名, 路径) 为键,保存命中的选择器/规则,或"该路径没有候选命中"的负缓存条目。由于一次缓存命中会跳过 整个候选集合,只有当匹配完全由请求路径决定时才会写入条目:
- 所有启用的候选选择器/规则的条件都是 URI 类型,且使用以下仅路径安全的操作符之一:
=、equals、contains、startsWith、endsWith、match、regex、exclude、pathPattern、isBlank。 - 没有任何启用的候选被标记为
matchRestful,且不存在条件列表为空的候选(例如全流量匹配选择器)——这类候选始终实时匹配,不会产生缓存条目。 - 正向条目还要求恰好只有一个候选命中:当同一请求有两个及以上候选命中时,仍会按条件数和排序选出胜者,但该结果不会写入缓存。
- 规则负缓存条目的键中不包含选择器 id,因此只有当该插件的整个选择器集合同样由路径决定时,负缓存才是安全的。
依赖请求头、查询参数、Cookie、IP/域名、时间相关谓词或自定义 SPI 操作符的条件取决于 URI 之外的状态,不满足缓存条件:这类匹配每次请求都会实时执行。这是实现上的安全限制,而不是对"看似 URI 规则"的一般性建议。
举例:如果选择器的条件只有 uri 类型的 pathPattern /api/**,它是可缓存的——匹配结果完全取决于请求路径。而如果选择器还额外依赖请求头条件(如 X-Token: abc),则不可缓存,因为同一路径携带不同请求头的两个请求可能得到不同的匹配结果。请注意,是否可以缓存取决于整个候选集合,而不只是命中的那条规则:路径键的缓存命中会跳过所有候选,因此只有当所有启用的候选都不依赖路径之外的状态时,才会写入缓存。