簡體   English   中英

X-Cache-Status 總是使用 Kong 代理緩存插件繞過

[英]X-Cache-Status is always Bypass using Kong proxy-cache plugin

我在 Kong 有服務,並且已經為該服務設置了代理緩存插件。

curl -X POST http://localhost:8001/plugins --data "name=proxy-cache" --data "config.strategy=redis" --data 'service_id=2f0a285d-7b25-48d6-adc3-bbf28ffe5f47' --data "config.redis.host=127.0.0.1" --data "config.redis.port=6379" --data "config.redis.password=my_redis_password"

當我從該服務調用 API 時:

curl -i -X GET --url http://localhost:3002/v1/currency --header 'apikey: MY_API_KEY'

一切正常,但X-Cache-Status總是繞過

HTTP/1.1 200 OK                                                                                                                                       
Content-Type: application/json; charset=utf-8                                                                                                         
Content-Length: 3654                                                                                                                                  
Connection: keep-alive                                                                                                                                
X-RateLimit-Limit-second: 100                                                                                                                         
X-RateLimit-Remaining-second: 99                                                                                                                      
X-Cache-Key: 3e18cdfc6e02359fb0f874efdf5788d8                                                                                                         
X-Cache-Status: Bypass                                                                                                                                
X-Powered-By: Express
...

如何調試繞過原因?

為避免在 X-Cache-Status 中繞過,您必須在創建代理緩存插件時添加此配置

--data "config.content_type=application/json; charset=utf-8" 

Kong 社區版附帶的插件proxy-cache僅允許內存緩存。 如果要使用Redis進行緩存,則必須使用Kong Enterprise版本。 更多信息在這里

作為替代方案, Github上有一個名為kong-plugin-proxy-cache開源插件。 您必須首先從 Luarocks 安裝插件,然后在 Kong 配置中啟用該插件

    # Install plugin dependency
    sudo luarocks install lua-resty-redis-connector

    # install plugin
    sudo luarocks install kong-plugin-proxy-cache

    # Enable plugin in kong.conf
    plugins = bundled,proxy-cache

    # After enabling, you can use plugin with any service, route or consumer.
    # To enable it for a service
    curl -X POST http://localhost:8001/services/<service-name>/plugins \
    --data "name=proxy-cache"  \
    --data "config.cache_ttl=300" \
    --data "config.cache_control=false" \
    --data "config.redis.host=<redis-host>" \
    --data "config.redis.port=<redis-port>"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM