SPRINGCLOUD15---Zuul-fallback

Source: Internet
Author: User

 package   Com.itmuch.cloud;  import   org.springframework.boot.SpringApplication;  import   org.springframework.boot.autoconfigure.SpringBootApplication;  import   org.springframework.cloud.netflix.zuul.enablezuulproxy;@ Springbootapplication@enablezuulproxy  public  class   zuulapplication { public  static  void   main (string[] args) {Springapplication.run (zuulapplication. ) class   
 PackageCom.itmuch.cloud.fallback;ImportJava.io.ByteArrayInputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportOrg.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;Importorg.springframework.http.HttpHeaders;ImportOrg.springframework.http.HttpStatus;ImportOrg.springframework.http.MediaType;ImportOrg.springframework.http.client.ClientHttpResponse;Importorg.springframework.stereotype.Component;//Access User micro-service HTTP via Zuul (8040 port)://LOCALHOST:8040/MICROSERVICE-PROVIDER-USER/SIMPLE/1//If the user Micro service is stopped at this time, it will return "Fallbackmicroservice-provider-user" (using the Hysitrcs circuit breaker function)//Feign's fallback is for a class, Zuul's fallback is for a microservices. @Component Public classMyfallbackproviderImplementsZuulfallbackprovider {@Override PublicString Getroute () {return"Microservice-provider-user"; } @Override Publicclienthttpresponse Fallbackresponse () {return NewClienthttpresponse () {@Override PublicHttpstatus Getstatuscode ()throwsIOException {returnhttpstatus.bad_request; } @Override Public intGetrawstatuscode ()throwsIOException {returnHttpStatus.BAD_REQUEST.value (); } @Override PublicString Getstatustext ()throwsIOException {returnHttpStatus.BAD_REQUEST.getReasonPhrase (); } @Override Public voidclose () {} @Override PublicInputStream GetBody ()throwsIOException {return NewBytearrayinputstream ("fallback" + Myfallbackprovider. This. Getroute ()). GetBytes ()); } @Override Publichttpheaders getheaders () {httpheaders headers=Newhttpheaders ();        Headers.setcontenttype (Mediatype.application_json); returnheaders;  }    }; }}
Spring:  application:    name:microservice-gateway-zuulserver:  8040Eureka:  Client:    service-URL:      defaultzone:http://user:[email protected]:8761/ Eureka  instance:    prefertrueHystrix.command. default. execution.isolation.thread.timeoutinmilliseconds:60000Ribbon:      60000
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <parent> <groupId>com.itmuch.cloud</groupId> <artifactid>mi Croservice-spring-cloud</artifactid> <version>0.0.1-SNAPSHOT</version> </parent> < Artifactid>microservice-gateway-zuul-fallback</artifactid> <packaging>jar</packaging> < Properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties&gt    ;            <dependencies> <dependency> <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-eureka </artifactId> </dEpendency> </dependencies></project> 

SPRINGCLOUD15---Zuul-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.