CentOS发布.netcore应用
一、使用 service-unit 发布
1、.netcore环境安装
- 执行命令
sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
- 更新yum
sudo yum update
- 安装.netcore
sudo yum install dotnet-sdk-2.2 -y
- 检查安装结果
dotnet --version
2、发布.netcore应用
- cd到/srv/www/目录下
cd /srv/www
- 创建发布目录
mkdir wgmes_webaip
- 使用ftp客户端上传文件到该目录下
- 使用
dotnet
命令启动应用
dotnet WGMes.WebApi.dll --server.urls "http://*:18001"
其中
--server.urls "http://*18001"
指定启动的端口,默认端口为5000。
且host名为localhost(localhost只能本机访问)。
- 阿里云后台管理开放18001端口
- 测试访问
3、设置.netcore应用开机自启动
- 创建服务文件
vim /etc/systemd/system/wgmes_webapi.service
写入以下内容:
[Unit]
Description=wgmes_webapi for centos7
[Service]
WorkingDirectory=/srv/www/wgmes_webapi
ExecStart=/usr/bin/dotnet /srv/www/wgmes_webapi/WGMes.WebApi.dll
Restart=always
RestartSec=10 # Restart service after 10 seconds if dotnet service crashes
SyslogIdentifier=dotnet-wgmes_webapi
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=ASPNETCORE_URLS=http://*:18001
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
- 设置开机自启动服务
systemctl enable wgmes_webapi.service
- 开启服务
systemctl start wgmes_webapi
- 查看服务状态
systemctl status wgmes_webapi
4、外网访问测试
- 外网访问测试
二、单个docker容器运行
1、开发机编译代码,发布容器镜像到镜像仓库;
2、服务器从镜像仓库拉取镜像并运行容器
docker run \
-e "ASPNETCORE_ENVIRONMENT=Production" \
-e "DATABASE_HOST=47.115.186.28" \
-e "DATABASE_ID=123456" \
-e "DATABASE_USER=sa" \
-e "DATABASE_PWD=123456" \
-e "PRINT_URL=http://print.ywlin.cn" \
--name wgmesapi \
--restart=always \
-p 18001:80 \
-d registry.cn-shenzhen.aliyuncs.com/wgmes/webapi:v1.9
三、docker-compose 容器编码发布流程
1、容器编排
- 编写docker-compose.yml文件
2、提交代码
- 开发机提交代码(git commit、git push)
- 合并发布代码到master分支
3、服务器发布
服务器拉取代码(首次拉取使用
git clone
)shell cd /home git clone https://gitee.com/lyzcren/WGMes.WebApi.git
服务器拉取代码(非首次使用
git pull
)shell cd /home/WGMes.WebApi/src/ git pull origin master
服务器运行
docker-compose up
命令shell cd /home/WGMes.WebApi/src/ docker-compose up --force-recreate --build -d
4、查看发布结果
- 使用
docker ps
查看容器运行状态shell docker ps docker ps -a
四、编写shell脚本实现发布流程
1、编写shell脚本
install.sh```shell
! /bin/sh
echo “开始安装金大Mes” if [ ! -d “/src/kd” ];then echo “创建目录/src/kd/“ mkdir -p /src/kd fi cd /src/kd/
写入nginx.config文件
cat>>nginx.config<<EOF server { listen 80;
gzip config
gzip on; gzip_min_length 1k; gzip_comp_level 9; gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml; gzip_vary on; gzip_disable “MSIE [1-6].“;
root /usr/share/nginx/html;
location / {
try_files $uri $uri/ /index.html;
} location /api {
proxy_pass http://47.115.186.28:18016;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
location /hub {
proxy_pass http://47.115.186.28:18016;
proxy_set_header Host $host:$server_port;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
} } EOF
写入docker-compose.yml文件
cat>>docker-compose.yml<<EOF version: “3.8”
services: wgmes.api: container_name: kd_mes_api image: ${DOCKER_REGISTRY-}wgmesapi build: context: ./Mes.KD.Api/src/ dockerfile: WGMes.Api/Dockerfile
# restart: unless-stopped
deploy:
replicas: 2
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
environment:
- ASPNETCORE_ENVIRONMENT=Development
- TZ=Asia/Shanghai
- CONNECTION_STRING=Data Source=192.168.0.7;Initial Catalog=mes_kd;Persist Security Info=True;User ID=sa;Password=123456
- REDIS_CONNECTION_STRING=kd_mes_redis:6379,defaultDatabase=0
- RABBITMQ_HOST_NAME=kd_mes_rabbitmq
- RABBITMQ_VHOST=/
- RABBITMQ_USER=wgmes
- RABBITMQ_PASS=123456
# ports:
# - 8080:80
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
redis: container_name: kd_mes_redis image: redis restart: unless-stopped
rabbitmq: container_name: kd_mes_rabbitmq hostname: kd_mes_rabbitmq image: “rabbitmq:3.7.16-management” environment: RABBITMQ_DEFAULT_VHOST: “/“ RABBITMQ_DEFAULT_USER: “wgmes” RABBITMQ_DEFAULT_PASS: “123456” restart: unless-stopped
ant-design-pro_build: build: ./Mes.KD.Antd container_name: “kd-mes-frontend_build” volumes:
- dist:/usr/src/app/dist
ant-design-pro_web: image: nginx container_name: “kd-mes-frontend_web”
# ports:
# - 80:80
restart: unless-stopped
volumes:
- dist:/usr/share/nginx/html:ro
- ./nginx.conf:/etc/nginx/conf.d/default.conf
volumes: dist: EOF
写入docker-compose.override.yml文件
cat>>docker-compose.override.yml<<EOF version: ‘3.8’
services: wgmes.api: environment:
- ASPNETCORE_ENVIRONMENT=Production
- TZ=Asia/Shanghai
- CONNECTION_STRING=Data Source=47.115.186.28;Initial Catalog=mes_kd;Persist Security Info=True;User ID=sa;Password=123456
ports:
- 18016:80
redis:
# ports:
# - 6379:6379
rabbitmq:
# ports:
# - 15672:15672
# - 25672:25672
# - 4369:4369
# - 5672:5672
ant-design-pro_web: ports:
- 18015:80
EOF git clone https://gitee.com/lyzcren/Mes.KD.Antd.git git clone https://gitee.com/lyzcren/Mes.KD.Api.git cd /src/kd/ docker-compose up —force-recreate —build -d echo “更新完成”
- update.sh```shell
#! /bin/sh
echo "开始自动化更新金大Mes"
cd /source/kd/Mes.KD.Antd/
git pull origin master
cd /source/kd/Mes.KD.Api/
git pull origin master
cd /source/kd/
docker-compose up --force-recreate --build -d
echo "更新完成"
2、授予脚本执行权限
chmod +x install.sh update.sh
3、首次安装服务
sh install.sh
4、后续更新服务
- 1)提交代码
- 2)提交pr到master分支
- 3)审核pr
- 4)进入服务器,执行:
shell sh update.sh
修改git记住密码的配置(1分钟)
git config --global credential.helper 'cache --timeout=300'
修改git记住密码的配置(永久)
git config --global credential.helper store