通过NodeJS自动生成的MySQL的REST风格API

通过NodeJS自动生成的MySQL的REST风格API

 

前置条件

安装NodeJS,requires node >= 7.6.0,目前使用的稳定版NodeJS v10.x

 

安装XMYSQL

xmysql requires node >= 7.6.0

npm install -g xmysql
xmysql -h localhost -u mysqlUsername -p mysqlPassword -d databaseName
http://localhost:3000

 

验证查询

Docker方式安装XMYSQL

1. 配置docker-compose.yml文件

version: '3'
services:
    xmysql:
        image: markuman/xmysql:0.4.2
        ports:
            - "80"
        environment:
            - DATABASE_HOST=172.18.231.224
            - DATABASE_USER=root
            - DATABASE_PASSWORD=ziyequma
            - DATABASE_NAME=jeecg
        restart: always

2. 启动容器

  • docker-compose up -d

3. 设置集群模式

  • docker-compse scale xmysql=10

4. 查看IP端口

5. 配置nginx的负载均衡

 

配置Nginx反向代理

events {
   worker_connections 1024;
   
}
http {
    server {
        server_name 127.0.0.1;
        listen 80 ;
        location / {
            rewrite ^/(.*) /$1 break;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://127.0.0.1:3000;
        }
    }
}

完整nginx.conf文件

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /usr/local/nginx/logs/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    
    include       mime.types;
    default_type  application/octet-stream;

    #access_log  logs/access.log  main;
    client_max_body_size 20m;

    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    
    gzip  on;

    # 设置缓存的路径和其他参数
    # proxy_cache_path path [levels=levels] keys_zone=name:size [inactive=time] [max_size=size] [loader_files=number] [loader_sleep=time] [loader_threshold=time];
    # 缓存路径 /data/nginx/cache 缓存结构为 2 层,即该路径下会有 2 层子目录,缓存文件会保存在最下层子目录
    # 缓存的 key 会保存在名为 web_cache 的内存区域,该内存区域大小为 50 m
    # 10 分钟内缓存没有被访问就会过期
    # 缓存文件最多占用 1g 空间
    proxy_cache_path ./web_cache levels=1:2 keys_zone=web_cache:1024m inactive=1000m max_size=1g;

    upstream manage-web_name-live {
        server 100.100.100.100:8000 weight=10  max_fails=3 fail_timeout=30s;
    }
   
    upstream cwbase {
        server 127.0.0.1:32777 weight=10 max_fails=3 fail_timeout=30s;
        server 127.0.0.1:32778 weight=10 max_fails=3 fail_timeout=30s;
        server 127.0.0.1:32779 weight=10 max_fails=3 fail_timeout=30s;
        server 127.0.0.1:32780 weight=10 max_fails=3 fail_timeout=30s;
        server 127.0.0.1:32781 weight=10 max_fails=3 fail_timeout=30s;
        server 127.0.0.1:32782 weight=10 max_fails=3 fail_timeout=30s;
        server 127.0.0.1:32783 weight=10 max_fails=3 fail_timeout=30s;
        server 127.0.0.1:32784 weight=10 max_fails=3 fail_timeout=30s;
        server 127.0.0.1:32785 weight=10 max_fails=3 fail_timeout=30s;
        server 127.0.0.1:32786 weight=10 max_fails=3 fail_timeout=30s;
    }

    server {
        listen       443 ssl;
        server_name  web_name.domain_name.com;

        ssl_certificate /root/.caddy/acme/acme-v02.api.letsencrypt.org/sites/web_name.domain_name.com/web_name.domain_name.com.crt;
        ssl_certificate_key /root/.caddy/acme/acme-v02.api.letsencrypt.org/sites/web_name.domain_name.com/web_name.domain_name.com.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        root         /usr/share/nginx/html;

        # 开启gzip
        gzip on;

        # 启用gzip压缩的最小文件,小于设置值的文件将不会压缩
        gzip_min_length 1k;

        # gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明
        gzip_comp_level 3;

        # 进行压缩的文件类型。javascript有多种形式。其中的值可以在 mime.types 文件中找到。
        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;

        # 是否在http header中添加Vary: Accept-Encoding,建议开启
        gzip_vary on;

        # 禁用IE 6 gzip
        gzip_disable "MSIE [1-6]\.";

        # 设置压缩所需要的缓冲区大小
        gzip_buffers 32 4k;

        # 设置gzip压缩针对的HTTP协议版本
        gzip_http_version 1.0;

        #后台服务配置,配置了这个location便可以通过http://域名/jeecg-boot/xxxx 访问
        location ^~ /jeecg-boot {
            proxy_pass              http://172.18.100.100:8080/jeecg-boot/;
            proxy_set_header        Host 172.18.100.100;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #设置Rest风格的API
        location ^~ /api {
            rewrite ^/(.*) /$1 break;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://cwbase/;
        }

        #解决Router(mode: 'history')模式下,刷新路由地址不能找到页面的问题
        location / {

            root   /usr/share/nginx/html;
            index  index.html index.htm;

            if (!-e $request_filename) {
                rewrite ^(.*)$ /index.html?s=$1 last;
                break;
            }
            # 缓存使用前面定义的内存区域
            proxy_cache web_cache;
            # 对于 200 和 304 的响应码进行缓存,过期时间为 1 分钟,这会覆盖前面定义的 10 分钟过期时间
            proxy_cache_valid 200 206 304 301 302 10d;
            # 设置缓存的 key
            proxy_cache_key  $scheme$host$request_uri;

            #设置超时时间
            expires 30d;
        }

        location ^~ /jeecg-boot/sys/common/view {
            alias   /root/jeecg/upFiles/;
            index  index.html index.htm;
        }

    }

}

 

 

API参考

HTTP TypeAPI URLComments
GET/Gets all REST APIs
GET/api/tableNameLists rows of table
POST/api/tableNameCreate a new row
PUT/api/tableNameReplaces existing row with new row
POST :fire:/api/tableName/bulkCreate multiple rows - send object array in request body
GET :fire:/api/tableName/bulkLists multiple rows - /api/tableName/bulk?_ids=1,2,3
DELETE :fire:/api/tableName/bulkDeletes multiple rows - /api/tableName/bulk?_ids=1,2,3
GET/api/tableName/:idRetrieves a row by primary key
PATCH/api/tableName/:idUpdates row element by primary key
DELETE/api/tableName/:idDelete a row by primary key
GET/api/tableName/findOneWorks as list but gets single record matching criteria
GET/api/tableName/countCount number of rows in a table
GET/api/tableName/distinctDistinct row(s) in table - /api/tableName/distinct?_fields=col1
GET/api/tableName/:id/existsTrue or false whether a row exists or not
GET/api/parentTable/:id/childTableGet list of child table rows with parent table foreign key
GET :fire:/api/tableName/aggregateAggregate results of numeric column(s)
GET :fire:/api/tableName/groupbyGroup by results of column(s)
GET :fire:/api/tableName/ugroupbyMultiple group by results using one call
GET /api/tableName/chartNumeric column distribution based on (min,max,step) or(step array) or (automagic)
GET :fire:/api/tableName/autochartSame as Chart but identifies which are numeric column automatically - gift for lazy while prototyping
GET /api/xjoinhandles join
GET /dynamicexecute dynamic mysql statements with params
GET :fire:/uploadupload single file
GET :fire:/uploadsupload multiple files
GET /downloaddownload a file
GET/api/tableName/describedescribe each table for its columns
GET/api/tablesget all tables in database
GET/_healthgets health of process and mysql -- details query params for more details
GET/_versiongets version of Xmysql, mysql, node
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值