【Java团队用OpenResty】2、Eclipse开发环境搭建

来源:互联网 发布:windows loader 和kms 编辑:程序博客网 时间:2024/06/05 09:49

一、前言

前一篇文章介绍了如何使用Notepad++开发OpenResty的应用,我们团队用了一段发现如下的问题:

1、很多情况下都会对nginx.conf进行修改,每次都去修改resty的perl脚本的模板,非常不方便,尤其是有多个项目的时候(一个实际项目、N个测试代码项目)

2、rest中代码是放在init_worker_by_lua如果需要在access_by_lua,或者需要从http request中获取数据的,resty就无能为力

3、notepad++不能很好的和svn、git进行集成,进行源代码管理

所以,我们还是在notepad++上编译,然后放在linux下,通过重启调试,当然后来发现,设置lua_code_cache off可以避免重启,但是还是不能很好的解决前面提到几个问题,难道Java团队的Eclipse就不能开发OpenResty应用吗?我们一定行!

二、Eclipse开发环境搭建

首先,在Eclipse上安装Lua Development Tools

安装成功后,就可以创建Lua Project和Lua File


通常的Lua就可以跑起来了,当然这只是我们的第一步,我们在Lua项目缺省的main.lua中写上一段带ngx.say的才算成功。

首先我们创建一个Lua项目,在src下会有一个缺省的main.lua,我们另外创建一个conf目录用来保存nginx配置文件。


下面是简单的nginx.conf示例

worker_processes  2;error_log  logs/error.log  info;events {    worker_connections  1024;}http {    default_type  application/octet-stream;    access_log  logs/access.log;    lua_package_path 'openresty-test/?.lua;;';    server {        listen       7777;        server_name  localhost;        default_type text/html;        location = /favicon.ico {            log_not_found off;            access_log off;        }        location /hello {            content_by_lua '              ngx.say("hello world")            ';        }        location /main {          content_by_lua_file openresty-test/main.lua;        }    }}

这个怎么跑,这时就需要我们今天的主角登场了,那就是已经被Java团队抛弃几年的apache ant。

是的,我们就利用ant,将nginx.conf和main.lua放在C:\ngx_openresty-1.9.7.1-win32

下,然后运行nginx程序,通过程序或者浏览器进行测试。

三、Ant脚本

简单解释一下,就是首先执行nginx stop清理可能存在的nginx进程,然后把之前的logs、conf和程序目录都清理掉,重新复制一份,最后再启动新的nginx

<project name="openresty-test" default="run" basedir=".">    <description>        run openresty-test    </description>  <!-- set global properties for this build -->  <property name="openresty-home" location="c:\ngx_openresty-1.9.7.1-win32"/>  <property name="conf" location="${basedir}/conf"/>  <property name="src" location="${basedir}/src"/><property name="target-conf" location="${openresty-home}/conf"/>  <property name="target-src" location="${openresty-home}/openresty-test"/>  <echo>######开发版本的ant配置#####</echo>    <target name="shutdown" depends="">    <echo>关闭正在运行的Nginx进程</echo>    <exec dir="${openresty-home}" executable="nginx.exe">      <arg line="-s stop"/>    </exec>  </target>  <target name="clean" depends="shutdown">    <echo>清理openresty目录 ${dist}下的conf,logs,janus,januslib</echo>    <delete dir="${target-conf}"/>    <delete dir="${target-src}"/>    <delete>      <fileset dir="${openresty-home}/logs" includes="*.log">      </fileset>    </delete>  </target>    <target name="init" depends="clean">    <echo>创建安装目录</echo>    <mkdir dir="${target-conf}"/>    <mkdir dir="${target-src}"/>  </target>  <target name="dist" depends="init" description="generate the distribution" >    <echo>复制安装文件</echo>    <copy todir="${target-conf}">      <fileset dir="${conf}"></fileset>    </copy>    <copy todir="${target-src}">      <fileset dir="${src}"></fileset>    </copy>  </target>  <target name="run" depends="shutdown,dist">    <echo>启动Nginx成功...</echo>    <exec dir="${openresty-home}" executable="nginx.exe"></exec>  </target></project>

四、小结

通过这种方式,每次都清理openresty的logs目录,重启部署程序,进行测试,也能够很好的支持git进行代码管理。团队成员顺道在创建一个Junit的功能测试,使用httpclient模拟用户输入,开发效率杠杠的!



原创粉丝点击