ToB企服应用市场:ToB评测及商务社交产业平台

标题: 精选版:用Java扩展Nginx(nginx-clojure 入门) [打印本页]

作者: 科技颠覆者    时间: 2023-9-7 07:37
标题: 精选版:用Java扩展Nginx(nginx-clojure 入门)
欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
本篇概览


关于Nginx

关于Nginx扩展

为什么要用java对nginx扩展?

Java程序员的尴尬

nginx-clojure,让Java扩展nginx成为现实

关于nginx-clojure

实战功能介绍

环境信息

下载集成了nginx-clojure模块的nginx包

解压nginx包

  1. ./nginx -v
  2. nginx version: nginx/1.18.0
复制代码
编码,开发java版handler

  1. <repositories>
  2.   <repository>
  3.     <id>clojars.org</id>
  4.     <url>http://clojars.org/repo</url>
  5.     </repository>
  6. </repositories>
  7. <dependencies>
  8.   <dependency>
  9.     <groupId>nginx-clojure</groupId>
  10.     <artifactId>nginx-clojure</artifactId>
  11.     <version>0.5.2</version>
  12.   </dependency>
  13. </dependencies>
复制代码
  1. package com.bolingcavalry.simplehello;
  2. import nginx.clojure.java.ArrayMap;
  3. import nginx.clojure.java.NginxJavaRingHandler;
  4. import java.time.LocalDateTime;
  5. import java.util.Map;
  6. import static nginx.clojure.MiniConstants.CONTENT_TYPE;
  7. import static nginx.clojure.MiniConstants.NGX_HTTP_OK;
  8. /**
  9. * @author zq2599@gmail.com
  10. * @Title: 产生内容的handler
  11. * @Package
  12. * @Description:
  13. * @date 2/1/22 12:41 PM
  14. */
  15. public class HelloHandler implements NginxJavaRingHandler {
  16.     @Override
  17.     public Object[] invoke(Map<String, Object> request) {
  18.         return new Object[] {
  19.                 NGX_HTTP_OK, //http status 200
  20.                 ArrayMap.create(CONTENT_TYPE, "text/plain"), //headers map
  21.                 "Hello, Nginx clojure! " + LocalDateTime.now()  //response body can be string, File or Array/Collection of them
  22.         };
  23.     }
  24. }
复制代码
编译,生成jar

jar放入nginx的jars目录

修改nginx的配置

  1. location /java {
  2.          content_handler_type 'java';
  3.          content_handler_name 'com.bolingcavalry.simplehello.HelloHandler';
  4. }
复制代码
  1. ###you can uncomment next two lines for easy debug
  2. ###Warning: if master_process is off, there will be only one nginx worker running. Only use it for debug propose.
  3. #daemon  off;
  4. #master_process  off;
  5. #user  nobody;
  6. worker_processes  1;
  7. #error_log  logs/error.log;
  8. #error_log  logs/error.log  notice;
  9. #error_log  logs/error.log  info;
  10. #pid        logs/nginx.pid;
  11. events {
  12.     worker_connections  1024;
  13. }
  14. http {
  15.     include       mime.types;
  16.     default_type  application/octet-stream;
  17.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  18.     #                  '$status $body_bytes_sent "$http_referer" '
  19.     #                  '"$http_user_agent" "$http_x_forwarded_for"';
  20.     #access_log  logs/access.log  main;
  21.     sendfile        on;
  22.     #tcp_nopush     on;
  23.     #keepalive_timeout  0;
  24.     keepalive_timeout  65;
  25.     #gzip  on;
  26.    
  27.     jvm_path auto;
  28.    
  29.     ### Set my app jars and resources, it must include nginx-clojure runtime jar,e.g. nginx-clojure-0.5.1.jar and
  30.     ### for clojure user clojure runtime jar is also needed.
  31.     ### See http://nginx-clojure.github.io/directives.html#jvm_classpath
  32.     jvm_classpath "libs/*:jars/*";
  33.    
  34.     ###jvm heap memory
  35.     #jvm_options "-Xms1024m";
  36.     #jvm_options "-Xmx1024m";
  37.    
  38.     #for enable java remote debug uncomment next two lines
  39.     #jvm_options "-Xdebug";
  40.     #jvm_options "-Xrunjdwp:server=y,transport=dt_socket,address=840#{pno},suspend=n";
  41.     ###threads number for request handler thread pool on jvm, default is 0.
  42.     ###check more details from
  43.     #jvm_workers 8;
  44.     server {
  45.         listen       8080;
  46.         server_name  localhost;
  47.         #charset koi8-r;
  48.         #access_log  logs/host.access.log  main;
  49.         location / {
  50.             root   html;
  51.             index  index.html index.htm;
  52.         }
  53.         #error_page  404              /404.html;
  54.         # redirect server error pages to the static page /50x.html
  55.         #
  56.         error_page   500 502 503 504  /50x.html;
  57.         location = /50x.html {
  58.             root   html;
  59.         }
  60.        location /clojure {
  61.           handler_type 'clojure';
  62.           handler_code '
  63.                                                 (fn[req]
  64.                                                   {
  65.                                                     :status 200,
  66.                                                     :headers {"content-type" "text/plain"},
  67.                                                     :body  "Hello Clojure & Nginx!"
  68.                                                     })
  69.           ';
  70.        }
  71.        location /java {
  72.          content_handler_type 'java';
  73.          content_handler_name 'com.bolingcavalry.simplehello.HelloHandler';
  74.        }
  75.       
  76. #      location /groovy {
  77. #          handler_type 'groovy';
  78. #          handler_code '
  79. #               import nginx.clojure.java.NginxJavaRingHandler;
  80. #               import java.util.Map;
  81. #               public class HelloGroovy implements NginxJavaRingHandler {
  82. #                  public Object[] invoke(Map<String, Object> request){
  83. #                     return [200, //http status 200
  84. #                             ["Content-Type":"text/html"], //headers map
  85. #                             "Hello, Groovy & Nginx!"]; //response body can be string, File or Array/Collection of them
  86. #                  }
  87. #               }
  88. #          ';
  89. #       }
  90. #         
  91.     }
  92. }
复制代码
启动nginx

  1. 2022/02/02 17:45:07 [emerg] 27703#0: bind() to 0.0.0.0:8080 failed (48: Address already in use)
  2. 2022/02/02 17:45:07 [emerg] 27703#0: bind() to 0.0.0.0:8080 failed (48: Address already in use)
  3. 2022/02/02 17:45:07 [emerg] 27703#0: bind() to 0.0.0.0:8080 failed (48: Address already in use)
  4. 2022/02/02 17:45:07 [emerg] 27703#0: bind() to 0.0.0.0:8080 failed (48: Address already in use)
  5. 2022/02/02 17:45:07 [emerg] 27703#0: bind() to 0.0.0.0:8080 failed (48: Address already in use)
  6. 2022/02/02 17:45:07 [emerg] 27703#0: still could not bind()
复制代码
验证

源码下载

名称链接备注项目主页https://github.com/zq2599/blog_demos该项目在GitHub上的主页git仓库地址(https)https://github.com/zq2599/blog_demos.git该项目源码的仓库地址,https协议git仓库地址(ssh)git@github.com:zq2599/blog_demos.git该项目源码的仓库地址,ssh协议
欢迎关注博客园:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4