由于 java.net.ConnectException,无法找到 PropertySource:连接被拒绝
Posted
技术标签:
【中文标题】由于 java.net.ConnectException,无法找到 PropertySource:连接被拒绝【英文标题】:Could not locate PropertySource because of java.net.ConnectException: Connection refused 【发布时间】:2018-12-25 19:43:24 【问题描述】:我创建了一个 docker 镜像并将其推送到 docker hub。现在,我尝试使用docker-compose
在远程机器上运行它,但出现以下错误。
java.lang.IllegalStateException: Could not locate PropertySource and the fail fast property is set, failing
my-test_1 | at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:132)
my-test_1 | at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:93)
my-test_1 | at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:567)
my-test_1 | at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:338)
my-test_1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:301)
my-test_1 | at net.ptidej.seodin.SeodinApp.main(SeodinApp.java:68)
my-test_1 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
my-test_1 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
my-test_1 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
my-test_1 | at java.lang.reflect.Method.invoke(Method.java:498)
my-test_1 | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
my-test_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
my-test_1 | at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
my-test_1 | at org.springframework.boot.loader.WarLauncher.main(WarLauncher.java:59)
my-test_1 | Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8761/config/seodin/prod/master": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
my-test_1 | at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:674)
my-test_1 | at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621)
my-test_1 | at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:539)
my-test_1 | at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.getRemoteEnvironment(ConfigServicePropertySourceLocator.java:172)
my-test_1 | at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:93)
my-test_1 | ... 13 common frames omitted
my-test_1 | Caused by: java.net.ConnectException: Connection refused (Connection refused)
my-test_1 | at java.net.PlainSocketImpl.socketConnect(Native Method)
my-test_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
my-test_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
my-test_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
my-test_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
my-test_1 | at java.net.Socket.connect(Socket.java:589)
my-test_1 | at java.net.Socket.connect(Socket.java:538)
my-test_1 | at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
my-test_1 | at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
my-test_1 | at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
my-test_1 | at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
my-test_1 | at sun.net.www.http.HttpClient.New(HttpClient.java:339)
my-test_1 | at sun.net.www.http.HttpClient.New(HttpClient.java:357)
my-test_1 | at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220)
my-test_1 | at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1156)
my-test_1 | at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
my-test_1 | at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984)
my-test_1 | at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:78)
my-test_1 | at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
my-test_1 | at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
my-test_1 | at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:112)
my-test_1 | at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator$GenericRequestHeaderInterceptor.intercept(ConfigServicePropertySourceLocator.java:237)
my-test_1 | at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:88)
my-test_1 | at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:72)
my-test_1 | at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
my-test_1 | at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
my-test_1 | at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:660)
my-test_1 | ... 17 common frames omitted
my-test_1 |
我猜这与其中一个配置文件中的端口有关,但我不知道是哪一个。这个错误是什么意思,我应该去哪里解决它?我将不胜感激源代码/配置文件中的任何线索/提示。谢谢。
【问题讨论】:
【参考方案1】:您的 Spring Boot 应用程序配置为从指定的 Spring Cloud Config 服务器加载属性
http://localhost:8761/config/seodin/prod/master
它看不到它。
当您在 Docker 中运行时,请确保配置服务器也在 Docker 中运行,并且您的 Spring Boot 应用程序已配置为指向该服务器(URL 不会是 localhost;它可能是容器名称如果你在 Docker 中运行配置服务器,或者外部配置服务器的 URL)
【讨论】:
【参考方案2】:当您在 Docker
容器内时,localhost
解析为该容器而不是 Docker Host
。因此,您应该在应用程序配置中将Config Server
的IP 地址更改为Docker Host
IP 地址或您的LAN IP 地址。当然,如果你的机器上有一个正在运行的Config Server
,它会起作用。
【讨论】:
在哪里可以找到定义的配置服务器 IP 以将其更改为 Docker 主机? 它可以是您的 application.properties 或 bootstrap.properties 文件中的spring.cloud.config.uri
配置。看看this link,可能会有帮助。
你的问题还有一个奇怪的地方。端口 8761 是 Eureka Discovery Server
的标准端口,端口 8888 是 Configuration Server
的标准端口。但是您的日志显示您的应用正在尝试从端口 8761 的 Configuration Server
获取其配置。以上是关于由于 java.net.ConnectException,无法找到 PropertySource:连接被拒绝的主要内容,如果未能解决你的问题,请参考以下文章