etcd和mysql_【轻知识】用cetus代理 mysql 用 etcd、confd 动态更换配置(未完待续)...

实验机器:阿里云ECS 1核 2g centos 8

功能需求

cetus配置文件根据etcd配置代理mysql主从。

然后etcd put php新的配置

php配置文件 根据 新的配置进行更换。实现从直连到切入代理。

环境需要

openresty 、php 、confd 、etcd 、cetus 、mysql

mysql 别装5.8版本

装吧

openresty

php

yum install php

confd

mysql

版本的话选择5.7吧。5.8的话cetus编译不过去。我是centos8

etcd

cetus

mkdir build/ && cd build/

装读写分离的版本吧

CFLAGS='-g -Wpointer-to-int-cast' cmake ../ -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/home/user/cetus_install -DSIMPLE_PARSER=ON

把confd 跟etcd 运行起来

etcd

执行 etcd 执行下跑起来。你看端口哈。因为confd要指定node。

confd

confd 的配置目录(如果你自定义目录 可以用confd -confdir /data/confd)。

先把 testapp这个项目的配置都放好。testapp现在是手写的。实际上用后台添加,程序执行命令的方式用ssh 创建好对应的配置。这样比较更自动化。

另外,这个目录可根据自己项目灵活创建。

/etc/confd/

├── conf.d

│ └── testapp

│ └── config.toml

└── templates

└── testapp

└── config.tmpl

定时运行 confd -interval=60 -backend etcd -node http://127.0.0.1:2379 & (运行不知这一种模式,我上文的连接都有介绍)

confd 更换 php配置

nginx 配置

server {

listen 80;

server_name localhost;

root /www/testapp;

access_log /www/logs/nginx/testapp.access.log;

error_log /www/logs/nginx/testapp.error.log;

location / {

index index.php index.html index.htm;

try_files $uri $uri/ /index.php?$query_string;

}

location ~ \.php$ {

fastcgi_pass unix:/run/php-fpm/www.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

}

代码demo目录

/www/testapp/

├── config.php # 放置配置

└── index.php # 打印打印东西,操作操作mysql

把配置 放到 confd目录下

testapp的config.toml

[root@i78igdnkcg5i9u5 testapp]# cat /etc/confd/conf.d/testapp/config.toml

[template]

prefix = 'testapp'

src = 'testapp/config.tmpl'

dest = '/www/testapp/config.php'

keys = [

"/dbconfig"

]

上面这个配置比较简单。如果你替换的是nginx 配置、twemproxy配置。需要用到两个选项

check_cmd = "/usr/local/openresty/nginx/sbin/nginx -t -c {{.src}}" #检查语法

reload_cmd = "/usr/local/openresty/nginx/sbin/nginx -s reload" #加载

配置模板

[root@iZ2ze78igdnkcg5i9u57cpZ testapp]# cat /etc/confd/templates/testapp/config.tmpl

return [

"mysql"=>[

{{ $dbconfig := json (getv "/dbconfig")}}

'w'=>[

{{range $dbconfig.Masters}}

[

"host"=> "{{.IP}}",

"port"=> "{{.Port}}",

"user"=> "{{.User}}",

"pwd"=> "{{.Pwd}}"

],

{{end}}

],

'r'=> [

{{range $dbconfig.Slaves}}

[

"host"=> "{{.IP}}",

"port"=> "{{.Port}}",

"user"=> "{{.User}}",

"pwd"=> "{{.Pwd}}"

],

{{end}}

]

]

];

实验替换

etcdctl put /testapp/dbconfig '{"Masters":[{"IP":"127.0.0.1","User":"root","Pwd":"","Port":"3306"}],"Slaves":[{"IP":"127.0.0.1","User":"root","Pwd":"","Port":"3307"},{"IP":"127.0.0.1","User":"root","Pwd":"","Port":"3308"}]}'

为了方便观察 用 watch 命令 watch cat config.php

return [

"mysql"=>[

'w'=>[

[

"host"=> "127.0.0.1",

"port"=> "3306",

"user"=> "root",

"pwd"=> ""

],

],

'r'=> [

[

"host"=> "127.0.0.1",

"port"=> "3307",

"user"=> "root",

"pwd"=> ""

],

[

"host"=> "127.0.0.1",

"port"=> "3308",

"user"=> "root",

"pwd"=> ""

],

]

]

];

MySQL是一个支持事务和ACID特性的关系型数据库。它可以通过使用中间件进行水平扩展和高可用性。Cetus是一个使用C语言开发的MySQL中间件,它提供了全面的数据库访问代理功能,使应用程序可以通过Cetus访问数据库而无需修改代码。 在MySQL和中间件的事务处理中,有几种常见的方案和技术可供选择。其中一种方案是使用触发器和用户定义函数(UDF)。触发器可以在数据库中的特定事件发生时自动执行一系列操作,而UDF是一种自定义的函数,可以扩展MySQL的功能。然而,触发器不支持事务且效率较低。 另一种方案是使用开源的中间件,如阿里开源的cannel中间件和go-mysql-transfer中间件。这些中间件通过主从复制的方式将数据同步到中间件,并将中间件伪装成MySQL的从数据库。其中部署布隆过滤器可以解决缓存穿透问题,而数据预热和加锁机制可以解决缓存击穿和缓存雪崩问题。另外,多级缓存和后台更新线程也是解决缓存雪崩问题的有效方案。 因此,MySQL和中间件的事务处理可以通过使用触发器、中间件和一些解决方案来实现数据的一致性和可靠性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [mysql mycat 中间件安装与使用](https://download.csdn.net/download/weixin_38518958/13699558)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Cetus MySQL数据库中间件-其他](https://download.csdn.net/download/weixin_38738506/19590438)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [中间件之MYSQL](https://blog.csdn.net/qq_39256469/article/details/125482650)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值