Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
FROM openresty/openresty:1.15.8.1-1-centos

MAINTAINER "shenshuo<191715030@qq.com>"

ENV LANG en_US.UTF-8
# 同步时间
FROM openresty/openresty:1.15.8.3-2-centos
ENV LANG=en_US.UTF-8
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN yum install epel-release -y && yum install -y supervisor && yum clean all -y

COPY . /usr/local/openresty/nginx/
VOLUME /var/log/
VOLUME /usr/local/openresty/nginx/logs/
EXPOSE 80
CMD ["/usr/bin/openresty", "-g", "daemon off;"]
RUN mv /usr/local/openresty/nginx/supervisor_ops.conf /etc/supervisord.conf

EXPOSE 80 443

CMD ["/usr/bin/supervisord"]
#CMD ["/usr/bin/openresty", "-g", "daemon off;"]
128 changes: 67 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ API网关系统,是基于openresty + Lua开发的一套API网关系统,主要功

- 熔断 (未完成)

# 一、服务部署
#### openresty 编译安装


# 一、部署

## 传统部署

#### 1. openresty 编译安装
```
wget https://openresty.org/download/openresty-1.13.6.2.tar.gz
tar zxf openresty-1.13.6.2.tar.gz && cd openresty-1.13.6.2
Expand All @@ -24,40 +29,40 @@ ln -s /usr/local/openresty-1.13.6.2/ /usr/local/openresty
ln -s /usr/local/openresty/bin/resty /usr/bin/resty
```

#### yum安装
**或者yum安装openresty**

```bash
# yum部署
yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum install openresty
yum install openresty-resty
```
##### 代码部署
#### 2. 代码部署
```bash
\cp -arp api-gateway/* /usr/local/openresty/nginx/
cp -arp api-gateway/* /usr/local/openresty/nginx/
```

# 二、 修改配置
##### 文件 /usr/local/openresty/nginx/conf/nginx.conf
- 修改 resolver 172.16.0.21; 为resolver DNS服务器。
##### 文件 /usr/local/openresty/nginx/conf/conf.d/gw.conf
- 修改 lua_code_cache on; 线上环境设置为on
- 修改 server_name 为你的网关域名
##### 文件 /usr/local/openresty/nginx/lua/configs.lua
- token_secret 为你的令牌的密钥 和登录JWT 服务的key一致
- rewrite_cache_url 刷新权限到redis接口
- rewrite_cache_token 为获取权限的令牌
#### - login_url 当token 无效或者过期 跳转的登录页面
- limit_conf 并发 限制默认即可 如有需求下面有详细介绍
- rewrite_conf 注册API 下面有详解



# 三、使用配置,注册API
> 要接入API网关系统,则要先进行注册,注册方式如下:
#### 3.修改配置文件

​ a、配置文件configs.lua中的rewrite_conf
**文件 /usr/local/openresty/nginx/conf/nginx.conf**
- 修改 resolver 172.16.0.21; 为resolver DNS服务器。

**文件 /usr/local/openresty/nginx/conf/conf.d/gw.conf**
- 修改 lua_code_cache on; 线上环境设置为on
- 修改 server_name 为你的网关域名

**文件 /usr/local/openresty/nginx/lua/configs.lua**
- token_secret 为你的令牌的密钥 和登录JWT 服务的key一致
- rewrite_cache_url 刷新权限到redis接口
- rewrite_cache_token 为获取权限的令牌
- login_url 当token 无效或者过期 跳转的登录页面
- limit_conf 并发 限制默认即可 如有需求下面有详细介绍
- rewrite_conf 注册API 下面有详解


#### 4. 使用配置,注册API
> 要接入API网关系统,则要先进行注册,注册方式如下:
​ a、配置文件configs.lua中的rewrite_conf
​ b、POST注册接口(暂无)

注册示例如下:
Expand Down Expand Up @@ -130,7 +135,24 @@ accounts 做过处理 不用经过鉴权



# 四、API鉴权权限
## docker 部署

**配置修改参考上述内容**

```
#bulid镜像
docker build -t gateway_image .

#启动
#注意:环境变量文件env.sh 请从opendevops项目里获取
docker run -it -d --name codo-gateway --env-file env.sh -p 8888:80
```

**使用docker部署启动之后端口为8888,防止单机部署造成端口冲突。你可以改为任意一个与本机不冲突的端口**

> 环境变量文件包含了所有项目启动所需要的变量,单启动该容器只为测试该应用是否正常,若您想使用和测试完整的项目,建议通过opendevops项目的docker-compose文件来启动一个完整的项目。

# 二、API鉴权权限

在configs.lua文件中配置redis信息和刷新redis权限接口信息,此信息由【权限系统】提供

Expand All @@ -154,27 +176,27 @@ accounts 做过处理 不用经过鉴权
测试:

​ 首次访问 http://gw.opendevops.cn/mg/xxxx/ 会返回 401错误,表示未登路
http://gw.opendevops.cn/accounts/login/
使用post 模拟登录
```
{
"username":"ss",
"password":"shenshuo",
"dynamic":"010073"
}
```
http://gw.opendevops.cn/accounts/login/
使用post 模拟登录
```
{
"username":"ss",
"password":"shenshuo",
"dynamic":"010073"
}
```
​ 登录成功,再次访问进行uri鉴权,鉴权成功则如下:
http://gw.opendevops.cn/mg/v2/sysconfig/settings/STORAGE/
```
{
"code": 0,
"msg": "获取配置成功",
"data": {}
}
```
http://gw.opendevops.cn/mg/v2/sysconfig/settings/STORAGE/
```
{
"code": 0,
"msg": "获取配置成功",
"data": {}
}
```


# 、API限速
# 、API限速

在configs.lua文件中配置limit,配置示例如下

Expand Down Expand Up @@ -220,7 +242,7 @@ Write errors: 0



# 、日志记录
# 、日志记录

在configs.lua文件中配置log地址及redis channel

Expand All @@ -247,23 +269,7 @@ Reading messages... (press Ctrl-C to quit)
3) "{\"time\":\"2018-09-19 10:48:52\",\"uri\":\"\\/devops\\/api\\/v1.0\\/job\\/\",\"login_ip\":\"172.16.80.12\",\"method\":\"POST\"}"
```

# docker 部署

**配置修改参考上述内容**

```
#删除前端的配置文件
mv conf/conf.d/demo.conf conf/conf.d/demo.conf-bak

#bulid镜像
docker build . -t gateway_image

#启动
docker-compose up -d
```
**使用docker部署启动之后端口为8888,防止单机部署造成端口冲突,如果想修改端口请修改`docker-compose.yml`文件。**

**默认域名:`http://gw.opendevops.cn:8888` 如果需要修改域名请修改`conf/conf.d/gw.conf`文件。**

## License

Expand Down
28 changes: 0 additions & 28 deletions conf/conf.d/demo.conf

This file was deleted.

5 changes: 3 additions & 2 deletions conf/conf.d/gw.conf → conf/conf.d/gateway.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
server {
listen 80;
server_name gw.opendevops.cn;
server_name gateway.opendevops.cn;
lua_need_request_body on; # 开启获取body数据记录日志

access_log /usr/local/openresty/nginx/logs/gateway-access.log;
error_log /usr/local/openresty/nginx/logs/gateway-error.log;
location / {
### ws 支持
proxy_http_version 1.1;
Expand Down
13 changes: 2 additions & 11 deletions conf/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
user root;
worker_processes auto;
worker_rlimit_nofile 51200;
error_log logs/error.log;
events {
use epoll;
worker_connections 51024;
}
http {
#设置默认lua搜索路径
lua_package_path '$prefix/lua/?.lua;/blah/?.lua;;';
lua_code_cache on; #线上环境设置为on, off时可以热加载lua文件
lua_code_cache off; #线上环境设置为on, off时可以热加载lua文件
lua_shared_dict user_info 1m;
lua_shared_dict my_limit_conn_store 100m; #100M可以放1.6M个键值对
include mime.types; #代理静态文件

client_header_buffer_size 64k;
large_client_header_buffers 4 64k;

gzip on;
gzip_min_length 1k;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript;
gzip_vary on;
gzip_buffers 4 16k;
gzip_http_version 1.1;

init_by_lua_file lua/init_by_lua.lua; # nginx启动时就会执行
include ./conf.d/*.conf; # lua生成upstream
resolver 172.16.0.21; # 内部DNS
resolver 127.0.0.11; # 内部DNS服务器地址,这里设置的是容器里的地址,如果你不是使用的容器请修改
}
9 changes: 0 additions & 9 deletions docker-compose.yml

This file was deleted.

2 changes: 1 addition & 1 deletion lua/access_check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ limit_req.incoming() --限速,限制每秒请求数

-- 获取访问URI
local url_path_list = tools.split(ngx.var.request_uri, '/')
local svc_code = url_path_list[2] -- 去第二位
local svc_code = url_path_list[2] -- 取第二位
--
table.remove(url_path_list,1)
local real_new_uri = tools.list_to_str(url_path_list,'/')
Expand Down
Loading