Reputation: 4076
I have 2 microservices, each one deployed in its own docker container. I use docker compose in order to deploy them and make microservice1 aware of microservice2. Each one is build in its own maven project.
Both microservices are deployed successfully, the docker containers are created and started and I can access each base root endpoint.
The issue is that from microservice1, I want to access trough /remote url the base root from microservice2. This does not work and I get 404 when the call reaches the REST client that makes the get call to microservice2 which is in the remote()
method bellow, see bold, see stacktrace bellow.
microservice1 exposes 2 REST endpoints:
@RequestMapping("/")
public String home() {
System.out.println("Hello Docker World " + 1);
return "Hello Docker World " + 1;
}
@GetMapping("/remote")
public String remote() {
RestTemplate restTemplate = new RestTemplate();
return restTemplate.getForObject("http://localhost:8080/maven-app2/", String.class);
}
microservice2 exposes 1 REST endpoint:
@GetMapping("/")
public String home() {
System.out.println("Hello Docker World " + 2);
return "Hello Docker World " + 2;
}
Dockerfile for microservice1
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.mhp.App"]
Dockerfile for microservice2
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.mhp.App"]
docker-compose file in microservice1:
version: "2"
services:
microservice2:
image: microsdocker2/maven-app2:latest
expose:
- 8080
ports:
- 0.0.0.0:8080:8080/tcp
microservice1:
image: microsdocker1/maven-app1:latest
ports:
- 0.0.0.0:8090:8080/tcp
depends_on:
- microservice2
links:
- microservice2
after building and installing each image I execute the docker-compose up
and I get both applications deployed successfully and I can access the base root endpoint of each microservice.
...
microservice1_1_2f0ac966a94e | 2018-11-26 14:26:08.955 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/maven-app1'
microservice1_1_2f0ac966a94e | 2018-11-26 14:26:08.960 INFO 1 --- [ main] com.mhp.App : Started App in 6.63 seconds (JVM running for 7.358)
...
microservice2_1_fa2376655f8c | 2018-11-26 14:26:08.090 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/maven-app2'
microservice2_1_fa2376655f8c | 2018-11-26 14:26:08.101 INFO 1 --- [ main] com.mhp.App : Started App in 6.821 seconds (JVM running for 7.75)
stacktrace :
microservice1_1_2f0ac966a94e | 2018-11-27 08:39:33.720 ERROR 1 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/maven-app1] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 404 null] with root cause
microservice1_1_2f0ac966a94e |
microservice1_1_2f0ac966a94e | org.springframework.web.client.HttpClientErrorException: 404 null
microservice1_1_2f0ac966a94e | at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:730) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:688) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:644) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:296) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at com.mhp.App.remote(App.java:29) ~[app/:na]
microservice1_1_2f0ac966a94e | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
microservice1_1_2f0ac966a94e | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
microservice1_1_2f0ac966a94e | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
microservice1_1_2f0ac966a94e | at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
microservice1_1_2f0ac966a94e | at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493) [tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800) [tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806) [tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498) [tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
microservice1_1_2f0ac966a94e | at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
microservice1_1_2f0ac966a94e | at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.34.jar:8.5.34]
microservice1_1_2f0ac966a94e | at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
versions:
spring-boot version 2.0.5.RELEASE
docker version 18.09.0
docker compose version 1.23.1
Upvotes: 2
Views: 3213
Reputation: 8060
Use service name microservice2
instead of localhost
@GetMapping("/remote")
public String remote() {
RestTemplate restTemplate = new RestTemplate();
return restTemplate.getForObject("http://microservice2:8080/maven-app2/", String.class);
}
https://docs.docker.com/compose/networking/
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
Upvotes: 4