nginx-clojure java 集成试用
主要是基于java 开发一个简单的扩展,学习下流程
java 项目
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalong</groupId>
<artifactId>nginx-clojure-java-learning</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>nginx-clojure</groupId>
<artifactId>nginx-clojure</artifactId>
<version>0.5.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>clojars.org</id>
<url>https://clojars.org/repo</url>
</repository>
</repositories>
<build>
<finalName>javaapp</finalName>
</build>
</project>
MyApp.java
package com.dalong;
import nginx.clojure.java.ArrayMap;
import nginx.clojure.java.NginxJavaRingHandler;
import java.io.IOException;
import java.util.Map;
import static nginx.clojure.MiniConstants.*;
public class MyApp implements NginxJavaRingHandler {
@Override
public Object[] invoke(Map<String, Object> map) throws IOException {
return new Object[] {
NGX_HTTP_OK, //http status 200
ArrayMap.create(CONTENT_TYPE, "text/plain"), //headers map
"Hello, Java & Nginx!" //response body can be string, File or Array/Collection of string or File
};
}
}
运行
基于docker-compose
- docker-compose 文件
version: '3'
services:
app:
image: dalongrong/nginx-clojure-0.5.3:latest
volumes:
- ./nginx.conf:/usr/local/nginx/conf/nginx.conf
- ./appjars:/opt/appjars
ports:
- 80:80
- nginx.conf
###you can uncomment next two lines for easy debug
###Warning: if master_process is off, there will be only one nginx worker running. Only use it for debug propose.
#daemon off;
#master_process off;
user 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;
keepalive_timeout 65;
#gzip on;
jvm_path auto;
### Set my app jars and resources, it must include nginx-clojure runtime jar,e.g. nginx-clojure-0.5.3.jar and
### for clojure user clojure runtime jar is also needed.
### See http://nginx-clojure.github.io/directives.html#jvm_classpath
# 添加自定义classpath
jvm_classpath "libs/*:jars/*:/opt/appjars/*";
###jvm heap memory
#jvm_options "-Xms1024m";
#jvm_options "-Xmx1024m";
#for enable java remote debug uncomment next two lines
#jvm_options "-Xdebug";
#jvm_options "-Xrunjdwp:server=y,transport=dt_socket,address=840#{pno},suspend=n";
###threads number for request handler thread pool on jvm, default is 0.
###check more details from
#jvm_workers 8;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 自己开发的java 方法
location /dalongdemo {
content_handler_type 'java';
content_handler_name 'com.dalong.MyApp';
}
location /clojure {
handler_type 'clojure';
handler_code '
(fn[req]
{
:status 200,
:headers {"content-type" "text/plain"},
:body "Hello Clojure & Nginx!"
})
';
}
}
}
- 运行效果
说明
nginx-clojure 很不错,而且很强大,对于nginx 的扩展开发我们又多了一种可选的方法
参考资料
https://nginx-clojure.github.io/installation.html
https://nginx-clojure.github.io/quickstart.html
https://github.com/rongfengliang/nginx-clojure-learning
https://github.com/rongfengliang/nginx-clojure-dockerfile