Docker容器中的Zuul重定向问题

发布于 2025-02-11 14:50:13 字数 3887 浏览 2 评论 0原文

我正在使用Grail应用程序,并编写了几个微服务,可以通过Grails应用程序访问。请求来自在4000端口上的Gateway到网关。现在,基于请求,网关将将请求重定向到诸如AUTH,NOTIFIFY,REPORT等各个服务,这些都是微服务。

现在,我正在旋转Docker容器中的微服务,服务发现位于8761,配置服务器位于8888上,Zuul Gateway位于4000端口上。通知服务在8755端口上运行,当请求到达Gateway以进行通知时,它首先将通知重定向到对我有效的通知,根据应用程序流,通知将验证令牌,因此它将调用在8282端口上暴露的auth Service。通知的那一刻是通过Gateway发送请求的失败...

当我在Docker容器中启动服务时,所有服务都在'Localhost'上启动并运行时,一切都在工作,因为容器将无法识别Localhost更改Zuul Conifguration如下

zuul:
  ignoredServices: '*'
  host:
    connect-timeout-millis: 120000
    socket-timeout-millis: 120000
  routes:
    notification-service:
      path: /notification/**
      url: http://${NOTIFICATION_SERVICE_HOST:127.0.0.1}:8755
      stripPrefix: true
      sensitiveHeaders:
    report-service:
      path: /report/**
      url: http://${REPORT_SERVICE_HOST:127.0.0.1}:8762
      stripPrefix: true
      sensitiveHeaders:
    p2p-auth-service:
      path: /authService/**
      url: http://${AUTH_SERVICE_HOST:172.17.0.1}:8282
      stripPrefix: true
      sensitiveHeaders:

所需的通知时,它正确地将其添加,这意味着我添加了Notification_service_host被我通过的IP所取代。但是,当我给出了常量的auth_service_host的调用auth服务时,也不会发生同样的情况。始终使用500个空消息失败。

我的docker组成的文件如下所示,

version: '3.7'

services:

  configserver:
    image: config
    container_name: configserver
    ports:
      - "8888:8888"
    networks:
      - bridge-abc
    environment:
      SPRING.PROFILES.ACTIVE: native
      EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://registry:8761/eureka

  registry:
    image: registry
    container_name: registry
    ports:
      - "8762:8761"
    networks:
      - bridge-abc
    depends_on:
      - configserver
    restart: on-failure
    environment:
      SPRING.PROFILES.ACTIVE: dev
      SPRING.CLOUD.CONFIG.URI: http://configserver:8888/
      EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://registry:8761/eureka

  gateway:
    image: gateway
    container_name: gateway
    ports:
      - "4000:4000"
    networks:
      - bridge-abc
    depends_on:
      - configserver
      - registry
    restart: on-failure
    environment:
      SPRING.PROFILES.ACTIVE: dev
      SPRING.CLOUD.CONFIG.URI: http://configserver:8888/
      EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://registry:8761/eureka
      REGISTRY_SERVER: registry
      REGISTRY_PORT: 8761
      NOTIFICATION_SERVICE_HOST: 172.17.0.1
      AUTH_SERVICE_HOST: 172.17.0.1

  authservice:
    image: auth-service
    container_name: authservice
    ports:
      - "8282:8282"
    networks:
      - bridge-abc
    depends_on:
      - configserver
      - registry
    restart: on-failure
    environment:
      SPRING.PROFILES.ACTIVE: dev
      SPRING.CLOUD.CONFIG.URI: http://configserver:8888/
      EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://registry:8761/eureka
      DB_HOST: 1.2.3.4
      DB_PORT: 1234
      DB_NAME: my-db-name
      DB_USERNAME: user
      DB_PASSWORD: password
      REGISTRY_SERVER: registry
      REGISTRY_PORT: 8761
      

  notification:
    image: notification-service
    container_name: notification
    ports:
      - 8755:8755
    networks:
      - bridge-perfios
    depends_on:
      - configserver
      - registry
    restart: on-failure
    environment:
      SPRING.PROFILES.ACTIVE: dev
      SPRING.CLOUD.CONFIG.URI: http://configserver:8888/
      EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://registry:8761/eureka
      REGISTRY_SERVER: registry
      REGISTRY_PORT: 8761
      DB_HOST: 1.2.3.4
      DB_PORT: 1234
      DB_NAME: my-db-name
      DB_USERNAME: user
      DB_PASSWORD: password
      REGISTRY_SERVER: registry
      REGISTRY_PORT: 8761

在上面的docker组成文件中,我通过notification_service_host和auth_service_host值为172.17.0.1,以便容器可以互相交谈,常数auth_service_host不起作用,并且始终假设不替代127.0.0.0.1(常数值得替换为127.0.0.1(常数值172.17.0.1,始终假设为127.0.0.1),而常数notification_service_host正常工作,并且该常数值替换为172.17.0.1

我不确定这里有什么问题,或者请建议某些配置是否缺失或我丢失任何错误。

I am using Grail application and couple of microservices are written which is accessible by grails application. Request comes from grails to Gateway which is on 4000 port. Now based on request gateway will redirect request to respectives services like auth, notification, report, these are the microservices.

Now I am spinning up microservices in docker container, service discovery is on 8761,config server is on 8888, zuul gateway is on 4000 port. Notification service is running on 8755 port and when request comes to Gateway for notification, it redirect first to notification which is working fine for me, as per application flow, notification will validate token hence it will call auth service which is exposed on 8282 port. The moment notification is sending request to auth via gateway its failing ...

everything is working when all services are up and running on 'localhost' this issue is happening when I am starting services in docker container as container will not recognize localhost hence I am changing zuul conifguration as below

zuul:
  ignoredServices: '*'
  host:
    connect-timeout-millis: 120000
    socket-timeout-millis: 120000
  routes:
    notification-service:
      path: /notification/**
      url: http://${NOTIFICATION_SERVICE_HOST:127.0.0.1}:8755
      stripPrefix: true
      sensitiveHeaders:
    report-service:
      path: /report/**
      url: http://${REPORT_SERVICE_HOST:127.0.0.1}:8762
      stripPrefix: true
      sensitiveHeaders:
    p2p-auth-service:
      path: /authService/**
      url: http://${AUTH_SERVICE_HOST:172.17.0.1}:8282
      stripPrefix: true
      sensitiveHeaders:

When request comes to notification, its coming properly, it mean constant which I added NOTIFICATION_SERVICE_HOST is replaced by the IP which I am passing. But same is not happening when its calling auth service where I have given constant AUTH_SERVICE_HOST. Always it is failing with 500 null message.

my docker compose file is as below

version: '3.7'

services:

  configserver:
    image: config
    container_name: configserver
    ports:
      - "8888:8888"
    networks:
      - bridge-abc
    environment:
      SPRING.PROFILES.ACTIVE: native
      EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://registry:8761/eureka

  registry:
    image: registry
    container_name: registry
    ports:
      - "8762:8761"
    networks:
      - bridge-abc
    depends_on:
      - configserver
    restart: on-failure
    environment:
      SPRING.PROFILES.ACTIVE: dev
      SPRING.CLOUD.CONFIG.URI: http://configserver:8888/
      EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://registry:8761/eureka

  gateway:
    image: gateway
    container_name: gateway
    ports:
      - "4000:4000"
    networks:
      - bridge-abc
    depends_on:
      - configserver
      - registry
    restart: on-failure
    environment:
      SPRING.PROFILES.ACTIVE: dev
      SPRING.CLOUD.CONFIG.URI: http://configserver:8888/
      EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://registry:8761/eureka
      REGISTRY_SERVER: registry
      REGISTRY_PORT: 8761
      NOTIFICATION_SERVICE_HOST: 172.17.0.1
      AUTH_SERVICE_HOST: 172.17.0.1

  authservice:
    image: auth-service
    container_name: authservice
    ports:
      - "8282:8282"
    networks:
      - bridge-abc
    depends_on:
      - configserver
      - registry
    restart: on-failure
    environment:
      SPRING.PROFILES.ACTIVE: dev
      SPRING.CLOUD.CONFIG.URI: http://configserver:8888/
      EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://registry:8761/eureka
      DB_HOST: 1.2.3.4
      DB_PORT: 1234
      DB_NAME: my-db-name
      DB_USERNAME: user
      DB_PASSWORD: password
      REGISTRY_SERVER: registry
      REGISTRY_PORT: 8761
      

  notification:
    image: notification-service
    container_name: notification
    ports:
      - 8755:8755
    networks:
      - bridge-perfios
    depends_on:
      - configserver
      - registry
    restart: on-failure
    environment:
      SPRING.PROFILES.ACTIVE: dev
      SPRING.CLOUD.CONFIG.URI: http://configserver:8888/
      EUREKA.CLIENT.SERVICEURL.DEFAULTZONE: http://registry:8761/eureka
      REGISTRY_SERVER: registry
      REGISTRY_PORT: 8761
      DB_HOST: 1.2.3.4
      DB_PORT: 1234
      DB_NAME: my-db-name
      DB_USERNAME: user
      DB_PASSWORD: password
      REGISTRY_SERVER: registry
      REGISTRY_PORT: 8761

In above docker compose file I am passing NOTIFICATION_SERVICE_HOST and AUTH_SERVICE_HOST value as 172.17.0.1 so that container can talk to each other, constant AUTH_SERVICE_HOST is not working and always assuming 127.0.0.1 (constant value is not replaced by 172.17.0.1, always it is assuming 127.0.0.1) whereas constant NOTIFICATION_SERVICE_HOST works properly and this constant value is replaced by 172.17.0.1

I am not sure what is wrong here or please suggest if some configuration is missing or I am doing any mistakes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城月光淡如水﹏ 2025-02-18 14:50:14

我已经解决了这个问题,每个微服务都在验证令牌的每个微服务中都在发生问题。它将转到网关,然后请求将重定向到AUTH Service。在每个微服务中解析DNS名称时,它使用默认IP IE映射IE 127.0.0.1。我已经更改了每个米光服务的容器 /etc /主机

172.17.0.1 'my domain name' 

,这解决了我的问题。
现在,我需要了解如何使其通用,以便我卸下容器并再次运行Docker-compose,然后以下详细信息应自动更新到容器的主机文件,

172.17.0.1 'my domain name'

任何建议都非常明显。

I have resolved this issue, problem was happening in each microservice where it was validating token, to do this; it will go to gateway and then request will redirect to auth service. While resolving DNS name in each microservice it was mapping with default IP i.e. 127.0.0.1. I have changed each miceroservice's container /etc/hosts as below

172.17.0.1 'my domain name' 

and this resolved my issue.
Now I need to understand how I can make it generic so that if I remove container and run again docker-compose up then below details should auto update to container's host file

172.17.0.1 'my domain name'

Any suggestion is much appreciable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文