Clickhouse分布式集群搭建
目标:1分片2副本的集群
所以需要两台机器,分别是:172.31.59.118|172.31.40.79
- https://hub.docker.com/r/yandex/clickhouse-client/dockerfile
- https://hub.docker.com/r/yandex/clickhouse-server/dockerfile
安装步骤
Amazon linux 2是centos系的,使用的yum系的安装方式
修改机器时区(不需要重启)
sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
开始安装
sudo yum install -y curl
sudo yum install -y epel-release
curl -s https://packagecloud.io/install/repositories/altinity/clickhouse/script.rpm.sh | sudo os=centos dist=7 bash # for Amazon Linux
sudo yum list 'clickhouse*'
sudo yum install -y clickhouse-server clickhouse-client
sudo yum list installed 'clickhouse*'
sudo /etc/init.d/clickhouse-server restart
clickhouse-client
安装zookeeper集群,也可以使用现成的,本次使用现成的,配置如下,后续加入配置文件中
<zookeeper-servers>
<node index="1">
<host>172.31.3.79</host>
<port>2181</port>
</node>
<node index="2">
<host>172.31.47.229</host>
<port>2181</port>
</node>
<node index="3">
<host>172.31.53.227</host>
<port>2181</port>
</node>
</zookeeper-servers>
设置每台机器clickhouse用户和密码,先生成sha256的密码
PASSWORD=$(base64 < /dev/urandom | head -c16); echo "$PASSWORD"; echo -n "$PASSWORD" | sha256sum | tr -d '-'
rKbfrrze4PO5xWeN
93626c6535b2817d55eca365d9a00bbe88c63e24fa4941ce8cdaf4c07f4ab4a6
添加用户
sudo vim /etc/clickhouse-server/users.xml
<users>
<zaihui>
<password_sha256_hex>93626c6535b2817d55eca365d9a00bbe88c63e24fa4941ce8cdaf4c07f4ab4a6</password_sha256_hex>
<networks incl="networks" replace="replace">
<ip>::/0</ip>
</networks>
<profile>default</profile>
<quota>default</quota>
</zaihui>
</users>
验证
clickhouse-client -u zaihui --password rKbfrrze4PO5xWeN
clickhouse-client -u zaihui --password rKbfrrze4PO5xWeN
修改clickhouse时区配置
<timezone>Asia/Shanghai</timezone>
取消访问来源ip的限制
sudo vim /etc/clickhouse-server/config.xml
<!-- <listen_host>::</listen_host> -->
<!-- Same for hosts with disabled ipv6: -->
<listen_host>0.0.0.0</listen_host>
<!-- Default values - try listen localhost on ipv4 and ipv6: -->
<listen_host>::1</listen_host>
<listen_host>127.0.0.1</listen_host>
-->
开始配置集群
配置
sudo vim /etc/clickhouse-server/config.xml
<!-- If element has 'incl' attribute, then for it's value will be used corresponding substitution from another file.
By default, path to file with substitutions is /etc/metrika.xml. It could be changed in config in 'include_from' element.
Values for substitutions are specified in /yandex/name_of_substitution elements in that file.
<include_from>/etc/clickhouse-server/metrika.xml</include_from>
<!-- ZooKeeper is used to store metadata about replicas, when using Replicated tables.
Optional. If you don't use replicated tables, you could omit that.
See https://clickhouse.yandex/docs/en/table_engines/replication/
-->
配置
/etc/clickhouse-server/metrika.xml
,所有机器都一样
<yandex>
<clickhouse_remote_servers>
<ck_cluster>
<shard>
<weight>1</weight>
<internal_replication>true</internal_replication>
<replica>
<host>172.31.59.118</host>
<port>9000</port>
<user>zaihui</user>
<password>rKbfrrze4PO5xWeN</password>
</replica>
<replica>
<host>172.31.40.79</host>
<port>9000</port>
<user>zaihui</user>
<password>rKbfrrze4PO5xWeN</password>
</replica>
</shard>
</ck_cluster>
</clickhouse_remote_servers>
<zookeeper-servers>
<node index="1">
<host>172.31.3.79</host>
<port>2181</port>
</node>
<node index="2">
<host>172.31.47.229</host>
<port>2181</port>
</node>
<node index="3">
<host>172.31.53.227</host>
<port>2181</port>
</node>
</zookeeper-servers>
<networks>
<ip>::/0</ip>
</networks>
<clickhouse_compression>
<min_part_size>10000000000</min_part_size>
<min_part_size_ratio>0.01</min_part_size_ratio>
<method>lz4</method>
</case>
</clickhouse_compression>
</yandex>
配置
/etc/clickhouse-server/config.d/macros.xml
,所有机器都不一样,虽然也可以把配置放在metrika.xml中,但是把不同的独立出来更合适
<yandex>
<macros>
<replica>172.31.59.118</replica>
<shard>01</shard>
<layer>01</layer>
</macros>
</yandex>
<yandex>
<macros>
<replica>172.31.40.79</
replica>
<shard>01</shard>
<layer>01</layer>
</macros>
</yandex>
集群配置完成之后重启一下,确保每个机器都能连接成功
使用datagrip连接各个机器,全部成功
验证集群功能
创建以replica结尾的本地表delphi_membership_properties_replica和分布式表delphi_membership_properties
插入数据:在本地表和分布式表插入时在每个replica中都有数据生成
JDBC连接clickhouse cluster
两种方式,一种是使用clickhouse-jdbc连接集群中的每一个节点,另外一种是使用SLB提供一个对外的统一地址
使用BalancedClickhouseDataSource
-
clickhouse-jdbc/BalancedClickhouseDataSource.java at master · yandex/clickhouse-jdbc · GitHub
/** * create Datasource for clickhouse JDBC connections * @param url address for connection to the database * must have the next format {@code jdbc:clickhouse://<first-host>:<port>,<second-host>:<port>/<database>?param1=value1¶m2=value2 } * for example, {@code jdbc:clickhouse://localhost:8123,localhost:8123/database?compress=1&decompress=2 } * @throws IllegalArgumentException if param have not correct format, or error happens when checking host availability public BalancedClickhouseDataSource(final String url) { this(splitUrl(url), getFromUrl(url)); * create Datasource for clickhouse JDBC connections * @param url address for connection to the database * @param properties database properties * @see #BalancedClickhouseDataSource(String) public BalancedClickhouseDataSource(final String url, Properties properties) { this(splitUrl(url), new ClickHouseProperties(properties)); * create Datasource for clickhouse JDBC connections * @param url address for connection to the database * @param properties database properties * @see #BalancedClickhouseDataSource(String) public BalancedClickhouseDataSource(final String url, ClickHouseProperties properties) { this(splitUrl(url), properties.merge(getFromUrlWithoutDefault(url))); }
使用SLB
配置LB: 使用标准JDBC连接时需要映射http协议到clickhouse的8123端口(http监听端口)
贴一下在Springboot中使用标准JDBC数据源HikariDataSource÷连接clickhouse的配置: