ClickHou的负载均衡如何做。
1,负载均衡。
⽬前我们公司后台使⽤clickhou,来做数据的离线分析;配置为四台集群(shard)
通过springboot+clickhou-jdbc完成服务与clickhou jdbc的连接;使⽤的是clickhou官
⽅,BalancedClickhouDataSource(urls,properties),做到四台服务之间的负载均衡;⽹上很多资料都表⽰
这个包虽然可以做到⼀定程度的负载均衡,但是⽆法做到故障转移;按照⼤家的推荐,我本地是nginx搭建clickhou-jdbc 负载均衡的服务
安装nginx
配置inx
upstream clickhou{
rver 10.22.5.129:18123;
marginwidth#rver 10.22.5.130:18123;
#rver 10.22.5.132:18123;
#rver 10.22.5.133:18123;
}
rver {
listen 18123;
rver_name localhost;
#chart koi8-r;
#access_log /var/log/nginx/host.access.log main;
# location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
location / {
proxy_pass clickhou;
}
#error_page 404 /404.html;
# redirect rver error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass 127.0.0.1;
#}
# pass the PHP scripts to FastCGI rver listening on 127.0.0.1:9000 #
#location ~ \.php$ {
endura
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
dazzy
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params;
6级答案#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
helpdesk#
#location ~ /\.ht {
# deny all;
#}
}
springboot 项⽬配置
rver.port=8181dialogic
spring boot 的config类
@Configuration
@MapperScan(baPackages = {"com.john.balanceclickhou.demo"},
sqlSessionFactoryRef ="dwstSqlSessionFactory" )
public class ClickhouConfig {
@Resource
private LocalProperties localProperties;
@Bean(name = "dataSource")
public DataSource clickHouDataSource() {
ClickHouProperties properties = new ClickHouProperties();
complimentaryproperties.Db());
properties.Urname());
properties.Password());
properties.ConnectionTimeout());
properties.SocketTimeout());
//clickhou jdbc 包,可以⽤来做负载均衡;策略是随机的,但是搭建SLB之后,就可以使⽤nginx 的负载均衡策略了
BalancedClickhouDataSource ckDataSource = new Address(), properties);
// 添加集群节点存活检查
return ckDataSource.AliveTestInterval(), TimeUnit.SECONDS);
}
@Bean(name = "configuration")
public org.apache.ibatis.ssion.Configuration globalConfiguration() {
org.apache.ibatis.ssion.Configuration configuration = new org.apache.ibatis.ssion.Configuration();
configuration.tMapUnderscoreToCamelCa(true);
return configuration;
}
@Bean(name = "SqlSessionFactory")
public SqlSessionFactory clickHouSqlSessionFactory(@Qualifier("dataSource") DataSource clickHouDataSource, @Qualifier("configuration") org.apache.ib final SqlSessionFactoryBean ssionFactory = new SqlSessionFactoryBean();
ssionFactory.tDataSource(clickHouDataSource);
ssionFactory.tMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml"));
ssionFactory.tTypeAliasPackage("com.john.balanceclickhou.demo");
ssionFactory.tConfiguration(configuration);
Object();
}
cope}
配置⽂件
@Data
@Component
@ConfigurationProperties(prefix = "st")
public class LocalProperties {
/**
* 数据库地址
/
private String address;
/**
* 连接数据库
*/
private String db;
/**
* socket超时时间
*/
就去成人网
private Integer socketTimeout;
/**
* ⽤户名
钱英语怎么写*/
private String urname;
/**
* 密码
*/
private String password;
/**
* 连接超时时间
*/
private Integer connectionTimeout;
/**
* 判断存活实例时间间隔
*/
private Integer aliveTestInterval;
}
总结:
虽然BalancedClickhouDataSource在⼀定程度上能做到负载均衡;如果不能做到故障转移,会很⼤程度上失去负载均衡的好处;本⽂通过实践,完成了clickhou jdbc slb 的服务器搭建,为更好的使⽤clickhou提供了帮助
3,如果是flink写⼊到clickhou,调⽤的jar包
<dependency>
<groupId>ru.ivi.opensource</groupId>
<artifactId>flink-clickhou-sink</artifactId>
<version>1.0.0</version>
</dependency>
也可⽤通过负载均衡做批量写⼊。
jar包⾃动做负载均衡。