为zuul添加回退
想要为Zuul添加回退,需要实现ZuulFallbackProvider接口,在实现类中,指定为哪个微服务提供回退,并提供一个ClientHttpResponse作为回退响应。
1.复制项目microservice-gateway-zuul,将ArtifactId修改为microservice-gateway-zuul-fallback。 2.编写zuul的回退类:package com.xhx.springcloud.hystrix; import com.alibaba.fastjson.JSONObject;import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.client.ClientHttpResponse;import org.springframework.stereotype.Component; import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream; /** * xuhaixing * 2018/6/12 21:36 */@Componentpublic class ZuulFallBack implements FallbackProvider { @Override public String getRoute() { return "eureka-feign"; //服务id,可以用* 或者 null 代表所有服务都过滤 } @Override public ClientHttpResponse fallbackResponse(String route, Throwable cause) { return new ClientHttpResponse() { @Override public HttpStatus getStatusCode() throws IOException { return HttpStatus.OK; //请求网关成功了,所以是ok } @Override public int getRawStatusCode() throws IOException { return HttpStatus.OK.value(); } @Override public String getStatusText() throws IOException { return HttpStatus.OK.getReasonPhrase(); } @Override public void close() { } @Override public InputStream getBody() throws IOException { JSONObject json =new JSONObject(); json.put("state","501"); json.put("msg","后台接口错误"); return new ByteArrayInputStream(json.toJSONString().getBytes("UTF-8")); //返回前端的内容 } @Override public HttpHeaders getHeaders() { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); //设置头 return httpHeaders; } }; }}