實(shí)驗(yàn)機(jī)器:阿里云ECS 1核 2g centos 8
功能需求
cetus配置文件根據(jù)etcd配置代理mysql主從。
然后etcd put php新的配置
php配置文件 根據(jù) 新的配置進(jìn)行更換。實(shí)現(xiàn)從直連到切入代理。
環(huán)境需要
openresty 、php 、confd 、etcd 、cetus 、mysql
mysql 別裝5.8版本
裝吧
openresty
centos http://openresty.org/cn/linux-packages.html
php
yum install php
confd
https://blog.csdn.net/bbwangj/article/details/82953786
模板語(yǔ)法 https://blog.csdn.net/ztsinghua/article/details/51643732?locationNum=3
mysql
版本的話(huà)選擇5.7吧。5.8的話(huà)cetus編譯不過(guò)去。我是centos8
https://blog.csdn.net/yanchao963852741/article/details/105297519/
etcd
https://github.com/etcd-io/etcd
cetus
https://github.com/cetus-tools/cetus
mkdir build/ && cd build/
裝讀寫(xiě)分離的版本吧
CFLAGS='-g -Wpointer-to-int-cast' cmake ../ -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/home/user/cetus_install -DSIMPLE_PARSER=ON
把confd 跟etcd 運(yùn)行起來(lái)
etcd
執(zhí)行 etcd 執(zhí)行下跑起來(lái)。你看端口哈。因?yàn)閏onfd要指定node。
confd
confd 的配置目錄(如果你自定義目錄 可以用confd -confdir /data/confd)。
先把 testapp這個(gè)項(xiàng)目的配置都放好。testapp現(xiàn)在是手寫(xiě)的。實(shí)際上用后臺(tái)添加,程序執(zhí)行命令的方式用ssh 創(chuàng)建好對(duì)應(yīng)的配置。這樣比較更自動(dòng)化。
另外,這個(gè)目錄可根據(jù)自己項(xiàng)目靈活創(chuàng)建。
/etc/confd/
├── conf.d
│ └── testapp
│ └── config.toml
└── templates
└── testapp
└── config.tmpl
定時(shí)運(yùn)行 confd -interval=60 -backend etcd -node http://127.0.0.1:2379 & (運(yùn)行不知這一種模式,我上文的連接都有介紹)
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"
]
上面這個(gè)配置比較簡(jiǎn)單。如果你替換的是nginx 配置、twemproxy配置。需要用到兩個(gè)選項(xiàng)
check_cmd = "/usr/local/openresty/nginx/sbin/nginx -t -c {{.src}}" #檢查語(yǔ)法
reload_cmd = "/usr/local/openresty/nginx/sbin/nginx -s reload" #加載
配置模板
[root@iZ2ze78igdnkcg5i9u57cpZ testapp]# cat /etc/confd/templates/testapp/config.tmpl
<?php
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}}
]
]
];
實(shí)驗(yàn)替換
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
<?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"=> ""
],
]
]
];
請(qǐng)求 http://39.107.233.96/ 內(nèi)容變了