http - 对elasticsearch服务器的Httpoison请求给了我econnrefused错误,而卷曲同样的事情却没有
问题描述
当我尝试使用 httpoison 查询弹性搜索服务器时
iex(1)> HTTPoison.get "http://localhost:9200"
我明白了
{:error, %HTTPoison.Error{id: nil, reason: :econnrefused}}.
如果我做
curl -XGET "http://localhost:9200"
我明白了
{
"name" : "es01",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "Wik-EpMkQ8ummJE6ctNAOg",
"version" : {
"number" : "7.0.1",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "e4efcb5",
"build_date" : "2019-04-29T12:56:03.145736Z",
"build_snapshot" : false,
"lucene_version" : "8.0.0",
"minimum_wire_compatibility_version" : "6.7.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
有谁知道这种行为是由于什么以及如何解决它?
PS:将 localhost 更改为 127.0.0.1 并不能解决问题。
解决方案
这是我的设置:
elasticsearch Version: 7.0.1
{:httpoison, "~> 1.5"} #=> mix.lock shows version 1.5.1 was installed
卷曲结果:
$ curl -XGET "http://localhost:9200"
{
"name" : "My-MacBook-Pro.local",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "vEFl3B5TTYaBxPhQFuXPyQ",
"version" : {
"number" : "7.0.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "e4efcb5",
"build_date" : "2019-04-29T12:56:03.145736Z",
"build_snapshot" : false,
"lucene_version" : "8.0.0",
"minimum_wire_compatibility_version" : "6.7.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
HTTPoison 结果:
$ iex -S mix
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
===> Compiling parse_trans
===> Compiling mimerl
===> Compiling metrics
===> Compiling unicode_util_compat
===> Compiling idna
==> ssl_verify_fun
Compiling 7 files (.erl)
Generated ssl_verify_fun app
===> Compiling certifi
===> Compiling hackney
==> httpoison
Compiling 3 files (.ex)
Generated httpoison app
==> hello
Compiling 15 files (.ex)
Generated hello app
Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> HTTPoison.get "http://localhost:9200"
{:ok,
%HTTPoison.Response{
body: "{\n \"name\" : \"My-MacBook-Pro.local\",\n \"cluster_name\" :
\"elasticsearch\",\n \"cluster_uuid\" : \"vEFl3B5TTYaBxPhQFuXPyQ\",\n
\"version\" : {\n \"number\" : \"7.0.1\",\n \"build_flavor\" :
\"default\",\n \"build_type\" : \"tar\",\n \"build_hash\" :
\"e4efcb5\",\n \"build_date\" : \"2019-04-29T12:56:03.145736Z\",\n
\"build_snapshot\" : false,\n \"lucene_version\" : \"8.0.0\",\n
\"minimum_wire_compatibility_version\" : \"6.7.0\",\n
\"minimum_index_compatibility_version\" : \"6.0.0-beta1\"\n },\n
\"tagline\" : \"You Know, for Search\"\n}\n",
headers: [
{"content-type", "application/json; charset=UTF-8"},
{"content-length", "522"}
],
request: %HTTPoison.Request{
body: "",
headers: [],
method: :get,
options: [],
params: %{},
url: "http://localhost:9200"
},
request_url: "http://localhost:9200",
status_code: 200
}}
iex(2)>
接下来,我停止了 elasticsearch 服务器,然后再次运行 HTTPoison 请求:
ex(2)> HTTPoison.get "http://localhost:9200"
{:error, %HTTPoison.Error{id: nil, reason: :econnrefused}}
对于 curl 请求,我得到了类似的结果:
$ curl -XGET "http://localhost:9200"
curl: (7) Failed to connect to localhost port 9200: Connection refused
如果你连续发出两个 curl 请求会发生什么?他们都成功了吗?尝试先发出 HTTPoison 请求,然后再发出 curl 请求。哪一个失败了?尝试相反的顺序。结果一样吗?
推荐阅读
- python - 我正在尝试使用 Tweepy 和 Python 制作 Twitter 机器人,但出现错误 215?
- c++ - 如果类在堆上,函数会进入堆还是堆栈?
- go - 从 github 问题中检索最新评论
- java - Spring Boot 对象字段属性中的验证顺序
- bash - ffmpeg:在while循环中执行命令
- java - 如何使用 ActiveMQ 消费来自虚拟主题的消息?
- typescript - Vuex商店更新未在组件中应用重新渲染
- react-native - 如何在抽屉导航屏幕中传递参数?
- nginx - 有关 Nginx 如何在 Google 计算引擎上使用 SSL 的详细信息
- r - R中混合lmer模型的警告消息