🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
**系统架构流程图:** ![](https://img.kancloud.cn/50/7f/507f4e6d68b468084fcb8d3f59e0fa8e_1431x707.jpg) ## 配置: **准备工作:** * 1台负载均衡服务器 * 1台redis服务器,如果没有可以和负载均衡服务器放在一起 * 1台云数据库作为主数据库(master) * 至少两台以上的服务器节点,并且在节点上面新建从数据库(slave) * 对数据库主从同步或负载均衡不熟悉的童鞋请移步查看教程,[点击查看]([https://www.kancloud.cn/caike\_/diary/1284155](https://www.kancloud.cn/caike_/diary/1284155)) **负载均衡配置:** ***** ### 1、打开nginx配置文件并修改以下信息: 在`server`配置一下内容: ``` server { listen 80; server_name wq2.mrye.xin; } ``` 添加负载均衡节点,在`http`底下添加以下内容: ``` upstream wq2.mrye.xin { #节点一 server 47.92.100.107:85 weight=8; #节点二 server 47.92.115.251:85 weight=2; } ``` 在upstream模块配置完成后,要让指定的访问反向代理到服务器列表,所以我们需要在`server`底下继续添加以下内容: ``` location ~ .*$ { index index.jsp index.html; proxy_pass http://wq2.mrye.xin ; } ``` 以上是根据权重来达到负载均衡,如以上策略不符合实际要求,请移步查看[nginx负载均衡配置 ]([https://www.kancloud.cn/caike\_/diary/1001782](https://www.kancloud.cn/caike_/diary/1001782)) #### 2、测试 打开浏览器访问`wq2.mrye.xin`并查看其效果 **mysql主从配置:** ***** **php存储方式配置:** ***** #### 1、配置session共享 第一种方式,通过配置微擎自带配置文件实现session共享,如下: 文件路径位于:`根目录/data/config.php` ``` //缓存类型 $config['setting']['cache'] = 'redis'; //redis配置 $config['setting']['redis']['server'] = '127.0.0.1'; $config['setting']['redis']['port'] = 6379; $config['setting']['redis']['pconnect'] = 0; $config['setting']['redis']['requirepass'] = ''; $config['setting']['redis']['timeout'] = 1; //指定session存储方式 $config['setting']['redis']['session'] = 1; ``` 第二种方式,通过配置`php.ini`实现session共享,如下: 在`php.ini` 中找到`session.save_handle `和 `session.save_path`并根据以下修改: 没有密码时使用: ``` session.save_handler = Redis session.save_path = "tcp://127.0.0.1:6379" ``` 有密码时使用: ``` session.save_handler = Redis session.save_path = "tcp://127.0.0.1:6379?persistent=1&database=10&auth=myredisG506" ``` #### 2、php`session`测试 节点一(存): 访问路径:[http://wq2.mrye.xin/web/index.php?c=site&a=entry&do=test-index&m=og\_test](http://wq2.mrye.xin/web/index.php?c=site&a=entry&do=test-index&m=og_test),该类文件路径:`根目录/addons/og_test/inc/web/Test.php` ``` namespace inc\web; class Test extends \Og_testModuleSite { public function index() { session('name','mrye'); session('age',20); session('hegiht',173); } } ``` 节点二(取): 访问路径:[http://wq2.mrye.xin/web/index.php?c=site&a=entry&do=test-get&m=og\_test](http://wq2.mrye.xin/web/index.php?c=site&a=entry&do=test-get&m=og_test),该类文件路径:`根目录/addons/og_test/inc/web/Test.php` ``` namespace inc\web; class Test extends \Og_testModuleSite { public function get() { echo 'name:'.session('name'); echo 'age:'.session('age'); echo 'height:'.session('hegiht'); } } ``` **系统配置:** ***** 数据库配置: ``` //主数据库(远程数据库) $config['db']['master']['host'] = 'rm-2ze5wdzun67eieur7qo.mysql.rds.aliyuncs.com'; $config['db']['master']['username'] = 'haojigou'; $config['db']['master']['password'] = 'Ck6688!@#..'; $config['db']['master']['port'] = '3306'; $config['db']['master']['database'] = 'haojigou'; $config['db']['master']['charset'] = 'utf8'; $config['db']['master']['pconnect'] = 0; $config['db']['master']['tablepre'] = 'ims_'; //主数据库(本地数据库) $config['db']['slave_status'] = true; $config['db']['slave']['1']['host'] = '127.0.0.1'; $config['db']['slave']['1']['username'] = 'haojigou'; $config['db']['slave']['1']['password'] = 'abcdsfgfdsgdg'; $config['db']['slave']['1']['port'] = '3306'; $config['db']['slave']['1']['database'] = 'haojigou'; $config['db']['slave']['1']['charset'] = 'utf8'; $config['db']['slave']['1']['pconnect'] = 0; $config['db']['slave']['1']['tablepre'] = 'ims_'; $config['db']['slave']['1']['weight'] = 0; ``` 缓存配置: ``` //缓存类型 $config['setting']['cache'] = 'redis'; ```