我正在开发Spring Cloud并使用Josh Long的示例项目
Bootiful Microservice by Josh Long
有一个API网关预订客户端,它使用来自服务预订服务的数据,该服务提供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回复,但链接全部来自预订服务 - 。
"_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"
}
}
}
答案 0 :(得分:0)
我明白了。
解决方案是在X-Forwarded-Host http标头中。 X-Forwarded-Host实质上告诉Spring,任何具有此标头的HATEOS响应,链接中的主机和端口信息都应该更新为X-Forwarded-Host http标头中提到的内容。
因此,在API网关预订 - 客户端代码中,我添加了一个片段,用于截取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服务。