设为首页 收藏本站
查看: 681|回复: 0

[经验分享] Master Slave SQL-7464112

[复制链接]

尚未签到

发表于 2018-10-21 10:51:16 | 显示全部楼层 |阅读模式

  http://turbogears.readthedocs.io/en/latest/cookbook/master-slave.html
SQLAlchemy Master Slave Load Balancing
Since version 2.2 TurboGears has basic support for Master/Slave load balancing
and provides a set of utilities to use it.

  TurboGears permits to declare a master server and any number of slave servers, all the
writes will automatically redirected to the master node, while the other calls will
be dispatched randomly to the slave nodes.
  All the queries executed outside of TurboGears controllers will run only on the
master node, those include the queries performed by the authentication stack to
initially look up an already logged in user, its groups and permissions.
Enabling Master Slave Balancing
  To enable Master Slave load Balancing you just need to edit yourmodel/__init__.pymaking the sessionmaker use the TurboGears BalancedSession:
from tg.configuration.sqla.balanced_session import BalancedSessionmaker = sessionmaker(autoflush=True, autocommit=False,  
                     class_=BalancedSession,
  
                     extension=ZopeTransactionExtension())
  Doing this by itself will suffice to make load balancing work, but still
as there is only the standard database configuration the BalancedSessionwill just be redirecting all the queries to the only available serve.
Configuring Balanced Nodes
  To let load balancing work we must specify at least a master and slave server
inside our application configuration. The master server can be specified
using thesqlalchemy.masterset of options, while any number of slaves
can be configured using thesqlalchemy.slavesoptions:
sqlalchemy.master.url = mysql://username:password@masterhost:port/databasenamesqlalchemy.master.pool_recycle = 3600sqlalchemy.slaves.slave1.url = mysql://username:password@slavehost:port/databasenamesqlalchemy.slaves.slave1.pool_recycle = 3600  The master node can be configured also to be a slave, this is usually the
case when we want the master to also handle some read queries.
Driving the balancer
  TurboGears provides a set of utilities to let you change the default behavior
of the load balancer. Those include the @with_engine(engine_name) decorator
and the DBSession().using_engine(engine_name) context.
The with_engine decorator
  The with_engine decorator permits to force a controller method to
run on a specific node. It is a great tool for ensuring that some
actions take place on the master node, like controllers that edit
content.
from tg import with_engine@expose('myproj.templates.about')@with_engine('master')def about(self):  
    DBSession.query(model.User).all()
  
    return dict(page='about')
  The previous query will be executed on the master node, if the @with_enginedecorator is removed it will get execute on any random slave.
  The with_engine decorator can also be used to force turbogears
to use the master node when some parameters are passed by url:
@expose('myproj.templates.index')@with_engine(master_params=['m'])def index(self):  
    DBSession.query(model.User).all()
  
    return dict(page='index')
  In this case calling http://localhost:8080/index will result in queries
performed on a slave node, while calling http://localhost:8080/index?m=1 will
force the queries to be executed on the master node.
  Pay attention that the m=1 parameter can actually have any value, it just
has to be there. This is especially useful when redirecting after an action
that just created a new item to a page that has to show the new item. Using
a parameter specified in master_params we can force TurboGears to fetch
the items from the master node so to avoid odd results due to data propagation
delay.
Keeping master_params around
  By default parameters specified in with_engine master_params will be
popped from the controller params. This is to avoid messing with validators
or controller code that doesn’t expect the parameter to exist.
  If the controller actually needs to access the parameter a dictionary can be
passed to @with_engine instead of a list. The dictionary keys will be
the parameters, while the value will be if to pop it from the
parameters or not.
@expose('myproj.templates.index')@with_engine(master_params={'m':False})def index(self, m=None):  
    DBSession.query(model.User).all()
  
    return dict(page='index', m=m)
Forcing Single Queries on a node
  Single queries can be forced to execute on a specific node using theusing_engine method of the BalancedSession. This method
returns a context manager, until queries are executed inside this
context they are run on the constrained engine:
with DBSession().using_engine('master'):  
    DBSession.query(model.User).all()
  
    DBSession.query(model.Permission).all()DBSession.query(model.Group).all()
  In the previous example the Users and the Permissions will be
fetched from the master node, while the Groups will be fetched
from a random slave node.
Debugging Balancing
  Setting the root logger of your application to DEBUG will let
you see which node has been choose by the BalancedSessionto perform a specific query.



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-624409-1-1.html 上篇帖子: VMware、安装部署vCenter Server-xiexiaojun 下篇帖子: sql server2008 自动备份和自动删除过期bak文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表