Spring Cloud (DALSTON.SR5)--zuul Gateway-hystrix fallback

Source: Internet
Author: User

When we configure the gateway to invoke the service of the cluster, the Ribbon route filter is executed, which is encapsulated as a hystrix command to execute when forwarding, and the Hystrix command has fault tolerance if there is a problem with the "source service" (such as a timeout), the The Hystrix command will trigger a fallback, and we need to implement the Org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider interface, which mainly needs to implement Getroute method, Fallbackresponse method, the Getroute method mainly returns the name of the route used to match the route of the Hystrix fallback method; The Fallbackresponse method mainly returns the return information of the fallback, the sample code is as follows:

  • Fallback Method Class

    The Getroute method is implemented, and the return Helloworld-provider represents the fallback class for the Helloworld-provider service, implements the Fallbackresponse method, and indicates the specific content returned by the fallback method

    Package Org.lixue.zuul;

    ??

    Import Org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;

    Import org.springframework.http.HttpHeaders;

    Import Org.springframework.http.HttpStatus;

    Import Org.springframework.http.MediaType;

    Import Org.springframework.http.client.ClientHttpResponse;

    ??

    Import Java.io.ByteArrayInputStream;

    Import java.io.IOException;

    Import Java.io.InputStream;

    ??

    Public class Helloworldfallback implements zuulfallbackprovider{

    @Override

    Public String Getroute () {

    return"Helloworld-provider" ;

    }

    ??

    @Override

    Public clienthttpresponse Fallbackresponse () {

    return new Clienthttpresponse () {

    @Override

    Public httpstatus Getstatuscode ()throwsioexception{

    return Httpstatus. OK;

    }

    ??

    @Override

    public int Getrawstatuscode ()throwsioexception{

    return Httpstatus. OK. Value ();

    }

    ??

    @Override

    Public String getstatustext ()throwsioexception{

    return H Ttpstatus. OK. toString ();

    }

    ??

    @Override

    Public void Close () {

    ??

    }

    ??

    @Override

    Public InputStream getbody () throws ioexception{

    return new Bytearrayinputstream ("fallback". GetBytes ());

    }

    ??

    @Override

    Public httpheaders getheaders () {

    Httpheaders headers=new httpheaders ();

    Headers.setcontenttype (mediatype. Text_plain );

    return headers;

    }

    };

    }

    }

    ??

  • Zuul Configuration Class

    Create a configuration class and create an instance Bean of the fallback class in the configuration class

    Package Org.lixue.zuul;

    ??

    Import org.springframework.context.annotation. Bean;

    Import org.springframework.context.annotation. Configuration;

    ??

    @Configuration

    Public class zuulfallbackconfiguration{

    ??

    @Bean

    Public helloworldfallback Helloworldfallback () {

    return new Helloworldfallback ();

    }

    }

    ??

  • Increase Routing configuration

    #配置应用名称

    Spring

    Application:

    Name: spring-cloud-zuul-microservices

    #服务端口

    Server

    Port: 9200

    #设置eureka服务注册中心的地址 if multiple comma-separated

    Eureka

    Client

    Service-url:

    #defaultZone表示默认的区域的eureka服务地址, multiple partitions using commas

    Defaultzone: http://eurekaserver01:9000/eureka/

    ??

    #zuul路由配置

    Zuul:

    Routes

    Hello

    Path: /hello/**

    serviceId: Helloworld-provider

    ??

  • Test validation

    the project relies on a eureka-sserver, Service-provider service that starts Eureka-server and Service-provider services first, and then starts Spring-cloud-zuul-microservices Service, Access to http://localhost:9200/hello/speaks?names=123 address, you can see the normal return, as follows:

    {"123": "Hello World 123 port=8080"}

    At this point, the Service-provider service is turned off again, and you can see that the Service-provider service is inaccessible and the data for the fallback method is returned, as follows:

    Fallback

Spring Cloud (DALSTON.SR5)--zuul Gateway-hystrix fallback

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.