java.lang.RuntimeException:从本地使用 Spring Boot 容器 API 时连接被拒绝
Posted
技术标签:
【中文标题】java.lang.RuntimeException:从本地使用 Spring Boot 容器 API 时连接被拒绝【英文标题】:java.lang.RuntimeException: Connection refused while using spring boot container apis from local 【发布时间】:2020-10-14 20:47:23 【问题描述】:我在点击 spring boot api 时出错。内部调用弹性搜索实现
943a3f6186c9 docker.elastic.co/elasticsearch/elasticsearch:7.8.0 '/tini -- /usr/local…' ' 11 minutes ago Up 11 minutes 127.0.0.1:9200->9200/tcp, 9300/tcp es01
202627ecb7fd docker.elastic.co/kibana/kibana:7.8.0 '/usr/local/bin/dumb…' 11 minutes ago Up 11 minutes 127.0.0.1:5601->5601/tcp kib01
79395c04a8cf backend:latest '/bin/sh -c '/usr/bi…' 11 minutes ago Up 11 minutes 127.0.0.1:8083->8083/tcp bac01
docker-compose.yml 看起来像:
version: '2.2'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.0
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- xpack.security.enabled=false
- http.host=0.0.0.0
- transport.host=127.0.0.1
- bootstrap.memory_lock=true
- discovery.type=single-node
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 127.0.0.1:9200:9200
networks:
- elastic
kib01:
image: docker.elastic.co/kibana/kibana:7.8.0
container_name: kib01
ports:
- 127.0.0.1:5601:5601
environment:
ELASTICSEARCH_URL: http://es01:9200
ELASTICSEARCH_HOSTS: http://es01:9200
networks:
- elastic
bac01:
image: backend:latest
container_name: bac01
ports:
- 127.0.0.1:8083:8083
networks:
- elastic
volumes:
data01:
driver: local
networks:
elastic:
driver: bridge
弹性搜索主机:localhost:9200 工作正常 kibana 主机:localhost:5601 工作正常
在访问 Spring Boot api 时。内部使用弹性搜索休息高级客户端实现失败
错误:
2020-06-24 10:04:36.154 INFO 1 --- [ main] i.d.backend.common.CommonApplication : Starting CommonApplication v0.0.1 on 79395c04a8cf with PID 1 (/backend.jar started by root in /)
2020-06-24 10:04:36.198 INFO 1 --- [ main] i.d.backend.common.CommonApplication : The following profiles are active: docker
2020-06-24 10:04:52.560 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8083 (http)
2020-06-24 10:04:52.915 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-06-24 10:04:52.916 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.17]
2020-06-24 10:04:53.962 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-06-24 10:04:53.963 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 16725 ms
2020-06-24 10:05:02.290 INFO 1 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2020-06-24 10:05:03.322 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8083 (http) with context path ''
2020-06-24 10:05:03.377 INFO 1 --- [ main] i.d.backend.common.CommonApplication : Started CommonApplication in 33.48 seconds (JVM running for 37.713)
2020-06-24 10:06:53.178 INFO 1 --- [nio-8083-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-06-24 10:06:53.233 INFO 1 --- [nio-8083-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-06-24 10:06:53.965 INFO 1 --- [nio-8083-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 722 ms
2020-06-24 10:07:00.732 ERROR 1 --- [nio-8083-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: Connection refused] with root cause
java.lang.RuntimeException: Connection refused
at in.device.backend.common.elasticsearch.service.impl.ElasticsearchServiceImpl.save(ElasticsearchServiceImpl.java:80) ~[classes!/:0.0.1]
at in.device.backend.common.persistance.service.impl.ESResultServiceImpl.save(ESResultServiceImpl.java:29) ~[classes!/:0.0.1]
at in.device.backend.common.controller.PerformanceController.getMeta(PerformanceController.java:69) ~[classes!/:0.0.1]
at in.device.backend.common.controller.PerformanceController.pushSuitResults(PerformanceController.java:48) ~[classes!/:0.0.1]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
【问题讨论】:
【参考方案1】:错误很少 -
bac01 容器依赖于 es01 和 kib01。所以,添加到 bac01 -
depends_on:
- es01
- kib01
kib01 容器依赖于 es01。所以,在 kib01 定义中为 es01 添加depends_on
bac01 容器将无法使用 localhost:9200 访问 es01。因为,localhost 指的是容器本身(bac01)。使用http://es01:9200访问es01
【讨论】:
这是我正在寻找的确切答案。像魅力一样工作。【参考方案2】:您正在网络中启动多个 Docker 容器。在这个网络中,您通过他们的名字来称呼他们的名字,比如containername:5601
,而不是localhost:5601
或127.0.0.1:5601
。您可以使用属性文件和环境变量来使用不同的配置来在本地或 docker 中运行您的项目。
【讨论】:
以上是关于java.lang.RuntimeException:从本地使用 Spring Boot 容器 API 时连接被拒绝的主要内容,如果未能解决你的问题,请参考以下文章