我使用spring cloud来构建多个微服务,我使用一个使用Spring Cloud Netfix的Zuul Server实现的API网关将请求路由到我们的微服务,网关配置如下:
application.yml:
server:
port: 8021
ribbon:
ConnectTimeout: 3000
ReadTimeout: 60000
zuul:
ignoredServices: "*"
add-proxy-headers: true
#prefix: /v1
routes:
m_test:
path: /api/testService/**
sensitiveHeaders: "*"
url: http://127.0.0.1:4008/testService/
eureka:
instance:
hostname: gateway
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka/
CorsFilter.java:
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization,Content-length");
response.setHeader("Access-Control-Max-Age", "1800");
Map<String, String> map = getHeadersInfo(request);
if (request.getMethod().equalsIgnoreCase("OPTIONS")) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(request, response);
}
}
public void init(FilterConfig filterConfig) {
}
public void destroy() {
}
private Map<String, String> getHeadersInfo(HttpServletRequest request) {
Map<String, String> map = new HashMap<>();
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
String value = request.getHeader(key);
map.put(key, value);
}
return map;
}
}
GatewaySystemApplication.java:
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class GatewaySystemApplication {
public static void main(String[] args) throws Exception {
//SpringApplication.run(GatewaySystemApplication.class, args);
new SpringApplicationBuilder(GatewaySystemApplication.class).web(true).run(args);
}
}
浏览器响应标头如下:
passing through gateway layer response headers
我的请求的响应标头错过了内容长度, 但是当我直接调用后端服务时它有内容长度。directly response headers
答案 0 :(得分:-3)
嗯,你没有设置Content-Length。
(FWIW,您的内容类型也是假的)。