所以我有一个多容器应用程序,前端和后端 API 都位于 API 网关后面。目前,当通过各自的命令(Java spring 应用程序和 Angular 前端)独立启动时,所有应用程序都可以正常工作。但是,当我通过 启动应用程序时docker-compose up
,所有应用程序都无法相互通信(连接被拒绝)。
网关只是一个基本的 spring-cloud-gateway 启动应用程序,它将请求路由到正确的应用程序。这是使用以下代码配置的:
@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("api", route -> route.path("/api/**").uri("http://localhost:5001"))
.route("front-end", route -> route.path("/**").uri("http://localhost:4200"))
.build();
}
向 发送 HTTP GET 请求后http://localhost:5000/api/categories
,网关应用程序中产生了此错误消息。
api-gateway_1 | 2021-01-11 00:05:14.514 ERROR 1 --- [or-http-epoll-5] a.w.r.e.AbstractErrorWebExceptionHandler : [d29e1cbf-1] 500 Server Error for HTTP GET "/api/categories"
api-gateway_1 |
api-gateway_1 | io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:5001
api-gateway_1 | Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
api-gateway_1 | Error has been observed at the following site(s):
api-gateway_1 | |_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
api-gateway_1 | |_ checkpoint ⇢ HTTP GET "/api/categories" [ExceptionHandlingWebHandler]
api-gateway_1 | Stack trace:
api-gateway_1 | Caused by: java.net.ConnectException: finishConnect(..) failed: Connection refused
api-gateway_1 | at io.netty.channel.unix.Errors.throwConnectException(Errors.java:124) ~[netty-transport-native-unix-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1 | at io.netty.channel.unix.Socket.finishConnect(Socket.java:251) ~[netty-transport-native-unix-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1 | at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.doFinishConnect(AbstractEpollChannel.java:673) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1 | at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.finishConnect(AbstractEpollChannel.java:650) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1 | at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.epollOutReady(AbstractEpollChannel.java:530) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1 | at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:470) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1 | at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:378) ~[netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar!/:4.1.55.Final]
api-gateway_1 | at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1 | at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1 | at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.55.Final.jar!/:4.1.55.Final]
api-gateway_1 | at java.base/java.lang.Thread.run(Unknown Source) ~[na:na]
我尝试定义自己的网络,但这不会改变任何内容,因此我将容器保留在运行时创建的默认网络上docker-compose up
。
我可以通过cURL
或通过 Postman独立点击每个应用程序,我可以通过浏览器访问前端。
version: "3"
services:
api:
build: ./api
ports:
- "5001:5001"
gateway:
build: ./gateway
ports:
- "5000:5000"
frontend:
build: ./frontend
ports:
- "4200:80"
FROM openjdk:11 as build
COPY . .
RUN ./gradlew build --parallel
FROM openjdk:11-jre-slim as runtime
COPY --from=build /build/libs/gateway-0.0.1-SNAPSHOT.jar /usr/app/
WORKDIR /usr/app
ENTRYPOINT ["java", "-jar", "gateway-0.0.1-SNAPSHOT.jar"]
FROM openjdk:11 as build
COPY . .
RUN ./gradlew build --parallel
FROM openjdk:11-jre-slim as runtime
COPY --from=build /build/libs/api-0.0.1-SNAPSHOT.jar /usr/app/
WORKDIR /usr/app
ENTRYPOINT ["java", "-jar", "api-0.0.1-SNAPSHOT.jar"]
FROM node:12.7-alpine AS build
WORKDIR usr/src/app
COPY package.json package-lock.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:1.17.1-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/dist/awards-frontend /usr/share/nginx/html
感谢@DavidMaze让我注意到这一点。
看来我是有点傻了。我的应用程序正在向本地主机发送网络请求。当它们都在容器外运行时,这很好用。在容器中时,他们需要向其他容器的名称发送请求。例如:
app_1 在端口 8080 上运行 app_2 在端口 5000 上运行
在 docker 之外运行时,app_1 可以通过http://localhost:5000
. 这在容器内不起作用,因为该容器中没有运行任何localhost:5000
内容。相反,它将需要引用另一个容器,例如:http://app_2:5000
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句