最近在写下单功能实现,想要压测下。单纯压测本地很难达到效果,最好是像生产部署一样,基于实际情况进行压测。手头没那么机器的情况下,采用docker做实例化部署就可以了。
以部署下单服务为例,我需要把api服务打包,然后构建docker镜像,最后多实例部署。每次修改完毕代码手动改来改去挺花时间的。一度想要搭建gitlab-jenkins-k8s的方案:代码提交后自动部署到k8s. 但感觉有些复杂,最后选择基于docker-compose稍微改造来实现部署。
整体脚手架目录:
.
├── conf
│ ├── default.conf
│ └── my-order.conf
├── docker-compose.yml
├── Dockerfile
├── entrypoint.sh
└── lib
├── readme.md
└── simple-order-1.0-SNAPSHOT.jar
自己的测试代码编写完毕后,提交gitee. 在docker服务器上git pull, 然后执行mvn install,把打包好的jar包cp到lib目录。启动docker-compose up即可。
git pull和打包,后续可修改为脚本。就当前目标而言,已经满足我做自己服务压测部署了。
Dockerfile
FROM openjdk:8
COPY ./lib /usr/src/myapp
WORKDIR /usr/src/myapp
COPY entrypoint.sh .
ENV server_port 8080
ENTRYPOINT [ "/bin/bash","entrypoint.sh" ]
entrypoint.sh
#!/bin/bash
echo 'start' > a;
echo "cur port: $server_port" >> a;
# tail -f a;
java -jar *.jar --server.port=${server_port}
nginx order.conf
upstream order{
server myapp:8080;
server myapp2:8080;
}
server {
listen 8090;
server_name _;
#access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass http://order;
}
}
docker-compose.yml
version: '3'
services:
myapp:
build:
context: ./
image: ryan/myapp:8
environment:
- server_port=8080
ports:
- 8083:8080
cpu_count: 2
mem_limit: 4gb
# deploy:
# mode: replicated
# replicas: 2
# endpoint_mode: vip
myapp2:
image: ryan/myapp:8
environment:
- server_port=8080
ports:
- 8084:8080
cpu_count: 2
mem_limit: 4gb
depends_on:
- myapp
ng:
image: nginx
volumes:
- ./conf:/etc/nginx/conf.d
ports:
- "80:80"
- "8090:8090"
links:
- myapp
- myapp2
environment:
- NGINX_HOST=foobar.com
- NGINX_PORT=80
关注我的公众号
唯有不断学习方能改变!
-- Ryan Miao