搭建高性能laravel8+swoole+rabbitmq消息队列+redis集群+mysql主从读写分离
一.laravel+swoole
1.laravel8+jwt搭建参考
https://blog.csdn.net/xiayu204575/article/details/111745630
2. 安装laravel-swoole
composer require swooletw/laravel-swoole
3.发布swoole配置文件到config
php artisan vendor:publish --tag=laravel-swoole
4.修改config/swoole_http.php
[
'server' => [
// Options here will pass to Swoole server's configuration directly
'options' => [
'max_request' => 1000,
// You can run your application in deamon
'daemonize' => env('SWOOLE_HTTP_DAEMONIZE', false),
// Normally this value should be 1~4 times lager according to your cpu cores
'reactor_num' => env('SWOOLE_HTTP_REACTOR_NUM', swoole_cpu_num() * 2),
'worker_num' => env('SWOOLE_HTTP_WORKER_NUM', swoole_cpu_num() * 2),
'task_worker_num' => env('SWOOLE_HTTP_TASK_WORKER_NUM', swoole_cpu_num() * 2),
// This value should be larger than `post_max_size` and `upload_max_filesize` in `php.ini`.
// This equals to 10 MB
'package_max_length' => 10 * 1024 * 1024,
'buffer_output_size' => 10 * 1024 * 1024,
// Max buffer size for socket connections
'socket_buffer_size' => 128 * 1024 * 1024,
// Worker will restart after processing this number of request
'max_request' => 3000,
// Enable coroutine send
'send_yield' => true,
// You must add --enable-openssl while compiling Swoole
'ssl_cert_file' => null,
'ssl_key_file' => null,
],
],
// You can customize your swoole tables here.
// See https://wiki.swoole.com/wiki/page/p-table.html for more detailed information.
'tables' => [
'table_name' => [
'size' => 1024,
'columns' => [
['name' => 'column_name', 'type' => Table::TYPE_STRING, 'size' => 1024],
]
],
],
// ...
*/
'providers' => [
Illuminate\Pagination\PaginationServiceProvider::class,
// 不要移动这两个组件的顺序 解决登录不成功问题
Illuminate\Auth\AuthServiceProvider::class,
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
Fruitcake\Cors\CorsServiceProvider::class,
],
]
5.启动:
#start|stop|restart|reload|infos
php artisan swoole:http start
切记:修改程序后一定要reload一下
6.配置nginx转发到swoole:http
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name xxx.xxx.com;
root /path/to/laravel/public;
index index.php;
location = /index.php {
# Ensure that there is no such file named "not_exists"
# in your "public" directory.
try_files /not_exists @swoole;
}
# any php files must not be accessed
#location ~* \.php$ {
# return 404;
#}
# 伪静态设置
location / {
try_files $uri $uri/ @swoole;
}
location @swoole {
set $suffix "";
if ($uri = /index.php) {
set $suffix ?$query_string;
}
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
# IF https
# proxy_set_header HTTPS "on";
proxy_pass http://127.0.0.1:1215$suffix;
}
}
二.mysql主从读写分离
1.mysql主从读写分离配置
参考
链接: https://blog.csdn.net/xiayu204575/article/details/111056764.
2.laravel配置主从
1)打开config/database.php 找到’mysql’ 修改如下
'mysql' => [
'read' => [
'host' => env('DB_READ_HOST', ''),
'username' => env('DB_READ_USERNAME', ''),
'password' => env('DB_READ_PASSWORD', '')
],
'write' => [
'host' => env('DB_WRITE_HOST', ''),
'username' => env('DB_WRITE_USERNAME', ''),
'password' => env('DB_WRITE_PASSWORD', '')
],
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
// 'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
// 'username' => env('DB_USERNAME', 'forge'),
// 'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
2)在.env中加入对应的值
三.redis集群
1.redis集群配置
参考
链接: https://blog.csdn.net/xiayu204575/article/details/111059900.
2.laravel配置
1)打开config/database.php 找到’redis’ 修改如下
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
],
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],
'clusters' => [
//'cluster1'是调用的时候的key Redis::connection('clusters1');
'cluster1' => [
[
'host' => '127.0.0.1',
'password' => '123456',
'port' => 7100,
'database' => 0,
],
[
'host' => '127.0.0.1',
'password' => '123456',
'port' => 7101,
'database' => 0,
],
[
'host' => '127.0.0.1',
'password' => '123456',
'port' => 7102,
'database' => 0,
],
[
'host' => '127.0.0.1',
'password' => '123456',
'port' => 7103,
'database' => 0,
],
[
'host' => '127.0.0.1',
'password' => '123456',
'port' => 7104,
'database' => 0,
],
[
'host' => '127.0.0.1',
'password' => '123456',
'port' => 7105,
'database' => 0,
],
],
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],
],
2)代码中使用
use Illuminate\Support\Facades\Redis;
$redis = Redis::connection('clusters1');
$key = 'foo';
$redis->set($key, 'test');
var_dump($redis->get($key));
默认情况下,集群可以在节点上实现客户端分片,允许你实现节点池以及创建大量可用内存。这里要注意,客户端共享不会处理失败的情况;因此,这个功能主要适用于从另一个主数据库获取的缓存数据。队列用rabbitmq,不用redis集群
四.rabbitmq消息队列
队列用rabbitmq,不用redis集群
参考
https://blog.csdn.net/xiayu204575/article/details/112136791