首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Multi Container Docker应用程序-容器之间的连接被拒绝

Multi Container Docker应用程序-容器之间的连接被拒绝
EN

Stack Overflow用户
提问于 2021-01-11 04:40:48
回答 1查看 410关注 0票数 0

因此,我有一个多容器应用程序,它的前端和后端API都位于API网关后面。目前,当通过各自的命令(Java spring应用程序和Angular前端)独立启动时,所有应用程序都可以正常工作。但是,当我通过docker-compose up启动应用程序时,没有一个应用程序可以相互通信(连接被拒绝)。

Gateway只是一个基本的spring-cloud-gateway入门应用程序,它将请求路由到正确的应用程序。这是使用以下代码配置的:

代码语言:javascript
复制
    @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://localhost:5000/api/categories发送HTTP GET请求后,网关应用程序中会产生此错误消息。

代码语言:javascript
复制
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或邮递员单独点击每个应用程序,我可以通过浏览器访问前端。

Docker-Compose.yml

代码语言:javascript
复制
version: "3"

services:
  api:
    build: ./api
    ports:
      - "5001:5001"
  gateway:
    build: ./gateway
    ports:
      - "5000:5000"
  frontend:
    build: ./frontend
    ports:
      - "4200:80"

Dockerfiles

网关

代码语言:javascript
复制
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"]

API接口

代码语言:javascript
复制
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"]

前端

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-11 08:27:37

感谢@DavidMaze让我注意到这一点。

看起来我有点傻。我的应用程序正在向localhost发送网络请求。当它们都在容器之外运行时,这种方法工作得很好。当在容器中时,它们需要向其他容器的名称发送请求。例如:

app_1在端口8080上运行app_2在端口5000上运行

在docker外部运行时,app_1可通过http://localhost:5000向app_2发送网络请求。这在容器内不起作用,因为容器中的localhost:5000上没有运行任何东西。相反,它需要引用另一个容器,例如:http://app_2:5000

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65658289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档