Sping Boot 微服务中的Docker连接问题-嵌套异常为java.net,连接被拒绝:配置服务器

n7taea2i  于 2022-12-21  发布在  Java
关注(0)|答案(2)|浏览(243)

我修改了一个Spring Boot微服务的示例,我在Docker上运行所有服务时遇到了一个问题。
创建每个服务的映像文件后,我设计了一个docker-composite. yml来运行Docker上的所有服务。我注意到一些服务抛出关于configserver的ConnectException,但我不知道如何修复它。以下是为每个服务创建Docker映像的代码片段。
下面是我的docker-compose.yml

services:

  serviceregistry:
    image: 'microservicedailybuffer/serviceregistry:0.0.1'
    container_name: serviceregistry
    ports:
      - '8761:8761'
    networks:
      - backend

  configserver:
    image: 'microservicedailybuffer/configserver:0.0.1'
    container_name: configserver
    ports:
      - '9296:9296'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://configserver:9296/actuator/health" ]
      interval: 10s
      timeout: 5s
      retries: 5
    depends_on:
      - serviceregistry
    networks:
      - backend

  apigateway:
    image: 'microservicedailybuffer/apigateway:0.0.1'
    container_name: apigateway
    ports:
      - '9090:9090'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
      - CONFIG_SERVER_URL=configserver
      - ZIPKIN_URL=http://zipkin:9411
      - REDIS_URL=redis://redis:6379
    depends_on:
      - configserver
      - zipkin
      - redis
      - serviceregistry
    networks:
      - backend

  zipkin:
    image: openzipkin/zipkin
    ports:
      - "9411:9411"
    networks:
      - backend

  redis:
    image: redis
    ports:
      - "6379:6379"
    networks:
      - backend

  authservice:
    image: 'microservicedailybuffer/authservice:0.0.1'
    container_name: authservice
    ports:
      - '7777:7777'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
      - CONFIG_SERVER_URL=configserver
      - spring.datasource.url=jdbc:mysql://database:3306/userdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
      - spring.datasource.username=root
      - spring.datasource.password=ippavlova_1990
    depends_on:
      database:
        condition: service_healthy
      configserver:
        condition: service_started
    networks:
      - backend

  productservice:
    image: 'microservicedailybuffer/productservice:0.0.1'
    container_name: productservice
    ports:
      - '8081:8081'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
      - CONFIG_SERVER_URL=configserver
      - spring.datasource.url=jdbc:mysql://database:3306/productdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
      - spring.datasource.username=root
      - spring.datasource.password=ippavlova_1990
      - ZIPKIN_URL=http://zipkin:9411
    depends_on:
      database:
        condition: service_healthy
      configserver:
        condition: service_started
      zipkin:
        condition: service_healthy
    networks:
      - backend

  orderservice:
    image: 'microservicedailybuffer/orderservice:0.0.1'
    container_name: orderservice
    ports:
      - '8082:8082'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
      - CONFIG_SERVER_URL=configserver
      - spring.datasource.url=jdbc:mysql://database:3306/orderdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
      - spring.datasource.username=root
      - spring.datasource.password=ippavlova_1990
      - ZIPKIN_URL=http://zipkin:9411
    depends_on:
      database:
        condition: service_healthy
      configserver:
        condition: service_started
      zipkin:
        condition: service_healthy
    networks:
      - backend

  paymentservice:
    image: 'microservicedailybuffer/paymentservice:0.0.1'
    container_name: paymentservice
    ports:
      - '8083:8083'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
      - CONFIG_SERVER_URL=configserver
      - spring.datasource.url=jdbc:mysql://database:3306/paymentdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
      - spring.datasource.username=root
      - spring.datasource.password=ippavlova_1990
      - ZIPKIN_URL=http://zipkin:9411
    depends_on:
      database:
        condition: service_healthy
      configserver:
        condition: service_started
      zipkin:
        condition: service_healthy
    networks:
      - backend

  database:
    container_name: mysql-database
    image: 'mysql:latest'
    ports:
      - "3306:3306"
    restart: always
    environment:
      #MYSQL_USER: root
      MYSQL_PASSWORD: ippavlova_1990
      MYSQL_ROOT_PASSWORD: ippavlova_1990
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - backend
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 20s
      retries: 10

networks:
  backend:
    driver: bridge

volumes:
  db-data:

当我通过docker-compose up运行docker-compose文件时,我在api网关中遇到了一些问题。

    • Api网关问题**
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://configserver:9296/API-GATEWAY/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)

我如何解决所有这些问题?
以下是回购协议:Link
下面是基于git的系统文件:Link

5uzkadbs

5uzkadbs1#

您必须更新application.ymldocker-compose.yml文件。
应存在这些行以覆盖EUREKA_SERVER_ADDRESS

eureka:
  instance:
    prefer-ip-address: true
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: ${EUREKA_SERVER_ADDRESS:http://localhost:8761/eureka}

要在容器内进行通信,你需要使用一个公共网络。所以它应该是这样的。

serviceregistry:
    image: 'microservicedailybuffer/serviceregistry:0.0.1'
    container_name: serviceregistry
    ports:
      - '8761:8761'
    networks:
      - backend

  configserver:
    image: 'microservicedailybuffer/configserver:0.0.1'
    container_name: configserver
    ports:
      - '9296:9296'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://configserver:9296/actuator/health" ]
      interval: 10s
      timeout: 5s
      retries: 5
    depends_on:
      - serviceregistry
    networks:
      - backend
bqf10yzr

bqf10yzr2#

修改如下所示的API网关后,问题消失了。

apigateway:
    image: 'microservicedailybuffer/apigateway:0.0.1'
    container_name: apigateway
    ports:
      - '9090:9090'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
      - CONFIG_SERVER_URL=configserver
      - ZIPKIN_URL=http://zipkin:9411
      - REDIS_URL=redis://redis:6379
    depends_on:
      configserver:
        condition: service_healthy
      zipkin:
        condition: service_healthy
      redis:
        condition: service_started
    networks:
      - backend

相关问题