【Docker容器啟動(dòng)問(wèn)題】容器啟動(dòng)時(shí), exceptions.JedisConnectionException:Could not get a resource from the pool
作者:xcbeyond
瘋狂源自夢(mèng)想,技術(shù)成就輝煌!微信公眾號(hào):《程序猿技術(shù)大咖》號(hào)主,專注后端開(kāi)發(fā)多年,擁有豐富的研發(fā)經(jīng)驗(yàn),樂(lè)于技術(shù)輸出、分享,現(xiàn)階段從事微服務(wù)架構(gòu)項(xiàng)目的研發(fā)工作,涉及架構(gòu)設(shè)計(jì)、技術(shù)選型、業(yè)務(wù)研發(fā)等工作。對(duì)于Java、微服務(wù)、數(shù)據(jù)庫(kù)、Docker有深入了解,并有大量的調(diào)優(yōu)經(jīng)驗(yàn)。
問(wèn)題現(xiàn)場(chǎng)環(huán)境:
1、本地虛擬機(jī)CentOS7 下的docker環(huán)境。
2、docker下的redis、mysql已正常啟動(dòng),且虛擬機(jī)外可正常訪問(wèn)。
3、啟動(dòng)容器(SpringCloud 網(wǎng)關(guān)服務(wù))需要使用 docker下的redis、MySql。
問(wèn)題現(xiàn)象:
在啟動(dòng)容器時(shí),出現(xiàn)錯(cuò)誤異常c.n.d.s.t.d.RedirectingEurekaHttpClient:Request execution error和nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool,如下所示:
2018-09-13 12:53:25.742 ERROR 1 --- [ main] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error
com.sun.jersey.api.client.ClientHandlerException: java.net.NoRouteToHostException: No route to host (Host unreachable)
at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar!/:1.19.1]
at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123) ~[jersey-client-1.19.1.jar!/:1.19.1]
at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.4.12.jar!/:1.4.12]
at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar!/:1.19.1]
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) ~[jersey-client-1.19.1.jar!/:1.19.1]
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.19.1.jar!/:1.19.1]
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:509) ~[jersey-client-1.19.1.jar!/:1.19.1]
at com.netflix.discovery.shared.transport.jersey.AbstractJerseyEurekaHttpClient.getApplicationsInternal(AbstractJerseyEurekaHttpClient.java:194) ~[eureka-client-1.4.12.jar!/:1.4.12]
……
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-09-13 12:53:29.150 ERROR 1 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.context.ApplicationContextException: Failed to start bean 'org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration'; nested exception is org.springframework.data
.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:50) ~[spring-context-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:348) ~[spring-context-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:151) ~[spring-context-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:114) ~[spring-context-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:879) ~[spring-context-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) ~[spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) ~[spring-context-4.3.7.RELEASE.jar!/:4.3.7.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE]
at com.wondersoft.gateway.GatewayApplication.main(GatewayApplication.java:31) [classes!/:1.5.2.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [gateway-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [gateway-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [gateway-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [gateway-1.5.2.RELEASE.jar:1.5.2.RELEASE]
問(wèn)題分析:
1、異常c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error
com.sun.jersey.api.client.ClientHandlerException: java.net.NoRouteToHostException: No route to host (Host unreachable),是處于網(wǎng)關(guān)在連接另外一個(gè)服務(wù)(注冊(cè)中心)時(shí),拋出的異常,屬于無(wú)法正常路由連接到注冊(cè)中心服務(wù)。
2、異常nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool,是處于正在連接redis時(shí),拋出的異常,屬于無(wú)法連接redis。
基于上述兩點(diǎn),問(wèn)題屬于容器間網(wǎng)絡(luò)通信問(wèn)題。
解決方法:
1、關(guān)閉防火墻。
使用root用戶,執(zhí)行 systemctl stop firewalld.service 命令,關(guān)閉防火墻。
2、重新啟動(dòng) 網(wǎng)關(guān)服務(wù)容器,正常啟動(dòng),則問(wèn)題解決。
3、如果啟動(dòng)時(shí),出現(xiàn)錯(cuò)誤
iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8111 -j DNAT,如下圖:
[root@cen7 data]# docker start gateway
Error response from daemon: Cannot start container gateway: iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8111 -j DNAT --to-destination 172.17.0.5:8111 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1)
Error: failed to start containers: [gateway]
則需要重建docker0網(wǎng)絡(luò)恢復(fù),按順序執(zhí)行如下命令,即可。
pkill docker
iptables -t nat -F
ifconfig docker0 down
brctl delbr docker0
docker -d
service docker restart
執(zhí)行過(guò)程如下:
[root@cen7 data]# pkill docker
[root@cen7 data]# iptables -t nat -F
[root@cen7 data]# ifconfig docker0 down
[root@cen7 data]# brctl delbr docker0
[root@cen7 data]# docker -d
INFO[0000] Listening for HTTP on unix (/var/run/docker.sock)
INFO[0000] [graphdriver] using prior storage driver "devicemapper"
WARN[0000] Running modprobe bridge nf_nat failed with message: , error: exit status 1
INFO[0000] Loading containers: start.
..............
INFO[0000] Loading containers: done.
INFO[0000] Daemon has completed initialization
INFO[0000] Docker daemon commit=786b29d execdriver=native-0.2 graphdriver=devicemapper version=1.7.1
^CINFO[0061] Processing signal 'interrupt'
[root@cen7 data]# systemctl restart docker
4、啟動(dòng)所需容器即可。