系統環境:CentOS7.6
1 添加源
yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
2 執行安裝
yum install openresty
3 openresty內置了nginx,修改其conf檔案
cd /usr/local/openresty/nginx/conf
ls
vi nginx.conf
4 給nginx.conf檔案server 添加如下一段
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
#應該只要添加下面的
#用戶請求url/update_content?id=1 攔截,交由lua腳本控制
location /update_content {
content_by_lua_file /mine/lua/update_content.lua;
}
}
5 lua安裝

6 訪問遠程服務器ip 80埠

7 openresty+lua 的首頁廣告快取高可用測驗,步驟如下:
核心意思:url請求被攔截–>交由lua腳本控制–>先查詢nginx cache ^1 ,若沒有–>查詢redis,若沒有 ^2 -->查詢資料庫 ^3
- 如果到^2步回傳資料,會將資料快取到nginx cache
- 如果到^3步回傳資料,會將資料快取到redis

8 在根目錄下創建檔案夾mine/lua,創建測驗檔案update_content.lua內容如下:
ngx.header.content_type="application/json;charset=utf8"
local cjson = require("cjson")
local mysql = require("resty.mysql")
local uri_args = ngx.req.get_uri_args()
local id = uri_args["id"]
local db = mysql:new()
db:set_timeout(1000)
local props = {
host = IP
port = 3306,
database = "changgou_content",
user = "root",
password = PWD
}
local res = db:connect(props)
local select_sql = "select url,pic from tb_content where status ='1' and category_id="..id.." order by sort_order"
res = db:query(select_sql)
db:close()
local redis = require("resty.redis")
local red = redis:new()
red:set_timeout(2000)
local ip = IP
local port = 6379
red:connect(ip, port)
red:set("content_"..id,cjson.encode(res))
red:close()
ngx.say("{flag:true}")

9 多載組態檔
cd /usr/local/openresty/nginx/conf

10 測驗請求

注:
- 如果有說什么nginx.log檔案不存在,是組態檔沒有關聯,
/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
- 以上是測驗,具體實作:
在 /mine/lua檔案夾下創建read_content.lua,具體內容如下:
--設定回應頭型別
ngx.header.content_type="application/json;charset=utf8"
--獲取請求中的引數ID
local uri_args = ngx.req.get_uri_args();
local id = uri_args["id"];
--獲取本地快取
local cache_ngx = ngx.shared.dis_cache;
--根據ID 獲取本地快取資料
local contentCache = cache_ngx:get('content_cache_'..id);
if contentCache == "" or contentCache == nil then
--引入redis庫
local redis = require("resty.redis");
--創建redis物件
local red = redis:new()
--設定超時時間
red:set_timeout(2000)
--連接
red:connect(IP, 6379)
local rescontent=red:get("content_"..id);
if ngx.null == rescontent then
local cjson = require("cjson");
local mysql = require("resty.mysql");
local db = mysql:new();
db:set_timeout(2000)
local props = {
host = IP
port = 3306,
database = "changgou_content",
user = "root",
password = PWD
}
local res = db:connect(props);
local select_sql = "select url,pic from tb_content where status='1' and category_id="..id.." order by sort_order";
res = db:query(select_sql);
local responsejson = cjson.encode(res);
red:set("content_"..id,responsejson);
ngx.say(responsejson);
db:close()
else
cache_ngx:set('content_cache_'..id, rescontent, 10*60);
ngx.say(rescontent)
end
red:close()
else
ngx.say(contentCache)
end

修改nginx.conf,添加內容如下:
cd /usr/local/openresty/nginx/conf
vi nginx.conf

多載組態檔,見9,請求訪問:

查詢的是資料庫,并且將快取存入了redis

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/291436.html
標籤:其他
