springboot+nginx+lua開發+html模板渲染

1、安裝openresty+nginxhtml

    1.安裝gcc  
 yum install -y readline-devel pcre-devel openssl-devel gcc
   2 新建目錄 mkdir /usr/server, cd /usr/server
   3 下載 openresty  wget http://openresty.org/download/openresty-1.11.2.5.tar.gz
   4  tar -zxvf  /usr/server/openresty-1.11.2.5.tar.gz
   5  cd bundle/LuaJIT-2.1-20170808/
   6  make clean && make && make install 
   7  ln -sf luajit-2.1.0-alpha /usr/local/bin/luajit
   8  cd /usr/server/openresty-1.11.2.5/bundle/ 
   9  wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.4.tar.gz  
   10 tar -xvf 2.3.tar.gz 
   11 cd /usr/server/openresty-1.11.2.5/bundle/ 
   12 wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz 
   13 tar -xvf v0.3.0.tar.gz
   14 cd /usr/server/openresty-1.11.2.5/
   15 編譯linux

 ./configure --prefix=/usr/server --with-http_realip_module  --with-pcre  --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2  
    16安裝 nginx

     make && make install 
    openresty+nginx 安裝完成 git

 16   查看有沒有安裝成功 github

        cd /usr/server
        文件目錄

        啓動nginx web

        /usr/server/nginx/sbin/nginx
17 查看進程有沒有啓動

ps axu|grep nginx
spring

會看到nginx 進程
用瀏覽器訪問下vim

http://ip:端口

部署成功
2、用ngxin+lua腳本開發一個 hello lua 程序
       
1. cd /usr/server/nginx/conf/ 目錄 vim nginx.conf
        2 .ngxin.conf 文件http 部分添加
                lua_package_path "/usr/server/lualib/?.lua;;";      
                lua_package_cpath "/usr/server/lualib/?.so;;";  
                include lua.conf;瀏覽器

       3.在/usr/servers/nginx/conf下,建立一個lua.conf
       4.vim lua.conf
       server{
        listen 80;
        server_name locahost;
        location /lua{
                default_type 'text/html';
                content_by_lua 'ngx.say("hello lua")';
            }
        }


       5.從新加載nginx 
        /usr/server/nginx/sbin/nginx -s reload  springboot

        用瀏覽器 訪問 ip+端口+路徑

        http://192.168.31.103/lua

        
        
    網站上顯示效果
3、springboot 搭建http接口 用於lua調用
    1.新建一個springboot的工程

    
2.新建一個類

    

package com.sumei.lua.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LuaController {
    
    @RequestMapping("lua")
    public String lug(){
        return "{'lua':'lua'}";
    }
}

用main 方法啓動spring boot

用瀏覽器訪問
本地寫的服務
http://192.168.31.134:8080/lua

 

四.用lua調用 springboot發佈的接口

1.由於要發送http請求,須要下載兩個http包
cd /usr/server/lualib/resty/

wget https://raw.githubusercontent.com/pintsized/lua-resty-ttp/master/lib/resty/http_headers.lua  
wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua 

2.cd /usr

   mkdir lua

    vim boot.lua

  在vim中編輯

--要請求的ip  
url="http://192.168.31.134:8080"
-- 請求的路徑
local path="/lua"
-- 發送請求
local http = require("resty.http")
local httpClient=http.new()
local resp, err = httpClient:request_uri(url, {
    method = "GET",
    path = path
})

-- 獲取請求結果
ngx.say(resp.body)
httpClient:close()

3.cd /usr/server/nginx/conf 目錄  編輯 lua.conf

server{
        listen 80;
        server_name locahost;
        location /lua{
                default_type 'text/html';
                content_by_lua 'ngx.say("hello lua")';
        }

       location /boot{
                default_type 'text/html';
                content_by_lua_file /usr/lua/boot.lua;
        }
}
 

用瀏覽器訪問linux的ip+/boot 就能夠轉發到本地的springboot的服務上去

個人linux 的ip是 192.168.31.131 

      

5.用lua+springboot進行html模板渲染

1.下載兩個lua腳本

cd /usr/server/lualib/resty/
wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template.lua
mkdir /usr/hello/lualib/resty/html
cd /usr/server/lualib/resty/
wget https://raw.githubusercontent.com/bungle/lua-resty-
template/master/lib/resty/template/html.lua

2.cd /usr/server/nginx/conf/

    編輯lua.conf  配置靜態模板的位置
     

server{
        set $template_location "/templates";
        set $template_root "/usr/lua/templates";
        listen 80;
        server_name locahost;
        location /lua{
                default_type 'text/html';
                content_by_lua 'ngx.say("hello lua")';
        }

      location /boot{
                default_type 'text/html';
                content_by_lua_file /usr/lua/intDes.lua;
        }

}

mkdir -p /usr/server/nginx/html/templates/
vim  /usr/server/nginx/html/templates/w3c.html

<!DOCTYPE html>
<html>
<body>

<article>
  <h1>{*internet*}</h1>
  <p>{*des*}</p>
</article>

</body>
</html>
 

3.新建個發佈個接口
 

package com.sumei.lua.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LuaController {

    @RequestMapping("lua")
    public String lug(){
        return "{'lua':'lua'}";
    }
    @RequestMapping("interDes")
    public String  luaDes(){
        System.out.println("測試");
        return "{\"internet\":\"ie\",\"des\":\"...................................................\"}";
    }
}

    

進行測試

在瀏覽器中輸入 nginx 上的地址+ip 

http://192.168.31.103/boot

搞定。

相關文章
相關標籤/搜索