推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
longmeier90
V2EX  ›  Python

公司的 web 项目怎么实现热更新,不需要停服就能实现发版

  •  
  •   longmeier90 · Jul 6, 2021 · 7114 views
    This topic created in 1807 days ago, the information mentioned may be changed or developed.
    之前用户少都是公司内部人使用,随便新增一个什么功能,直接就是拉一下代码,重启 web 服务也没问题。最近公司使用的人多了以后,发现这种方式不太适用啦。
    我就想有用什么方式改造一下能够实现热更新或者是发版不停服,让用户无感知发版。难道是启动两套服务,做个负载,一个一个的重启?没这样搞过,不知道这种模式会不会有什么坑之类的,或者有什么更好的实现方法没有。
    各位大佬给个建议!```
    Supplement 1  ·  Jul 7, 2021
    俺们公司项目目前是用的 linux+django+supervisor+gunicorn 架构
    24 replies    2021-07-07 19:51:18 +08:00
    chogath
        1
    chogath  
       Jul 6, 2021
    服务器部署可以用 pm2,集群可以靠谱部署 k8s 做滚动更新或者其他策略
    no1xsyzy
        2
    no1xsyzy  
       Jul 6, 2021
    我这边有一个就是直接起两个服务做负载均衡分别重启。

    当然也有只需要起一个服务并且在重启过程中阻塞保持现有链接的。
    gunicorn 的自动重载应当是支持这样操作的,但它可以作为攻击面,小心使用。

    更好当然是 k8s 及其轻量替代品,但就是轻量替代品可能都嫌重。
    arischow
        3
    arischow  
       Jul 6, 2021 via iPhone
    负载均衡,检查服务健康,注册 /反注册服务,可以用 Ansible 类似的工具做。

    我用 Kubernetes
    mkdir
        4
    mkdir  
       Jul 6, 2021
    来了来了,K8S 替代品 docker swarm,滚动发布,操作简单
    DeWjjj
        5
    DeWjjj  
    PRO
       Jul 6, 2021
    启用两套服务,然后主从数据库做交接。
    securityCoding
        6
    securityCoding  
       Jul 6, 2021
    k8s 正解,现在都是云服务器直接用云服务商的集群就好
    netwjx
        7
    netwjx  
       Jul 6, 2021
    前置 nginx, 后面放个集群, 至少 2 台
    手动切集群
    luoqeng
        8
    luoqeng  
       Jul 6, 2021
    web 都是无状态服务,没啥可热更新的
    akira
        9
    akira  
       Jul 6, 2021
    理论上只需要 3 步 :dodge
    1. 启动新服务
    2. 新流量切换到新服务
    3. 关闭旧服务
    myd
        10
    myd  
       Jul 6, 2021
    数据库的更新呢
    kingfalse
        11
    kingfalse  
       Jul 7, 2021 via Android
    rancher 一把梭子 over
    finian
        12
    finian  
       Jul 7, 2021   ❤️ 1
    搜 graceful shutdown
    vueli
        13
    vueli  
       Jul 7, 2021
    web 项目? 是前后端分离的还是怎么样的.
    longmeier90
        14
    longmeier90  
    OP
       Jul 7, 2021
    @vueli 嗯,前后端分离的项目
    lele88lala
        15
    lele88lala  
       Jul 7, 2021
    不好意思打扰了,你们都是怎么发帖子的,为什么我发不了呢?
    ClericPy
        16
    ClericPy  
       Jul 7, 2021
    以前 Gunicorn 用的 HUP 信号, 这算热更新么

    Reload the configuration
    The HUP signal can be used to reload the Gunicorn configuration on the fly. Sending a HUP signal will reload the configuration, start the new worker processes with a new configuration and gracefully shutdown older workers.

    现在一直 uvicorn 没找到类似的, 估计也要依赖 Gunicorn worker 来整
    hb751968840
        17
    hb751968840  
       Jul 7, 2021
    要有集群
    jorneyr
        18
    jorneyr  
       Jul 7, 2021
    轮流升级也有坑: 例如计算数值的 Api:
    旧版: 1+1 返回 3
    新版: 1+1 返回 2
    使用负载的时候,新旧版同时存在,同一个接口调用因为负载返回 2 个不同结果。
    binbin0915
        19
    binbin0915  
       Jul 7, 2021
    php 吧 随便替换...
    abersheeran
        20
    abersheeran  
       Jul 7, 2021
    @ClericPy uvicorn 没考虑过做这个功能,gunicorn+uvicorn worker 搭配就行了。
    ClericPy
        21
    ClericPy  
       Jul 7, 2021
    @abersheeran 是, 不过我很多时候懒得装 gunicorn, 所以自己 handle signal 然后外面用 systemd 守护, 或者直接用 uvicorn 自带的 reload. 就赌不会那么导致重启一刹那正好有个请求没接到...

    gunicorn 的 HUP 信号算热重启么, 会有几率丢请求么. 非后端不确定有没有什么靠谱点的办法, 一开始以为 nginx 会在程序重启的时候帮我 hold 住结果直接给人返回了 502... 前东家除了 HUP 信号, 还有几个操作就是靠的 nginx upstream 的负载均衡, 每次重启服务一台一台重启让坏节点自动被另一个节点接管
    abersheeran
        22
    abersheeran  
       Jul 7, 2021   ❤️ 1
    @ClericPy https://github.com/index-py/index.py/blob/master/indexpy/cli.py#L206 我也比较懒,直接封装了 gunicorn 和 uvicorn 的操作,对外暴露的使用方式一致。我每次用的时候只要改 index-cli [uvicorn|gunicorn] ....... 中间的部分就可以无缝切换了。
    ruanimal
        23
    ruanimal  
       Jul 7, 2021
    @ClericPy 还是 lvs 靠谱点
    YangWaleed
        24
    YangWaleed  
       Jul 7, 2021
    换 erlang 吧( doge
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5179 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 392ms · UTC 05:52 · PVG 13:52 · LAX 22:52 · JFK 01:52
    ♥ Do have faith in what you're doing.