cassandra的连接池配置
cassandra的连接池配置 cassandra的datastax驱动使用的是异步nio实现的,发出去的请求,不会阻塞线程,当有响应的时候会通知你。所以cassandra客户端和服务器之间不需要太多的连接,因为发送一个请求是很快的,只要一个线程不断监听响应就可以了。 cassandra的配置方式如下: PoolingOptions poolingOptions = new PoolingOptions(); poolingOptions .setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, 32); poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL, 2); poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, 4); Cluster cluster = Cluster.builder() .addContactPoints("192.168.1.101") .withCredentials(username, password) .withPoolingOptions(poolingOptions); 这就完成了一个对连接池的配置。 setCoreConnectionsPerHost(HostDistance.LOCAL, 2); 表示和集群里的机器至少有2个连接。注意是和集群里的每个机器都至少有2个连接。 setMaxConnectionsPerHost(HostDistance.LOCAL, 4); 最多有4个连接 setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, 32); 每个连接允许32请求并发。 也就是说你这个配置,最多允许(324机器个数)个并发请求。如果太多的并发可能会发生获取连接失败。 以上是说的集群部署在一个机房,只有一个数据中心DC的情况,如果有多个数据中心。要设置REMOTE连接数。 PoolingOptions poolingOptions = new PoolingOptions(); poolingOptions .setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, 32); poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL, 2); poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, 4); poolingOptions.setCoreConnectionsPerHost(HostDistance.REMOTE, 2); poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, 4); 配置完之后,获取session,就可以全程序单例使用了。