gpt4 book ai didi

spring-boot - 同一应用程序无法通过Docker Compose发送HTTP请求

转载 作者:行者123 更新时间:2023-12-02 19:20:01 25 4
gpt4 key购买 nike

Spring 靴

改造2

docker

在我的docker-compose文件中:

version: "3"

services:
server:
image: asubscriber/eshop:1.0.1
build: .
ports:
- 8090:8090
  • 从控制台运行我的应用程序( Spring 启动),如下所示:

    java -jar build / libs / eshop-1.0.1.jar eshop-1.0.1.jar

  • 运行成功。并成功向http请求。这里的日志:
    [INFO ] 2020-03-14 21:41:09.480 [OkHttp http://localhost:8091/...] OkHttpClient - --> POST http://localhost:8091/api/v1/notifyEmail?emailTo=a_subscriber%40mail.ru&action=order_update&orderId=38 http/1.1
    [INFO ] 2020-03-14 21:41:09.480 [OkHttp http://localhost:8091/...] OkHttpClient - Content-Length: 0
    [INFO ] 2020-03-14 21:41:09.480 [OkHttp http://localhost:8091/...] OkHttpClient -
    [INFO ] 2020-03-14 21:41:09.481 [OkHttp http://localhost:8091/...] OkHttpClient -
    [INFO ] 2020-03-14 21:41:09.481 [OkHttp http://localhost:8091/...] OkHttpClient - --> END POST (0-byte body)
    [INFO ] 2020-03-14 21:41:12.732 [OkHttp http://localhost:8091/...] OkHttpClient - <-- 200 http://localhost:8091/api/v1/notifyEmail?emailTo=a_subscriber%40mail.ru&action=order_update&orderId=38 (3250ms)
    [INFO ] 2020-03-14 21:41:12.732 [OkHttp http://localhost:8091/...] OkHttpClient - Content-Type: application/json
    [INFO ] 2020-03-14 21:41:12.732 [OkHttp http://localhost:8091/...] OkHttpClient - Transfer-Encoding: chunked
    [INFO ] 2020-03-14 21:41:12.732 [OkHttp http://localhost:8091/...] OkHttpClient - Date: Sat, 14 Mar 2020 19:41:12 GMT
    [INFO ] 2020-03-14 21:41:12.733 [OkHttp http://localhost:8091/...] OkHttpClient - Keep-Alive: timeout=60
    [INFO ] 2020-03-14 21:41:12.733 [OkHttp http://localhost:8091/...] OkHttpClient - Connection: keep-alive
    [INFO ] 2020-03-14 21:41:12.734 [OkHttp http://localhost:8091/...] OkHttpClient -
    [INFO ] 2020-03-14 21:41:12.734 [OkHttp http://localhost:8091/...] OkHttpClient - {"code":0,"message":"Success"}

    真好很好成功发送POST http请求。
  • 现在我通过docker compose运行 SAME我的应用程序

    docker-组成

  • 成功运行应用程序。
    但是,如果我想创建http请求,则会收到错误消息:
    Failed to connect to localhost/127.0.0.1:8091


    server_1 | [INFO ] 2020-03-14 19:37:29.526 [OkHttp http://localhost:8091/...] OkHttpClient - --> POST http://localhost:8091/api/v1/notifyEmail?emailTo=a_subscriber%40mail.ru&action=order_update&orderId=4 http/1.1
    server_1 | [INFO ] 2020-03-14 19:37:29.527 [OkHttp http://localhost:8091/...] OkHttpClient - Content-Length: 0
    server_1 | [INFO ] 2020-03-14 19:37:29.527 [OkHttp http://localhost:8091/...] OkHttpClient -
    server_1 | [INFO ] 2020-03-14 19:37:29.527 [OkHttp http://localhost:8091/...] OkHttpClient -
    server_1 | [INFO ] 2020-03-14 19:37:29.527 [OkHttp http://localhost:8091/...] OkHttpClient - --> END POST (0-byte body)
    server_1 | [DEBUG] 2020-03-14 19:37:29.529 [http-nio-8090-exec-5] SQL - select user0_.id as id1_3_, user0_.active as active2_3_, user0_.created as created3_3_, user0_.password as password4_3_, user0_.updated as updated5_3_, user0_.username as username6_3_ from usr user0_ where user0_.username=?
    server_1 | [DEBUG] 2020-03-14 19:37:29.530 [http-nio-8090-exec-5] SQL - select roles0_.user_id as user_id1_2_0_, roles0_.role as role2_2_0_ from user_roles roles0_ where roles0_.user_id=?
    server_1 | [DEBUG] 2020-03-14 19:37:29.530 [http-nio-8090-exec-5] SQL - select orders0_.user_id as user_id7_1_0_, orders0_.id as id1_1_0_, orders0_.id as id1_1_1_, orders0_.category_id as category6_1_1_, orders0_.created as created2_1_1_, orders0_.description as descript3_1_1_, orders0_.name as name4_1_1_, orders0_.updated as updated5_1_1_, orders0_.user_id as user_id7_1_1_ from orders orders0_ where orders0_.user_id=?
    server_1 | [DEBUG] 2020-03-14 19:37:29.531 [http-nio-8090-exec-5] SQL - select orders0_.id as id1_1_, orders0_.category_id as category6_1_, orders0_.created as created2_1_, orders0_.description as descript3_1_, orders0_.name as name4_1_, orders0_.updated as updated5_1_, orders0_.user_id as user_id7_1_ from orders orders0_ where orders0_.user_id=?
    server_1 | [DEBUG] 2020-03-14 19:37:29.536 [http-nio-8090-exec-5] SQL - select category0_.id as id1_0_0_, category0_.created as created2_0_0_, category0_.description as descript3_0_0_, category0_.name as name4_0_0_, category0_.updated as updated5_0_0_, orders1_.id as id1_1_1_, orders1_.category_id as category6_1_1_, orders1_.created as created2_1_1_, orders1_.description as descript3_1_1_, orders1_.name as name4_1_1_, orders1_.updated as updated5_1_1_, orders1_.user_id as user_id7_1_1_, user2_.id as id1_3_2_, user2_.active as active2_3_2_, user2_.created as created3_3_2_, user2_.password as password4_3_2_, user2_.updated as updated5_3_2_, user2_.username as username6_3_2_, roles3_.user_id as user_id1_2_3_, roles3_.role as role2_2_3_ from category category0_ left outer join orders orders1_ on category0_.id=orders1_.category_id left outer join usr user2_ on orders1_.user_id=user2_.id left outer join user_roles roles3_ on user2_.id=roles3_.user_id where category0_.id=?
    server_1 | [INFO ] 2020-03-14 19:37:29.538 [OkHttp http://localhost:8091/...] OkHttpClient - <-- HTTP FAILED: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:8091
    server_1 | [ERROR] 2020-03-14 19:37:29.539 [OkHttp http://localhost:8091/...] DefaultRestClientCallback - onFailure: Error: Failed to connect to localhost/127.0.0.1:8091
    server_1 | java.net.ConnectException: Failed to connect to localhost/127.0.0.1:8091
    server_1 | at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:265) ~[okhttp-3.14.4.jar!/:?]
    server_1 | at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:183) ~[okhttp-3.14.4.jar!/:?]
    server_1 | at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.java:224) ~[okhttp-3.14.4.jar!/:?]
    server_1 | at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.java:108) ~[okhttp-3.14.4.jar!/:?]

    为什么同一个应用程序无法从Docker运行?

    最佳答案

    将此文件添加到docker-compose文件中的服务中(仅网络部分)

    version: "3"

    services:
    server:
    image: asubscriber/eshop:1.0.1
    build:
    context: .

    network_mode: "host"

    expose:
    - 9080


    这是一个使它起作用的答案,最好阅读 this,如Sergei Petunin在上面的评论中所述。

    关于spring-boot - 同一应用程序无法通过Docker Compose发送HTTP请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60686674/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com