Redis Gearman数据库缓存
秃顶_的技术博客 人气:0前言
一、前端搭建
1、Nginx部署
安装部署Nginx
yum install unzip openssl-devel pcre-devel gcc make -y tar -zxf nginx-1.22.0.tar.gz ./configure --with-http_stub_status_module --with-http_ssl_module --with-file-aio --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ make -j 2 && make install ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
vim nginx.conf location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi.conf; }
2、php部署安装以及模块安装
php安装同时部署相应的兼容模块方式
yum install php php-fpm -y yum install php-pecl-gearman-1.1.2-1.el7.x86_64.rpm php-pecl-igbinary-1.2.1-1.el7.x86_64.rpm php-pecl-redis-2.2.8-1.el7.x86_64.rpm php-fpm-5.4.16-46.el7.x86_64.rpm -y systemctl start php-fpm.serrvice
将编写好的php放置到Nginx的默认发布目录位置
vim test.php <?php $redis = new Redis(); $redis->connect('192.168.63.102',6379) or die ("could net connect redis server"); # $query = "select * from test limit 9"; $query = "select * from test"; for ($key = 1; $key < 10; $key++) { if (!$redis->get($key)) { $connect = mysql_connect('192.168.63.109','redis','dockerps-A1'); mysql_select_db(test); $result = mysql_query($query); //如果没有找到$key,就将该查询sql的结果缓存到redis while ($row = mysql_fetch_assoc($result)) { $redis->set($row['id'],$row['name']); } $myserver = 'mysql'; break; } else { $myserver = "redis"; $data[$key] = $redis->get($key); } } echo $myserver; echo "<br>"; for ($key = 1; $key < 10; $key++) { echo "number is <b><font color=#FF0000>$key</font></b>"; echo "<br>"; echo "name is <b><font color=#FF0000>$data[$key]</font></b>"; echo "<br>"; } ?>
二、后端部署
1.MySQL部署
1.建立用户设定远程登录
yum install mariadb-server -y systemctl start mariadb.service
部署后在MySQL端进行创建一个用户给与远程登录权限,使得Redis作为缓存可以用来同步数据使用
建立test表保证有可以查看的数据
MariaDB [test]> CREATE USER redis@'%' IDENTIFIED BY 'dockerps-A1' ; MariaDB [test]> CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; MariaDB [test]> INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9'); MariaDB [test]> grant all on test.* to redis@'%';
2.注册UDF函数
yum install mariadb-devel -y #下载对应的开发包 unzip lib_mysqludf_json-master.zip gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c #利用udf函数使得其生成UDF函数 MariaDB [(none)]> show global variables like 'plugin_dir'; #查看函数的存储目录 cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/ MariaDB [(none)]> CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so';
2.Redis作为缓存的部署以及安装
make -j 2 && make install ./utils/install_server.sh
三、germand分布式缓存位置部署
1、gearman原理
这里C端为MySQL的UDF函数节点通过已经注册的UDF函数和触发器结合使数据发生改变后将数据的更改通知到前端gearmand节点使其从Redis节点从新同步发生改变的数据
S端为前端worker节点即前端gearmand,同样需要在php环境中兼容这个插件的模块
2、C端部署
./configure --with-mysql=/usr/bin/mysql_config --libdir=/usr/lib64/mysql/plugin/ make -j 2 && make install MariaDB [(none)]> CREATE FUNCTION gman_do_background RETURNS STRING SONAME 'libgearman_mysql_udf.so'; MariaDB [(none)]> CREATE FUNCTION gman_servers_set RETURNS STRING SONAME 'libgearman_mysql_udf.so'; #注册Gearman的UDF函数使其可以正常调用数据库的内容 MariaDB [(none)]> SELECT gman_servers_set('192.168.63.101:4730'); #IP为S端IP以及端口为gearmand的运行端口
vim trigger.sql use test; DELIMITER $$ CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN SET @RECV=gman_do_background('syncToRedis', json_object(NEW.id as `id`, NEW.name as `name`)); END$$ DELIMITER ;
3、S端部署
yum install gearmand-1.1.12-18.el7.x86_64.rpm libgearman-1.1.12-18.el7.x86_64.rpm -y systemctl start gearmand.service
编写worker文件
vim worker.php <?php $worker = new GearmanWorker(); $worker->addServer(); $worker->addFunction('syncToRedis', 'syncToRedis'); $redis = new Redis(); $redis->connect('192.168.63.102', 6379); while($worker->work()); function syncToRedis($job) { global $redis; $workString = $job->workload(); $work = json_decode($workString); if(!isset($work->id)){ return false; } $redis->set($work->id, $work->name); } ?>
总结
加载全部内容