實驗機(jī)器:阿里云ECS 1核 2g centos 8
功能需求
cetus配置文件根據(jù)etcd配置代理mysql主從筹我。
然后etcd put php新的配置
php配置文件 根據(jù) 新的配置進(jìn)行更換破衔。實現(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
模板語法 https://blog.csdn.net/ztsinghua/article/details/51643732?locationNum=3
mysql
版本的話選擇5.7吧雕薪。5.8的話cetus編譯不過去。我是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/
裝讀寫分離的版本吧
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)行起來
etcd
執(zhí)行 etcd 執(zhí)行下跑起來晓淀。你看端口哈所袁。因為confd要指定node。
confd
confd 的配置目錄(如果你自定義目錄 可以用confd -confdir /data/confd
)凶掰。
先把 testapp這個項目的配置都放好燥爷。testapp現(xiàn)在是手寫的。實際上用后臺添加懦窘,程序執(zhí)行命令的方式用ssh 創(chuàng)建好對應(yīng)的配置前翎。這樣比較更自動化。
另外畅涂,這個目錄可根據(jù)自己項目靈活創(chuàng)建港华。
/etc/confd/
├── conf.d
│ └── testapp
│ └── config.toml
└── templates
└── testapp
└── config.tmpl
定時運(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"
]
上面這個配置比較簡單苹丸。如果你替換的是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
<?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}}
]
]
];
實驗替換
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"=> ""
],
]
]
];
請求 http://39.107.233.96/ 內(nèi)容變了