CentOS发布.netcore应用

一、使用 service-unit 发布

1、.netcore环境安装

  • 执行命令
  1. sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
  • 更新yum
  1. sudo yum update
  • 安装.netcore
  1. sudo yum install dotnet-sdk-2.2 -y
  • 检查安装结果
  1. dotnet --version

2、发布.netcore应用

  • cd到/srv/www/目录下
  1. cd /srv/www
  • 创建发布目录
  1. mkdir wgmes_webaip
  • 使用ftp客户端上传文件到该目录下
  • 使用dotnet命令启动应用
  1. dotnet WGMes.WebApi.dll --server.urls "http://*:18001"

其中--server.urls "http://*18001"指定启动的端口,默认端口为5000。
且host名为localhost(localhost只能本机访问)。

  • 阿里云后台管理开放18001端口
  • 测试访问

3、设置.netcore应用开机自启动

  • 创建服务文件
  1. vim /etc/systemd/system/wgmes_webapi.service
  1. 写入以下内容:
  1. [Unit]
  2. Description=wgmes_webapi for centos7
  3. [Service]
  4. WorkingDirectory=/srv/www/wgmes_webapi
  5. ExecStart=/usr/bin/dotnet /srv/www/wgmes_webapi/WGMes.WebApi.dll
  6. Restart=always
  7. RestartSec=10 # Restart service after 10 seconds if dotnet service crashes
  8. SyslogIdentifier=dotnet-wgmes_webapi
  9. User=root
  10. Environment=ASPNETCORE_ENVIRONMENT=Production
  11. Environment=ASPNETCORE_URLS=http://*:18001
  12. Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
  13. [Install]
  14. WantedBy=multi-user.target
  • 设置开机自启动服务
  1. systemctl enable wgmes_webapi.service
  • 开启服务
  1. systemctl start wgmes_webapi
  • 查看服务状态
  1. systemctl status wgmes_webapi

4、外网访问测试

  • 外网访问测试

二、单个docker容器运行

1、开发机编译代码,发布容器镜像到镜像仓库;

2、服务器从镜像仓库拉取镜像并运行容器

  1. docker run \
  2. -e "ASPNETCORE_ENVIRONMENT=Production" \
  3. -e "DATABASE_HOST=47.115.186.28" \
  4. -e "DATABASE_ID=123456" \
  5. -e "DATABASE_USER=sa" \
  6. -e "DATABASE_PWD=123456" \
  7. -e "PRINT_URL=http://print.ywlin.cn" \
  8. --name wgmesapi \
  9. --restart=always \
  10. -p 18001:80 \
  11. -d registry.cn-shenzhen.aliyuncs.com/wgmes/webapi:v1.9

三、docker-compose 容器编码发布流程

1、容器编排

  • 编写docker-compose.yml文件

2、提交代码

  • 开发机提交代码(git commit、git push)
  • 合并发布代码到master分支

3、服务器发布

  • 服务器拉取代码(首次拉取使用 git cloneshell cd /home git clone https://gitee.com/lyzcren/WGMes.WebApi.git

  • 服务器拉取代码(非首次使用 git pullshell 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 / {

    1. try_files $uri $uri/ /index.html;

    } location /api {

    1. proxy_pass http://47.115.186.28:18016;
    2. proxy_set_header X-Forwarded-Proto $scheme;
    3. proxy_set_header Host $http_host;
    4. proxy_set_header X-Real-IP $remote_addr;

    }

    location /hub {

    1. proxy_pass http://47.115.186.28:18016;
    2. proxy_set_header Host $host:$server_port;
    3. proxy_http_version 1.1;
    4. proxy_set_header Upgrade $http_upgrade;
    5. 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

  1. # restart: unless-stopped
  2. deploy:
  3. replicas: 2
  4. update_config:
  5. parallelism: 2
  6. delay: 10s
  7. restart_policy:
  8. condition: on-failure
  9. environment:
  10. - ASPNETCORE_ENVIRONMENT=Development
  11. - TZ=Asia/Shanghai
  12. - CONNECTION_STRING=Data Source=192.168.0.7;Initial Catalog=mes_kd;Persist Security Info=True;User ID=sa;Password=123456
  13. - REDIS_CONNECTION_STRING=kd_mes_redis:6379,defaultDatabase=0
  14. - RABBITMQ_HOST_NAME=kd_mes_rabbitmq
  15. - RABBITMQ_VHOST=/
  16. - RABBITMQ_USER=wgmes
  17. - RABBITMQ_PASS=123456
  18. # ports:
  19. # - 8080:80
  20. volumes:
  21. - ${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:

  1. - dist:/usr/src/app/dist

ant-design-pro_web: image: nginx container_name: “kd-mes-frontend_web”

  1. # ports:
  2. # - 80:80
  3. restart: unless-stopped
  4. volumes:
  5. - dist:/usr/share/nginx/html:ro
  6. - ./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:

  1. - ASPNETCORE_ENVIRONMENT=Production
  2. - TZ=Asia/Shanghai
  3. - CONNECTION_STRING=Data Source=47.115.186.28;Initial Catalog=mes_kd;Persist Security Info=True;User ID=sa;Password=123456
  4. ports:
  5. - 18016:80

redis:

  1. # ports:
  2. # - 6379:6379

rabbitmq:

  1. # ports:
  2. # - 15672:15672
  3. # - 25672:25672
  4. # - 4369:4369
  5. # - 5672:5672

ant-design-pro_web: ports:

  1. - 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 “更新完成”

  1. - update.sh```shell
  2. #! /bin/sh
  3. echo "开始自动化更新金大Mes"
  4. cd /source/kd/Mes.KD.Antd/
  5. git pull origin master
  6. cd /source/kd/Mes.KD.Api/
  7. git pull origin master
  8. cd /source/kd/
  9. docker-compose up --force-recreate --build -d
  10. echo "更新完成"

2、授予脚本执行权限

  1. chmod +x install.sh update.sh

3、首次安装服务

  1. sh install.sh

4、后续更新服务

  • 1)提交代码
  • 2)提交pr到master分支
  • 3)审核pr
  • 4)进入服务器,执行:shell sh update.sh
  1. 修改git记住密码的配置(1分钟)
  2. git config --global credential.helper 'cache --timeout=300'
  3. 修改git记住密码的配置(永久)
  4. git config --global credential.helper store