• springcloud15---zuul-fallback


    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, args);
      }
    }
    package com.itmuch.cloud.fallback;
    
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    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 org.springframework.stereotype.Component;
    
    //通过zuul(8040端口)访问user微服务http://localhost:8040/microservice-provider-user/simple/1
    //如果此时user微服务停掉,就会返回"fallbackmicroservice-provider-user",(使用的是hysitrcs的断路器功能)
    //feign的Fallback是针对一个类,zuul的Fallback是针对一个微服务。
    @Component
    public class MyFallbackProvider implements ZuulFallbackProvider {
      @Override
      public String getRoute() {
        return "microservice-provider-user";
      }
    
      @Override
      public ClientHttpResponse fallbackResponse() {
        return new ClientHttpResponse() {
          @Override
          public HttpStatus getStatusCode() throws IOException {
            return HttpStatus.BAD_REQUEST;
          }
    
          @Override
          public int getRawStatusCode() throws IOException {
            return HttpStatus.BAD_REQUEST.value();
          }
    
          @Override
          public String getStatusText() throws IOException {
            return HttpStatus.BAD_REQUEST.getReasonPhrase();
          }
    
          @Override
          public void close() {
          }
    
          @Override
          public InputStream getBody() throws IOException {
            return new ByteArrayInputStream(("fallback" + MyFallbackProvider.this.getRoute()).getBytes());
          }
    
          @Override
          public HttpHeaders getHeaders() {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            return headers;
          }
        };
      }
    }
    spring:
      application:
        name: microservice-gateway-zuul
    server:
      port: 8040
    eureka:
      client:
        service-url:
          defaultZone: http://user:password123@localhost:8761/eureka
      instance:
        prefer-ip-address: true
    hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
    ribbon:
      ConnectTimeout: 3000
      ReadTimeout: 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>microservice-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>
    
        <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>
  • 相关阅读:
    洛谷 3393 逃离僵尸岛
    洛谷 3275 [SCOI2011]糖果
    SP1437 Longest path in a tree(树的直径)
    洛谷2483 k短路([SDOI2010]魔法猪学院)
    洛谷3243 [HNOI2015]菜肴制作
    洛谷 4568 [JLOI2011] 飞行路线
    [USACO08DEC]在农场万圣节Trick or Treat on the Farm
    手机端table表格bug
    手机端左右滑动效果
    去掉手机端延迟300ms
  • 原文地址:https://www.cnblogs.com/yaowen/p/9159472.html
Copyright © 2020-2023  润新知