【问题标题】:Feign HATEOS response contains wrong server,port informationFeign HATEOS 响应包含错误的服务器、端口信息
【发布时间】:2018-09-29 17:31:06
【问题描述】:

我正在开发 Spring Cloud 并使用 Josh Long 的示例项目

Bootiful Microservice by Josh Long

有一个 API 网关 reservation-client 使用来自服务 reservation-service 的数据,该服务提供 HATEOAS 响应,然后将其转换为简单的 JSON 响应。

@RestController
@RequestMapping("/reservations")
class ReservationApiGateway {

方法:

@HystrixCommand(fallbackMethod = "fallback")
@RequestMapping(method = RequestMethod.GET, value = "/names")
public Collection<String> names() {
    return this.reservationReader
            .read()
            .getContent()
            .stream()
            .map(Reservation::getReservationName)
            .collect(Collectors.toList());
}

我修改它以向我转发这样的 HATEOAS 响应。

@HystrixCommand(fallbackMethod = "fallback")
@RequestMapping(method = RequestMethod.GET, value = "/names")
public Resources<Resource<Reservation>> names() {
    return this.reservationReader
            .read();
}

这给了我一个 HATEAOS 响应,但链接都来自 reservation-service - 。

  "_links" : {

    "self" : {

      "href" : "**http://192.168.0.3:7000/reservations/1**"

    },

    "reservation" : {

      "href" : "http://192.168.0.3:7000/reservations/1"

    }

  }

如何确保 Feign 更新 API 网关的服务器和端口的链接? - http://192.168.0.3:9999/reservations/1

来自预订客户端的相同响应(与预订服务相同):

{

  "_embedded" : {

    "reservations" : [ {

      "reservationName" : "Josh",

      "_links" : {

        "self" : {

          "href" : "http://192.168.0.3:7000/reservations/1"

        },

        "reservation" : {

          "href" : "http://192.168.0.3:7000/reservations/1"

        }

      }

    }, {

      "reservationName" : "Dr. Johnson",

      "_links" : {

        "self" : {

          "href" : "http://192.168.0.3:7000/reservations/2"

        },

        "reservation" : {

          "href" : "http://192.168.0.3:7000/reservations/2"

        }

      }

    }, {

      "reservationName" : "Dr. Syer",

      "_links" : {

        "self" : {

          "href" : "http://192.168.0.3:7000/reservations/3"

        },

        "reservation" : {

          "href" : "http://192.168.0.3:7000/reservations/3"

        }

      }

    }, {

      "reservationName" : "Dr. Pollack",

      "_links" : {

        "self" : {

          "href" : "http://192.168.0.3:7000/reservations/4"

        },

        "reservation" : {

          "href" : "http://192.168.0.3:7000/reservations/4"

        }

      }

    } ]

  },

  "_links" : {

    "self" : {

      "href" : "http://192.168.0.3:7000/reservations{?page,size,sort}",

      "templated" : true

    },

    "profile" : {

      "href" : "http://192.168.0.3:7000/profile/reservations"

    },

    "search" : {

      "href" : "http://192.168.0.3:7000/reservations/search"

    }

  }

}

【问题讨论】:

    标签: spring-boot spring-cloud netflix-eureka hateoas feign


    【解决方案1】:

    我想通了。

    解决方案是在 X-Forwarded-Host http 标头中。 X-Forwarded-Host 本质上告诉 Spring 任何具有此标头的 HATEOS 响应,链接中的主机和端口信息都应更新为 X-Forwarded-Host http 标头中提到的内容。

    所以,在 API Gateway 的预订客户端代码中,我添加了这个 sn-p,它拦截 Feign 对后端服务预订服务的调用,并将 http 标头添加到请求中。

    @Component
    class LanguageRequestInterceptor implements RequestInterceptor {
        private static final String X_FORWARDED_HOST = "X-Forwarded-Host";
    
        @Override
        public void apply(RequestTemplate requestTemplate) {
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (requestAttributes == null) {
                return;
            }
            HttpServletRequest request = requestAttributes.getRequest();
            if (request == null) {
                return;
            }
    
            requestTemplate.header(X_FORWARDED_HOST, "localhost:9999");
        }
    }
    

    现在,所有 HATOES 响应都有 API 网关的主机和端口信息,而不是后端 HATEOS 服务。

    【讨论】:

      猜你喜欢
      • 2022-11-27
      • 1970-01-01
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 2013-02-16
      • 2016-03-12
      • 2013-02-14
      • 2012-04-08
      相关资源
      最近更新 更多