由于上文所说的种种原因,ClickHouse为什么不往分布式表直接写数据https://blog.csdn.net/wenyichuan/article/details/114019734
所以采用Nginx做代理
安装依赖模块
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
下载解压Nginx
cd /home
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -xvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
编译安装
不带用户模式
./configure --prefix=/home/nginx --with-http_stub_status_module \
--with-http_ssl_module --with-stream \
--with-http_gzip_static_module \
--with-http_sub_module
带用户模式
./configure --user=www --group=www --prefix=/home/nginx \
--with-http_stub_status_module \
--with-http_ssl_module --with-stream \
--with-http_gzip_static_module \
--with-http_sub_module
make && make install
修改 nginx.conf 配置
添加如下配置,tcp协议访问数据库,181,182,183,184为clickhouse的四个节点
nginx监听clickhouse端口为18123
stream {
upstream ck {
server 172.16.10.181:8123;
server 172.16.10.182:8123;
server 172.16.10.183:8123;
server 172.16.10.184:8123;
}
server {
listen 18123;
proxy_pass ck;
}
全部配置文件如下
#user nobody;
worker_processes 8;
error_log /home/nginx/logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
stream {
upstream ck {
server 172.16.10.181:8123;
server 172.16.10.182:8123;
server 172.16.10.183:8123;
server 172.16.10.184:8123;
}
server {
listen 18123;
proxy_pass ck;
}
log_format proxy '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
access_log /home/nginx/logs/tcp-access.log proxy ;
open_log_file_cache off;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /home/nginx/logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 60;
gzip on;
server {
listen 18080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
配置文件验证
nginx -t
启动nginx
nginx
nginx -s reload
查看日志信息
cd /home/nginx/logs
[root@nginx logs]# ls
access.log error.log nginx.pid tcp-access.log
实时查看连接
tail -f tcp-access.log
程序访问
访问很简单,只要把原先代码中的JDBC连接,IP跟端口号修改为nginx的IP跟监听端口就行
Nginx参数配置
down 表示负载过重或者不参与负载
weight 权重过大代表承担的负载就越大
backup 其它服务器时或down时才会请求backup服务器
max_fails 失败超过指定次数会暂停或请求转往其它服务器
fail_timeout 失败超过指定次数后暂停时间
Nginx负载方式
- 轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 - weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 - ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 - fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。 - url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。