热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

基于FlaskAngular的项目组网架构与部署

基于Raindrop网,分享项目的组网架构和部署。项目组网架构架构说明1.流项目访问分为两个流,通过nginx分两个端口暴露给外部使用:数据流:用户访问Raindrop网站。控制流

基于Raindrop网,分享项目的组网架构和部署。

项目组网架构

《基于Flask-Angular的项目组网架构与部署》

架构说明

1.流

项目访问分为两个流,通过nginx分两个端口暴露给外部使用:
数据流:用户访问Raindrop网站。
控制流:管理人员通过supervisor监控、管理服务器进程。

图中除了将程序部署在ECS上外,还使用了OSS(对象存储服务,可以理解成一个nosql数据库),主要是为了存放一些静态文件,提高访问速度。阿里的OSS还可以与CDN一起使用,同样可以提高访问速度。

2.nginx

通过nginx对外暴露两个端口,如上所述,80端口供用户访问网站,另一个端口供管理人员使用。
80端口:根据请求的url配置了方向代理,分别导向client(angular)和server(flask).
其中server通过gunicorn部署在[socket]localhost:10000
配置如下:

server {
listen 80 default_server;
# set client body size to 4M (add by dh) #
client_max_body_size 4M;
gzip_types text/plain text/css application/json application/Javascript text/xml application/xml application/xml+rss text/Javascript;
# product
root /home/raindrop/www/client/dist;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
} # access flask static folder
location /static/ {
# product
root /home/raindrop/www/server/app;
}
location /api/ {
proxy_pass http://localhost:10000/api/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# Error pages
error_page 413 @413_json;
location @413_json {
default_type application/json;
return 200 '{"msg": "Request Entity Too Large(max=4M)"}';
}
}

3.gunicorn

gunicorn作为wsgi容器,用来执行flask server。
gunicorn可以使用异步socket:gevent,其本质是基于greenlet实现协程的第三方库,改善io阻塞问题,通过简单的配置就能使程序获得极高的并发处理能力。

注意:使用gunicorn != 使用gevent,如要开启gevent socket,启动gunicorn时需要增加–work-class参数,如下:

gunicorn –workers=4 –worker-class socketio.sgunicorn.GeventSocketIOWorker -b localhost:10000 wsgi:app

除了gunicorn,也可以选用uwsgi,但是有两点限制需要注意:
1.如果使用了Flask-socketio,请不要使用uwsgi,原因

Note regarding uWSGI: While this server has support for gevent and WebSocket,
there is no way to use the custom event loop needed by gevent-socketio,
so there is no directly available method for hosting Flask-SocketIO applications on it.
If you figure out how to do this please let me know!

2.如果使用异步WSGI Server,请勿使用uwsgi,原因:慎用异步 WSGI Server 运行 Flask 应用

Flask(Werkzeug)的Local Thread无法与uwsgi基于uGreen的微线程兼容,引起Local Thread工作混乱。

4.celery

在程序运行过程中会有一些比较耗时但并非紧急的工作,这些任务可以使用异步任务来处理,提高server的响应速度。
celery – Distributed Task Queue是一个第三方库,提供异步队列和任务处理功能。
Raindrop使用celery配合实现feed功能。通过flask进行配置,使用redis作为异步队列来存储任务,并将处理结果(feed activity)存储在redis中。

5.supervisor

如上所述,我们需要在服务器上运行gunicorn和celery两个进程。
显然,我们需要一个monitor来帮我们管理这两个进程,避免进程崩溃不能及时拉起,阻塞业务。
supervisor:Supervisor process control system for UNIX是一个开源monitor程序,并且内置了web管理功能,可以远程监控、重启进程。配置如下:

[inet_http_server]
# web 管理端口,通过nginx暴露给远端用户
port=127.0.0.1:51000
[program:raindrop]
# 程序启动命令
command = gunicorn --workers=4 --worker-class socketio.sgunicorn.GeventSocketIOWorker -b localhost:10000 wsgi:app
directory = /home/raindrop/www/server
user = dh
stopwaitsecs=60
stdout_logfile = /var/log/raindrop/supervisor-raindrop.log
redirect_stderr = true
[program:celery]
command = celery -P gevent -A wsgi.celery worker
directory = /home/raindrop/www/server
user = dh
stopwaitsecs=60
stdout_logfile = /var/log/raindrop/supervisor-celery.log
redirect_stderr = true

6.数据库

mysql:网站主要数据存储在关系型数据库中
redis:缓存mysql数据 + celery异步队列

服务器

使用阿里云的服务器
ECS:部署服务端程序
OSS:存储前端静态文件(速度很快,对前端体验改善很大,采用angular框架强烈推荐使用)

组件

nginx: HTTP Server
angularjs: client框架
flask:server框架
gunicorn: web 容器
celery: 异步任务处理
supervisor: monitor
redis: 数据缓存 + 任务队列
mysql: 数据库

本地开发环境与工具

ubuntu12.04 64: 操作系统
virtualenv: python虚拟环境(隔离项目开发环境,不用担心包冲突了)
vagrant: 基于virtualbox的本地虚拟环境(环境可以导入导出,团队开发必备)
gulp: angular工程打包工具
pip: python包管理工具
fabric: 基于python的远程脚本(自动化部署神器)

打包部署

基于Ubuntu 12.0.4,其它系统安装命令(apt-get 等)请自行修改。

繁琐VS自动化

《基于Flask-Angular的项目组网架构与部署》

  1. 如果是第一次部署,需要初始化ECS服务器,安装基本工具:
    nginx, supervisor, redis, mysql, pip, virtualenv

  2. 打包项目代码

  3. 发布静态文件到OSS服务器

  4. 发布项目代码到ECS服务器,安装server依赖的包

  5. 修改mysql, nginx, supervisor配置文件

  6. 拉起所需进程

然后各种apt-get install, scp, tar, cp, mv,不得不说,这是一个烦人且毫无技术含量的工作,干过几次后基本就可以摔键盘了。
不过,有繁琐的地方,一定有自动化。其实完成上面这些工作,三条命令足以:

fab init_env
fab build
fab deploy

这要感谢Fabric:Simple, Pythonic remote execution and deployment项目,封装了非常简洁的远程操作命令。

Fabric

使用Fabric,只需要编写一个fabile.py脚本,在启动定义init_env, build, deploy三个任务:

# -*- coding: utf-8 -*-
import os, re, hashlib
from termcolor import colored
from datetime import datetime
from fabric.api import *
from fabric.contrib.files import exists
class FabricException(Exception):
pass
env.abort_exception = FabricException
# 服务器地址,可以有多个,依次部署:
env.hosts = [
'user@120.1.1.1'
]
env.passwords = {
'user@120.1.1.1:22':'123456'
}
# sudo用户为root:
env.sudo_user = 'root'
# mysql
db_user = 'root'
db_password = '123456'
_TAR_FILE = 'raindrop.tar.gz'
_REMOTE_TMP_DIR = '/tmp'
_REMOTE_BASE_DIR = '/home/raindrop'
_ALIYUN_OSS = {
'endpoint' : 'oss-cn-qingdao.aliyuncs.com',
'bucket' : 'yourbucketname',
'accessKeyId' : 'youraccessKeyId' ,
'accessKeySecret': 'youraccessKeySecret'
}
def build():
'''
必须先打包编译client,再打包整个项目
'''
with lcd(os.path.join(os.path.abspath('.'), 'client')):
local('gulp build') # 上传静态文件到oss服务器,并修改index.html中对静态文件的引用
with lcd(os.path.join(os.path.abspath('.'), 'client/dist')):
with lcd('scripts'):
for file in _list_dir('./'):
if oss_put_object_from_file(file, local('pwd', capture=True) + '/' + file):
_cdnify('../index.html', file)
with lcd('styles'):
for file in _list_dir('./'):
if oss_put_object_from_file(file, local('pwd', capture=True) + '/' + file):
_cdnify('../index.html', file)

# 注意在oss上配置跨域规则,否则fonts文件无法加载
# !!修改fonts文件夹请放开此段程序!!
# with lcd('fonts'):
# for file in _list_dir('./'):
# oss_put_object_from_file('fonts/%s' % file, local('pwd', capture=True) + '/' + file)
with lcd(os.path.join(os.path.abspath('.'), 'server')):
local('pip freeze > requirements/common.txt')
excludes = ['oss', 'distribute']
[local('sed -i -r -e \'/^.*' + exclude + '.*$/d\' "requirements/common.txt"') for exclude in excludes]
local('python setup.py sdist')
# e.g command: fab deploy:'',Fasle
# 注意命令两个参数间不要加空格
def deploy(archive='', needPut='True'):
if archive is '':
filename = '%s.tar.gz' % local('python setup.py --fullname', capture=True).strip()
archive = 'dist/%s' % filename
else:
filename = archive.split('/')[-1] tmp_tar = '%s/%s' % (_REMOTE_TMP_DIR, filename)
if eval(needPut):
# 删除已有的tar文件:
run('rm -f %s' % tmp_tar)
# 上传新的tar文件:
put(archive, _REMOTE_TMP_DIR)
# 创建新目录:
newdir = 'raindrop-%s' % datetime.now().strftime('%y-%m-%d_%H.%M.%S')
with cd(_REMOTE_BASE_DIR):
sudo('mkdir %s' % newdir)

# 重置项目软链接:
with cd(_REMOTE_BASE_DIR):
# 解压到新目录:
with cd(newdir):
sudo('tar -xzvf %s --strip-compOnents=1' % tmp_tar)
# 保存上传文件
if exists('www/server/app/static/upload/images/', use_sudo=True):
sudo('cp www/server/app/static/upload/images/ %s/server/app/static/upload/ -r' % newdir)
sudo('rm -f www')
sudo('ln -s %s www' % newdir)
with cd(_REMOTE_BASE_DIR):
with prefix('source %s/env/local/bin/activate' % _REMOTE_BASE_DIR):
sudo('pip install -r www/server/requirements/common.txt')
# 启动服务
with cd('www'):
# mysql
sudo('cp etc/my.cnf /etc/mysql/')
sudo('restart mysql')
# monitor
sudo('cp etc/rd_super.conf /etc/supervisor/conf.d/')
sudo('supervisorctl stop celery')
sudo('supervisorctl stop raindrop')
sudo('supervisorctl reload')
sudo('supervisorctl start celery')
sudo('supervisorctl start raindrop')

# nginx
sudo('cp etc/rd_nginx.conf /etc/nginx/sites-available/')
# ln -f —-如果要建立的链接名已经存在,则删除之
sudo('ln -sf /etc/nginx/sites-available/rd_nginx.conf /etc/nginx/sites-enabled/default')
sudo('nginx -s reload')
def init_env():
sudo('aptitude update')
sudo('aptitude safe-upgrade')
# sudo('apt-get install nginx')
sudo('aptitude install python-software-properties')
sudo('add-apt-repository ppa:nginx/stable')
sudo('aptitude update')
sudo('apt-get install nginx')
sudo('apt-get install supervisor')
sudo('apt-get install redis-server')
sudo('apt-get install mysql-server')
sudo('apt-get install python-pip python-dev build-essential')
sudo('pip install virtualenv')
run('mkdir /var/log/raindrop -p')
with cd ('/home/raindrop'):
sudo('virtualenv env')
def oss_put_object_from_file(key, file_path):
from oss.oss_api import *
oss = OssAPI(_ALIYUN_OSS['endpoint'], _ALIYUN_OSS['accessKeyId'], _ALIYUN_OSS['accessKeySecret'])
res = oss.put_object_from_file(_ALIYUN_OSS['bucket'], key, file_path)
return res.status == 200 and True or False
def _expand_path(path):
print path
return '"$(echo %s)"' % path
def sed(filename, before, after, limit='', backup='.bak', flags=''):
# Characters to be escaped in both
for char in "/'":
before = before.replace(char, r'\%s' % char)
after = after.replace(char, r'\%s' % char)
# Characters to be escaped in replacement only (they're useful in regexen
# in the 'before' part)
for char in "()":
after = after.replace(char, r'\%s' % char)
if limit:
limit = r'/%s/ ' % limit
cOntext= {
'script': r"'%ss/%s/%s/%sg'" % (limit, before, after, flags),
'filename': _expand_path(filename),
'backup': backup
}
# Test the OS because of differences between sed versions
with hide('running', 'stdout'):
platform = local("uname")
if platform in ('NetBSD', 'OpenBSD', 'QNX'):
# Attempt to protect against failures/collisions
hasher = hashlib.sha1()
hasher.update(env.host_string)
hasher.update(filename)
context['tmp'] = "/tmp/%s" % hasher.hexdigest()
# Use temp file to work around lack of -i
expr = r"""cp -p %(filename)s %(tmp)s \
&& sed -r -e %(script)s %(filename)s > %(tmp)s \
&& cp -p %(filename)s %(filename)s%(backup)s \
&& mv %(tmp)s %(filename)s"""
else:
context['extended_regex'] = '-E' if platform == 'Darwin' else '-r'
expr = r"sed -i%(backup)s %(extended_regex)s -e %(script)s %(filename)s"
command = expr % context
return local(command)
def _cdnify(index_file, cdn_file):
sed(index_file, '([^<]*(src|href)=")[^<]*' + cdn_file + '"', '\\1http://' + _ALIYUN_OSS['bucket'] + '.' + _ALIYUN_OSS['endpoint'] + '/' + cdn_file + '"')
def _list_dir(dir=None, access=lcd, excute=local):
"""docstring for list_dir"""
if dir is None:
return []
with access(dir):
string = excute("for i in *; do echo $i; done", capture=True)
files = string.replace("\r","").split("\n")
return files

通过fabric自动化部署有两点需要注意:
1.安装mysql时,设置的密码不能生效,需要登到服务器上手动设置一下:

mysql -u root
use mysql;
update user set password=PASSWORD('123456') where User='root';
flush privileges;
quit;

2.服务器上的用户需要自己手动创建。

gulp

在build任务中,使用gulp build打包client的angular代码。
这里先不多说,有需要请参考github项目generator-gulp-angular

setup

setup是python自己的打包工具。
build任务中,最后使用python setup.py sdist命令把整个工程打包成一个tar.gz文件,上传到服务器。
使用这个工具需要编写如下两个文件:
setup.py:打包脚本

#!/usr/bin/env python
from setuptools import setup, find_packages
import server
try:
long_description = open('README.md').read()
except:
long_description = server.__description__
REQUIREMENTS = []
exclude_lib = ['oss', 'distribute']
for lib in open("server/requirements/common.txt").readlines():
for exclude in exclude_lib:
if lib.lower() not in exclude:
REQUIREMENTS.append(lib)
setup(
name='raindrop',
url='https://www.yudianer.com',
version=server.__version__,
author=server.__author__,
author_email=server.__email__,
description=server.__description__,
long_description=long_description,
license=server.__license__,
packages=find_packages(),
zip_safe=False,
platforms='any',
install_requires=REQUIREMENTS
)

MANIFEST.in:指定打包哪些文件夹

recursive-include etc *
recursive-include client/dist *
recursive-include server/app *
recursive-include server/requirements *
recursive-exclude server *.pyc
prune server/app/static/upload/images
prune server/env
prune server/tests

本文由raindrop网码农撰写。欢迎转载,但请注明出处。


推荐阅读
  • 本文介绍了高校天文共享平台的开发过程中的思考和规划。该平台旨在为高校学生提供天象预报、科普知识、观测活动、图片分享等功能。文章分析了项目的技术栈选择、网站前端布局、业务流程、数据库结构等方面,并总结了项目存在的问题,如前后端未分离、代码混乱等。作者表示希望通过记录和规划,能够理清思路,进一步完善该平台。 ... [详细]
  • 本文介绍了前端人员必须知道的三个问题,即前端都做哪些事、前端都需要哪些技术,以及前端的发展阶段。初级阶段包括HTML、CSS、JavaScript和jQuery的基础知识。进阶阶段涵盖了面向对象编程、响应式设计、Ajax、HTML5等新兴技术。高级阶段包括架构基础、模块化开发、预编译和前沿规范等内容。此外,还介绍了一些后端服务,如Node.js。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 本文介绍了OkHttp3的基本使用和特性,包括支持HTTP/2、连接池、GZIP压缩、缓存等功能。同时还提到了OkHttp3的适用平台和源码阅读计划。文章还介绍了OkHttp3的请求/响应API的设计和使用方式,包括阻塞式的同步请求和带回调的异步请求。 ... [详细]
  • Linux下部署Symfoy2对app/cache和app/logs目录的权限设置,symfoy2logs
    php教程|php手册xml文件php教程-php手册Linux下部署Symfoy2对appcache和applogs目录的权限设置,symfoy2logs黑色记事本源码,vsco ... [详细]
  • 本文详细解析了JavaScript中相称性推断的知识点,包括严厉相称和宽松相称的区别,以及范例转换的规则。针对不同类型的范例值,如差别范例值、统一类的原始范例值和统一类的复合范例值,都给出了具体的比较方法。对于宽松相称的情况,也解释了原始范例值和对象之间的比较规则。通过本文的学习,读者可以更好地理解JavaScript中相称性推断的概念和应用。 ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • python限制递归次数(python最大公约数递归)
    本文目录一览:1、python为什么要进行递归限制 ... [详细]
  • 目录浏览漏洞与目录遍历漏洞的危害及修复方法
    本文讨论了目录浏览漏洞与目录遍历漏洞的危害,包括网站结构暴露、隐秘文件访问等。同时介绍了检测方法,如使用漏洞扫描器和搜索关键词。最后提供了针对常见中间件的修复方式,包括关闭目录浏览功能。对于保护网站安全具有一定的参考价值。 ... [详细]
  • nginx+多个tomcat
    学习nginx的时候遇到的问题:nginx怎么部署两台tomcat?upstream在网上找的资源,我在nginx配置文件(nginx.conf)中添加了两个server。结果只显 ... [详细]
  • ZABBIX 3.0 配置监控NGINX性能【OK】
    1.在agent端查看配置:nginx-V查看编辑时是否加入状态监控模块:--with-http_stub_status_module--with-http_gzip_stat ... [详细]
author-avatar
冰忆ch
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有