Skip to content

Commit e2b9bfe

Browse files
authored
feat: 升级nginx版本,支持docker构建 (#15)
* feat:调整构建脚本,删除nginx代码 * 使用rm -f防止删除失败 * feat; 无需执行清理逻辑 * feat: 修改nginx限流的问题 * feat: 重新编写readme * feat: 支持容器镜像的构建
1 parent 82fc302 commit e2b9bfe

File tree

9 files changed

+194
-47
lines changed

9 files changed

+194
-47
lines changed

.github/workflows/docker.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: DockerImage
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
release:
9+
name: Release Polaris Docker Image
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
goos: [linux]
14+
goarch: [amd64]
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v2
18+
19+
- name: Get version
20+
id: get_version
21+
run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}
22+
23+
- name: Set up Docker Buildx
24+
uses: docker/setup-buildx-action@v1
25+
with:
26+
config-inline: |
27+
insecure-entitlements = [ "network.host" ]
28+
29+
- name: Log in to Docker Hub
30+
uses: docker/login-action@v1
31+
with:
32+
username: ${{ secrets.POLARIS_DOCKER_NAME }}
33+
password: ${{ secrets.POLARIS_DOCKER_PASSWORD }}
34+
35+
- name: Build Server
36+
id: build-server
37+
env:
38+
DOCKER_TAG: ${{ steps.get_version.outputs.VERSION }}
39+
run: |
40+
cd build
41+
./build_docker.sh ${DOCKER_TAG}

Dockerfile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
FROM debian:stable-slim
2+
3+
# install dependencies
4+
RUN set -ex \
5+
&& apt-get update \
6+
&& apt-get install --no-install-recommends --no-install-suggests -y \
7+
autoconf automake wget git libtool curl make gcc g++ unzip libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
8+
9+
COPY source /build/source
10+
COPY third_party /build/third_party
11+
COPY polaris.yaml /build/
12+
13+
# build polaris-cpp
14+
RUN set -ex \
15+
&& cd /build/third_party \
16+
&& git config --global http.sslverify false \
17+
&& git clone -b release_v1.1.0 https://github.com/polarismesh/polaris-cpp polaris-cpp \
18+
&& cd polaris-cpp \
19+
&& make \
20+
&& make package \
21+
&& tar xf polaris_cpp_sdk.tar.gz \
22+
&& mv polaris_cpp_sdk/* /build/third_party/polaris_client/
23+
24+
RUN set -ex \
25+
&& mkdir -p /server \
26+
&& cd /build/third_party \
27+
&& ngx_file_name=nginx-1.23.1 \
28+
&& curl http://nginx.org/download/"$ngx_file_name".tar.gz -o "$ngx_file_name".tar.gz \
29+
&& tar xf "$ngx_file_name".tar.gz \
30+
&& cp nginx/make "$ngx_file_name"/auto/ \
31+
&& chmod +x "$ngx_file_name"/configure \
32+
&& cd "$ngx_file_name" \
33+
&& ./configure --prefix=/server --add-module=../../source/nginx_polaris_limit_module --add-module=../polaris_client --with-stream --with-cpp=g++ \
34+
&& make
35+
36+
WORKDIR /server
37+
38+
CMD ["nginx", "-g", "daemon off;"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
3232

3333
用户也可以通过源码编译的方式,生成安装包。
3434

35-
- 安装依赖项:在编译之前,需要先安装依赖项。通过执行```yum install autoconf automake libtool curl make g++ unzip```进行安装。
35+
- 安装依赖项:在编译之前,需要先安装依赖项。通过执行```yum install autoconf automake libtool curl make gcc-c++ libstdc++-devel unzip```进行安装。
3636

3737
- 下载源码包:可以直接从[releases](https://github.com/polarismesh/nginx-gateway/releases)下载最新的nginx网关源码包。
3838

build/build.sh

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ echo "build nginx-gateway for version ${version}"
1010
pushd ../third_party
1111
rm -rf polaris-cpp
1212
git clone -b release_v1.1.0 https://github.com/polarismesh/polaris-cpp polaris-cpp
13-
rm -rf nginx-1.18.0
14-
curl http://nginx.org/download/nginx-1.18.0.tar.gz -o nginx-1.18.0.tar.gz
15-
tar xf nginx-1.18.0.tar.gz
16-
cp nginx/make nginx-1.18.0/auto/
13+
14+
ngx_file_name=nginx-1.23.1
15+
rm -rf "$ngx_file_name"
16+
curl http://nginx.org/download/"$ngx_file_name".tar.gz -o "$ngx_file_name".tar.gz
17+
tar xf "$ngx_file_name".tar.gz
18+
cp nginx/make "$ngx_file_name"/auto/
1719

1820
pushd polaris-cpp
1921
make
@@ -31,13 +33,15 @@ echo "target name $folder_name"
3133
rm -rf ../$folder_name
3234
mkdir -p ../$folder_name
3335

34-
pushd nginx-1.18.0/
36+
pushd "$ngx_file_name"/
3537
chmod +x configure
3638
./configure \
3739
--prefix=../../$folder_name \
38-
--add-module=../../source/nginx_polaris_limit_module \
39-
--add-module=../polaris_client \
40-
--with-stream
40+
--add-module=../../source/nginx_polaris_limit_module \
41+
--add-module=../polaris_client \
42+
--with-stream \
43+
--with-cpp=g++
44+
4145
make
4246
make install
4347
cp ../nginx/start.sh ../../$folder_name/sbin/start.sh

build/build_docker.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
if [ $# != 1 ]; then
4+
echo "e.g.: bash $0 v1.0"
5+
exit 1
6+
fi
7+
8+
docker_tag=$1
9+
10+
docker_repository="polarismesh"
11+
12+
echo "docker repository : ${docker_repository}/nginx, tag : ${docker_tag}"
13+
14+
arch_list=( "amd64" )
15+
platforms=""
16+
17+
for arch in ${arch_list[@]}; do
18+
platforms+="linux/${arch},"
19+
done
20+
21+
platforms=${platforms::-1}
22+
extra_tags=""
23+
24+
pre_release=`echo ${docker_tag}|egrep "(alpha|beta|rc|[T|t]est)"|wc -l`
25+
if [ ${pre_release} == 0 ]; then
26+
extra_tags="-t ${docker_repository}/nginx:latest"
27+
fi
28+
29+
cd ..
30+
docker buildx build --network=host -t ${docker_repository}/nginx:${docker_tag} ${extra_tags} --platform ${platforms} --push ./

build/make.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
set -e
44

5+
ngx_file_name=nginx-1.23.1
6+
57
pushd ../third_party
68
rm -rf polaris-cpp
79
git clone -b release_v1.1.0 https://github.com/polarismesh/polaris-cpp polaris-cpp
8-
rm -rf nginx-1.18.0
9-
curl http://nginx.org/download/nginx-1.18.0.tar.gz -o nginx-1.18.0.tar.gz
10-
tar xf nginx-1.18.0.tar.gz
10+
rm -rf "$ngx_file_name"
11+
curl http://nginx.org/download/"$ngx_file_name".tar.gz -o "$ngx_file_name".tar.gz
12+
tar xf "$ngx_file_name".tar.gz
13+
cp nginx/make "$ngx_file_name"/auto/
1114

1215
pushd polaris-cpp
1316
make
@@ -25,13 +28,14 @@ echo "target name $folder_name"
2528
rm -rf ../$folder_name
2629
mkdir -p ../$folder_name
2730

28-
pushd nginx-1.18.0/
31+
pushd "$ngx_file_name"/
2932
chmod +x configure
3033
./configure \
3134
--prefix=../../$folder_name \
3235
--add-module=../../source/nginx_polaris_limit_module \
3336
--add-module=../polaris_client \
34-
--with-stream
37+
--with-stream \
38+
--with-cpp=g++
3539
make
3640
popd
3741

source/nginx_polaris_limit_module/ngx_http_polaris_limit_module.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ static ngx_int_t ngx_http_polaris_limit_handler(ngx_http_request_t *r) {
8989
service_namespace_str = plcf->service_namespace;
9090
service_name_str =plcf->service_name;
9191

92+
polaris::LimitApi* limit_api = Limit_API_SINGLETON.GetLimitApi();
93+
if (NULL == limit_api) {
94+
return NGX_OK;
95+
}
9296
std::string service_namespace(reinterpret_cast<char *>(service_namespace_str.data), service_namespace_str.len);
9397
std::string service_name(reinterpret_cast<char *>(service_name_str.data), service_name_str.len);
9498
polaris::ServiceKey serviceKey = {service_namespace, service_name};
@@ -334,4 +338,48 @@ static void get_labels_from_request(ngx_http_request_t* r, const std::set<std::s
334338
}
335339
}
336340
}
341+
}
342+
343+
static bool endsWith(const std::string& str, const std::string& suffix)
344+
{
345+
return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
346+
}
347+
348+
static std::string get_polaris_conf_path() {
349+
char *cwd = get_current_dir_name();
350+
std::string dirname(cwd);
351+
free(cwd);
352+
if (endsWith(dirname, PATH_SBIN)) {
353+
dirname = dirname.substr(0, dirname.rfind(PATH_SBIN));
354+
} else {
355+
dirname += "/";
356+
}
357+
dirname += "conf/polaris.yaml";
358+
return dirname;
359+
}
360+
361+
static bool exist_file(const std::string& name) {
362+
std::ifstream file(name.c_str());
363+
return file.good();
364+
}
365+
366+
const std::string defaultConfigContent = R"##(
367+
global:
368+
serverConnector:
369+
addresses:
370+
- ${polaris_address}
371+
)##";
372+
373+
LimitApiWrapper::LimitApiWrapper() {
374+
std::string conf_path = get_polaris_conf_path();
375+
std::string err_msg;
376+
if (exist_file(conf_path)) {
377+
m_limit = polaris::LimitApi::CreateFromFile(conf_path, err_msg);
378+
} else {
379+
std::cout << "[polaris-limiter] config file " << conf_path << " not exists, create with default config" << std::endl;
380+
m_limit = polaris::LimitApi::CreateFromString(defaultConfigContent, err_msg);
381+
}
382+
if (NULL == m_limit) {
383+
std::cout << "[polaris-limiter] fail to create limit api, err: " << err_msg << std::endl;
384+
}
337385
}

source/nginx_polaris_limit_module/ngx_http_polaris_limit_module.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern "C" {
2424
#include <iostream>
2525
#include <string>
2626
#include <unistd.h>
27+
#include <fstream>
2728

2829
static const char KEY_NAMESPACE[] = "namespace=";
2930
static const uint32_t KEY_NAMESPACE_SIZE = sizeof(KEY_NAMESPACE) - 1;
@@ -38,27 +39,9 @@ static const std::string LABEL_KEY_QUERY = "$query.";
3839
static const std::string LABEL_KEY_CALLER_IP = "$caller_ip";
3940
static const std::string PATH_SBIN = "sbin";
4041

41-
static bool endsWith(const std::string& str, const std::string& suffix)
42-
{
43-
return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
44-
}
45-
46-
static std::string get_polaris_conf_path() {
47-
char *cwd = get_current_dir_name();
48-
std::string dirname(cwd);
49-
free(cwd);
50-
if (endsWith(dirname, PATH_SBIN)) {
51-
dirname = dirname.substr(0, dirname.rfind(PATH_SBIN));
52-
} else {
53-
dirname += "/";
54-
}
55-
dirname += "conf/polaris.yaml";
56-
return dirname;
57-
}
58-
5942
class LimitApiWrapper {
6043
public:
61-
LimitApiWrapper() { m_limit = polaris::LimitApi::CreateFromFile(get_polaris_conf_path()); }
44+
LimitApiWrapper();
6245

6346
static LimitApiWrapper& Instance() {
6447
static LimitApiWrapper limit_api;

third_party/nginx/make

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ cat << END > $NGX_MAKEFILE
2222

2323
CC = $CC
2424
CFLAGS = $CFLAGS
25-
CPP = g++
26-
CXX = g++
27-
CXXFLAGS = -std=c++11 -Wall
28-
LINK = g++
25+
CPP = $CPP
26+
CPPFLAGS = -std=c++11 -Wall
27+
LINK = $LINK
2928

3029
END
3130

@@ -315,7 +314,7 @@ $ngx_obj: \$(CORE_DEPS) \$(HTTP_DEPS)$ngx_cont$ngx_src
315314
END
316315

317316
fi
318-
done
317+
done
319318

320319
fi
321320

@@ -345,7 +344,7 @@ $ngx_obj: \$(CORE_DEPS) \$(MAIL_DEPS)$ngx_cont$ngx_src
345344
$ngx_cc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src$NGX_AUX
346345

347346
END
348-
done
347+
done
349348

350349
fi
351350

@@ -375,7 +374,7 @@ $ngx_obj: \$(CORE_DEPS) \$(STREAM_DEPS)$ngx_cont$ngx_src
375374
$ngx_cc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src$NGX_AUX
376375

377376
END
378-
done
377+
done
379378

380379
fi
381380

@@ -401,7 +400,7 @@ $ngx_obj: \$(CORE_DEPS) $ngx_cont$ngx_src
401400
$ngx_cc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src$NGX_AUX
402401

403402
END
404-
done
403+
done
405404

406405
fi
407406

@@ -410,9 +409,8 @@ fi
410409

411410
if test -n "$NGX_ADDON_SRCS"; then
412411

413-
ngx_cc="\$(CPP) $ngx_compile_opt -std=c++11 -g -O0 \$(CFLAGS) $ngx_use_pch \$(ALL_INCS)"
414-
ngx_cxx="\$(CXX) $ngx_compile_opt \$(CXXFLAGS) $ngx_use_pch ddons sources\$(ALL_INCS)"
415-
412+
ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) $ngx_use_pch \$(ALL_INCS)"
413+
ngx_cpp="\$(CPP) $ngx_compile_opt \$(CPPFLAGS) $ngx_use_pch \$(ALL_INCS)"
416414
for ngx_src in $NGX_ADDON_SRCS
417415
do
418416
ngx_obj="addon/`basename \`dirname $ngx_src\``"
@@ -431,7 +429,7 @@ if test -n "$NGX_ADDON_SRCS"; then
431429
ext=`echo ${ngx_src} | cut -d . -f 2`
432430
ngx_gcc=$ngx_cc
433431
if [ $ext = "cpp" ]; then
434-
ngx_gcc=$ngc_cxx
432+
ngx_gcc=$ngx_cpp
435433
fi
436434

437435
cat << END >> $NGX_MAKEFILE
@@ -440,7 +438,7 @@ $ngx_obj: \$(ADDON_DEPS)$ngx_cont$ngx_src
440438
$ngx_gcc$ngx_tab$ngx_objout$ngx_obj$ngx_tab$ngx_src$NGX_AUX
441439

442440
END
443-
done
441+
done
444442

445443
fi
446444

@@ -511,6 +509,7 @@ fi
511509
for ngx_module in $DYNAMIC_MODULES
512510
do
513511
eval ngx_module_srcs="\$${ngx_module}_SRCS"
512+
eval ngx_module_shrd="\$${ngx_module}_SHRD"
514513
eval eval ngx_module_libs="\\\"\$${ngx_module}_LIBS\\\""
515514

516515
eval ngx_module_modules="\$${ngx_module}_MODULES"
@@ -576,7 +575,7 @@ END
576575
| sed -e "s/\(.*\.\)c/\1$ngx_objext/"`
577576

578577
ngx_module_objs=
579-
for ngx_src in $ngx_module_srcs
578+
for ngx_src in $ngx_module_srcs $ngx_module_shrd
580579
do
581580
case "$ngx_src" in
582581
src/*)

0 commit comments

Comments
 (0)