docker安装openresty

docker安装openresty

  • 拉取官方镜像并运行

docker run --name openresty -p 80:80 -p 443:443 -di  openresty/openresty
  • 访问测试

http://121.199.79.7/    
  • 进入容器修改文件

docker exec -it openresty /bin/bash
  • 容器安装vi

apt-get update
apt-get -y install vim

缓存预热

  • 编写脚本

cd /root
mkdir lua
cd lua
vim ad_update.lua
# 此时vim不能粘贴  必须 esc-》:-》set mouse-=a  然后就可以粘贴下面脚本
# 修改mysql和redis连接信息,我都是docker容器里面的,用的宿主机ip
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 position = uri_args["position"]

local db = mysql:new()
db:set_timeout(1000)  
local props = {  
    host = "121.199.79.7",  
    port = 3306,  
    database = "changgou_business",  
    user = "root",  
    password = "123456"  
}

local res = db:connect(props)  
local select_sql = "select url,image from tb_ad where status ='1' and position='"..position.."' and start_time<= NOW() AND end_time>= NOW()"  
res = db:query(select_sql)  
db:close()  

local redis = require("resty.redis")
local red = redis:new()
red:set_timeout(2000)

local ip ="121.199.79.7"
local port = 6379
red:connect(ip,port)

red:set("ad_"..position,cjson.encode(res))
red:close()

ngx.say("{flag:true}")
  • 修改nginx配置信息

vim /usr/local/openresty/nginx/conf/nginx.conf
# 在第一行添加 
user root root;
# 删除末尾的 include /etc/nginx/conf.d/*.conf;
# 替换成下面的 server内容
#user  nobody;
user root root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        #access_log  logs/host.access.log  main;
        # 添加
        location /ad_update {
            content_by_lua_file /root/lua/ad_update.lua;
        }
        
        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }        
    }
}
  • 重启nginx

/usr/local/openresty/nginx/sbin/nginx -s reload
  • 测试

http://192.168.31.42/ad_update?position=web_index_lb
# 浏览器显示  {flag:true}
# 此时第一阶段配置成功

广告缓存读取

  • 编写脚本

vim /root/lua/ad_read.lua
# 一样  set mouse-=a  才能粘贴
# 修改redis连接后报存
--设置响应头类型
ngx.header.content_type="application/json;charset=utf8"
--获取请求中的参数ID
local uri_args = ngx.req.get_uri_args();
local position = uri_args["position"];
--引入redis库
local redis = require("resty.redis");
--创建redis对象
local red = redis:new()
--设置超时时间
red:set_timeout(2000)
--连接
local ok, err = red:connect("121.199.79.7", 6379)
--获取key的值
local rescontent=red:get("ad_"..position)
--输出到返回响应中
ngx.say(rescontent)
--关闭连接
red:close()
  • 修改nginx配置

# 在/usr/local/openresty/nginx/conf/nginx.conf中server下添加配置
vim /usr/local/openresty/nginx/conf/nginx.conf
        location /ad_read {
            content_by_lua_file /root/lua/ad_read.lua;
        }
  • 重启nginx

/usr/local/openresty/nginx/sbin/nginx -s reload
  • 测试

http://192.168.31.42/ad_read?position=web_index_lb 

二级缓存-加入openresty本地缓存

  • 编写脚本

# 修改之前read文件
vim /root/lua/ad_read.lua
# 删除内容 
# 并 set mouse-=a 后 粘贴
# 还是修改 redis 连接信息
--设置响应头类型
ngx.header.content_type="application/json;charset=utf8"
--获取请求中的参数ID
local uri_args = ngx.req.get_uri_args();
local position = uri_args["position"];

--获取本地缓存
local cache_ngx = ngx.shared.dis_cache;
--根据ID 获取本地缓存数据
local adCache = cache_ngx:get('ad_cache_'..position);

if adCache == "" or adCache == nil then

	--引入redis库
	local redis = require("resty.redis");
	--创建redis对象
	local red = redis:new()
	--设置超时时间
	red:set_timeout(2000)
	--连接
	local ok, err = red:connect("121.199.79.7", 6379)
	--获取key的值
	local rescontent=red:get("ad_"..position)
	--输出到返回响应中
	ngx.say(rescontent)
	--关闭连接
	red:close()
	--将redis中获取到的数据存入nginx本地缓存
	cache_ngx:set('ad_cache_'..position, rescontent, 10*60);
else
 	--nginx本地缓存中获取到数据直接输出
 	ngx.say(adCache)
end
  • 修改nginx配置

# 修改nginx配置文件,http节点下添加配置:
vim /usr/local/openresty/nginx/conf/nginx.conf
# 在http里面添加下面数据 5m 可以自定义
#包含redis初始化模块
lua_shared_dict dis_cache 5m;  #共享内存开启
  • 重启nginx

/usr/local/openresty/nginx/sbin/nginx -s reload
  • 测试

http://192.168.31.42/ad_read?position=web_index_lb 

nginx限流

  • 匀速限流

docker exec -it openresty /bin/bash

vim /usr/local/openresty/nginx/conf/nginx.conf

# 在http里面设置限流配置,与缓存同级
 # 设置限流配置 $binary_remote_addr:根据请求ip限流  myRateLimit:缓存空间10m rate:每秒允许2个请求
 limit_req_zone $binary_remote_addr zone=myRateLimit:10m rate=2r/s;
# 在server location下 设置缓存生效
limit_req zone=myRateLimit;
# 最终配置
# nginx.conf  --  docker-openresty
#
# This file is installed to:
#   `/usr/local/openresty/nginx/conf/nginx.conf`
# and is the file loaded by nginx at startup,
# unless the user specifies otherwise.
#
# It tracks the upstream OpenResty's `nginx.conf`, but removes the `server`
# section and adds this directive:
#     `include /etc/nginx/conf.d/*.conf;`
#
# The `docker-openresty` file `nginx.vh.default.conf` is copied to
# `/etc/nginx/conf.d/default.conf`.  It contains the `server section
# of the upstream `nginx.conf`.
#
# See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
#

#user  nobody;
user root root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #设置名为dis_cache的共享内存空间,内存大小为5m。
    lua_shared_dict dis_cache 5m; 

     # 设置限流配置
     limit_req_zone $binary_remote_addr zone=myRateLimit:10m rate=2r/s;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    # See Move default writable paths to a dedicated directory (#119)
    # https://github.com/openresty/docker-openresty/issues/119
    client_body_temp_path /var/run/openresty/nginx-client-body;
    proxy_temp_path       /var/run/openresty/nginx-proxy;
    fastcgi_temp_path     /var/run/openresty/nginx-fastcgi;
    uwsgi_temp_path       /var/run/openresty/nginx-uwsgi;
    scgi_temp_path        /var/run/openresty/nginx-scgi;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        #access_log  logs/host.access.log  main;
        # 添加
        location /ad_update {
            content_by_lua_file /root/lua/ad_load.lua;
        }

        location /ad_read {
            limit_req zone=myRateLimit;
            content_by_lua_file /root/lua/ad_read.lua;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

docker安装openresty_第1张图片
1s刷新2次就会限流,代表每隔500ms处理一个请求,如果在500ms内有多个请求,则拒绝
docker安装openresty_第2张图片

  • 处理突发流量

docker exec -it openresty /bin/bash

vim /usr/local/openresty/nginx/conf/nginx.conf
# 在server location下 设置缓存生效 在限流配置后增加 burst nodelay
limit_req zone=myRateLimit  burst=5 nodelay;
# 平均每秒允许不超过2个请求,突发不超过5个请求,并且处理突发5个请求的时候,没有延迟,等到完成之后,按照正常的速率处理。
# 重启nginx
 /usr/local/openresty/nginx/sbin/nginx -s reload
  • nginx 代理goods

docker安装openresty_第3张图片
把所有的 /brand请求发往本机的goods服务
docker安装openresty_第4张图片
限制每秒钟2个请求
docker安装openresty_第5张图片

最后:推荐给大家一个压力请求测试的软件 jmeter

# 下载
http://jmeter.apache.org/download_jmeter.cgi
# 运行
 打开bin/jmeter.bat
# 设置中文
 打开bin/jmeter.properties
 在第40行添加
 language=Zh_CN

使用

docker安装openresty_第6张图片

你可能感兴趣的:(docker)