doutong1890 2018-02-26 14:57
浏览 54
已采纳

我们应该在应用程序中创建多少Elasticsearch客户端连接

I'm using Golang & elastic client.

Bellow is my client creation logic:

if client, err := elastic.NewClient(elastic.SetURL(ElasticsearchURL)); err != nil {
    // Handle error
    logger.Error.Println(err)
    return nil
} else {
    return client
}

Whats the correct approach to:

  • keep the client object singleton across the application?
  • create and close the clients for each request?

I am kind of confused between counterintuitive answers in below links:

  1. where-to-close-an-elasticsearch-client-connection- suggests one connection per app
  2. how-many-transport-clients-can-a-elasticsearch-cluster-have - suggests one connection per app
  3. elasticsearch-how-to-query-for-number-of-connections -- kind of indicates connections quickly die after serving a request
  • 写回答

1条回答 默认 最新

  • dongxi5423 2018-04-28 12:24
    关注

    That depends on the application.

    In 99% of the use cases you have a normal, long-running application. Then you should create just one client with elastic.NewClient. You can pass it around in your code and it should always work, even in different goroutines. This will create a long-running client which has several benefits. E.g. it will run health checks in the background that will prevent Elastic from sending requests to unhealthy or dead nodes.

    However, if you have a short-running application (something like AWS Lambda oder Cloud Functions) you might need a "connection" on a request level. In that specific case you can use elastic.NewSimpleClient. It has a bit more overhead though as you're creating a new client every time. And it won't do any health checks and other things.

    DO NOT create a new client with elastic.NewClient for every request, as any call to NewClient will create a set of goroutines and you'll quickly run out of resources if you do that.

    Please read the documentation and the wiki for further details.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况