我有2个微服务,每个微服务都部署在自己的docker容器中。 我使用docker compose来部署它们并使microservice1意识到microservice2。每个人都在自己的Maven项目中构建。
两个微服务都已成功部署,创建并启动了docker容器,我可以访问每个基本根端点。
问题是从microservice1,我想从microservice2访问槽/远程url的基本根。这不起作用,当调用到达在remote()
方法下面的microservice2进行get调用的REST客户端时,我得到404,请参见下面的粗体,请参见stacktrace。 < / strong>
microservice1 公开了2个REST端点:
@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 公开了1个REST端点:
@GetMapping("/")
public String home() {
System.out.println("Hello Docker World " + 2);
return "Hello Docker World " + 2;
}
用于微服务的Dockerfile1
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"]
用于微服务2的Dockerfile
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"]
微服务1中的docker-compose 文件:
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
在构建和安装每个映像之后,我执行docker-compose up
,并且成功部署了两个应用程序,并且可以访问每个微服务的基本根端点。
...
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 |在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]
版本:
spring-boot version 2.0.5.RELEASE
docker version 18.09.0
docker compose version 1.23.1
答案 0 :(得分:1)
使用服务名称microservice2
代替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/
默认情况下,Compose为您的应用设置单个网络。每 服务的容器加入默认网络,并且两者都是 可被该网络上的其他容器访问,并被 它们的主机名与容器名称相同。