SpringCloud Hystrix fault tolerance and rollback

Add fall back to zuul

Want to add a fallback Zuul, the need to achieve ZuulFallbackProvider interface in the implementation class, which is designated as a fallback offer micro-services, and provide a ClientHttpResponse as a fallback response.
1. Copy the project microservice-gateway-zuul, will ArtifactId modified to microservice-gateway-zuul-fallback.
2. Write zuul rollback categories:

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
 */
@Component
public 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;
            }
        };
    }
}

Guess you like

Origin www.cnblogs.com/wuhen8866/p/11116195.html