本文是在Nginx+lua实现本地HTML页面动态渲染文章的基础上进行的。
在Nginx+lua实现本地HTML页面动态渲染中我们已经解决了在Nginx本地动态渲染HTML以及数据在Nginx中进行存取,现在就来解决下一个问题,在Nginx中向后台服务发送请求获取数据。
添加HTTP依赖
进入到OpenResty安装目录根目录下的lualib/resty目录下
wget https://github.com/ledgetech/lua-resty-http/raw/master/lib/resty/http_headers.lua
wget https://github.com/ledgetech/lua-resty-http/raw/master/lib/resty/http.lua
编写后台服务
代码非常简单,一个People对象,一个根据username获取People实例的get方法,这里为了方便,就直接使用堆内存预先存放一些对象。
public class PeopleModel {
private String username;
private Integer age;
private String hobby;
public PeopleModel(String username, Integer age, String hobby) {
this.username = username;
this.age = age;
this.hobby &#