万隆的笔记 万隆的笔记
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
博文索引
笔试面试
  • 在线学站

    • 菜鸟教程 (opens new window)
    • 入门教程 (opens new window)
    • Coursera (opens new window)
  • 在线文档

    • w3school (opens new window)
    • Bootstrap (opens new window)
    • Vue (opens new window)
    • 阿里开发者藏经阁 (opens new window)
  • 在线工具

    • tool 工具集 (opens new window)
    • bejson 工具集 (opens new window)
    • 文档转换 (opens new window)
  • 更多在线资源
  • Changlog
  • Aboutme
GitHub (opens new window)
  • 快速开始

  • 服务注册与发现

  • 服务熔断

  • 服务网关

    • 路由网关-Netflix Zuul
    • 路由网关失败回调
      • 概述
      • Zulu路由失败回调
      • 测试路由失败回调
    • 路由网关服务过滤
  • 配置中心

  • 服务追踪

  • SpringCloud-Netflix
  • 服务网关
2021-08-29
目录

路由网关失败回调

# 路由网关失败回调

# 概述

我们先前讨论了为了避免服务雪崩效应,引入了熔断器的解决方案Netflix Hystrix,现在用了路由网关统一管理了对服务的接口访问,假如,网关向相关API服务请求失败了该如何处理?同样的,路由的服务无法请求时也需要触发熔断功能。

所以在搭建路由网关服务的时候,一般还要配置失败的回调,注意的是对于用户来说,客户端向网关发起的请求是成功的,不应该将API服务的404,500等问题抛给客户端,因为网关和API服务集群对于客户端来说是黑盒。下面以Zuul网关的失败回调为例。

# Zulu路由失败回调

这里以路由hello-spring-cloud-netflix-consumer-feign失败为例,配置回调:

package com.example.hello.spring.cloud.netflix.zuul.fallback;

import com.fasterxml.jackson.databind.ObjectMapper;
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;
import java.util.HashMap;
import java.util.Map;

@Component
public class FeignConsumerFallbackProvider implements FallbackProvider {
    @Override
    public String getRoute() {
        // 路由失败的服务名称。如果需要所有调用都支持回退,则 return "*" 或 return null
        return "hello-spring-cloud-netflix-consumer-feign";
    }

    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        return new ClientHttpResponse() {
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.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 {
                ObjectMapper objectMapper = new ObjectMapper();
                Map<String, Object> map = new HashMap<>();
                map.put("status", 200);
                map.put("message", "哦豁,网络出了个小差~");
                return new ByteArrayInputStream(objectMapper.writeValueAsString(map).getBytes("UTF-8"));
            }

            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                return headers;
            }
        };
    }
}

# 测试路由失败回调

关闭hello-spring-cloud-netflix-consumer-feign服务,浏览器访问:http://localhost:8181/api/consumer/feign/hi?msg=HelloZuul,输出如下:

{
  "message": "哦豁,网络出了个小差~",
  "status": 200
}
上次更新: 5/30/2023, 12:51:40 AM
路由网关服务过滤

路由网关服务过滤→

最近更新
01
Vue-Ajax
06-13
02
2025
01-15
03
Elasticsearch面试题
07-17
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式