【轻知识】用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

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 运行起来

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
[
        {{ $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

[

    '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/ 内容变了

你可能感兴趣的:(【轻知识】用cetus代理 mysql 用 etcd、confd 动态更换配置(未完待续))