Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpringCloudZuul回退服务_Spring_Spring Cloud_Netflix Zuul - Fatal编程技术网

SpringCloudZuul回退服务

SpringCloudZuul回退服务,spring,spring-cloud,netflix-zuul,Spring,Spring Cloud,Netflix Zuul,是否可以设置到主服务器/服务器/服务的代理,但如果不可用,请将请求重定向到辅助服务器/服务器/服务 辅助服务不是与主服务类型相同的实例。它们的API是兼容的,但并不相同。当主资源不可用时,辅助资源是性能较差的最后一个资源 我们不使用Eureka,只使用固定IP zuul: routes: whatever: path: /whatever/** sensitiveHeaders: Cookie,Set-Cookie url: http://serv

是否可以设置到主服务器/服务器/服务的代理,但如果不可用,请将请求重定向到辅助服务器/服务器/服务

辅助服务不是与主服务类型相同的实例。它们的API是兼容的,但并不相同。当主资源不可用时,辅助资源是性能较差的最后一个资源

我们不使用Eureka,只使用固定IP

zuul:
  routes:
    whatever:
      path: /whatever/**
      sensitiveHeaders: Cookie,Set-Cookie
      url: http://server1:8080/whatever
我查看了
ZuulFallbackProvider
,但是这个接口在出现错误时提供了一个固定的响应。我想,当没有响应时,重定向到


谢谢。

您可以使用
ZuulFallbackProvider
实现这一点,但您需要首先配置以下内容

首先,url路由-直接在
zuul.routes..url
-中指定url-在zuul中不在HystrixCommand中执行。要实现这一点,您需要如下更改配置

zuul:
  routes:
    whatever:
      path: /whatever/**
      sensitiveHeaders: Cookie,Set-Cookie
      serviceId: whatever
      stripPrefix: false

ribbon:
  eureka:
    enabled: false

whatever:
  ribbon:
    listOfServers: http://server1:8080/
上述配置使用的是不带eureka的Ribbon。你可以找到细节

现在,您的请求将通过Ribbon在HystrixCommand中执行。因此,您可以提供自己的ZuulFallbackProvider

在ZuulFallbackProvider中,您可以向
http://server2:8080/whateverApi2.
回退响应
方法中,如下所示。下面是一个非常幼稚的例子。:-)出于您自己的目的,您需要完成以下示例

@Component
public class TestZuulFallbackProvider implements ZuulFallbackProvider{
    @Override
    public String getRoute() {
        return "test";
    }

    @Override
    public ClientHttpResponse fallbackResponse() {

        ResponseEntity<String> response = new RestTemplate().exchange("http://server2:8080/whateverApi2", HttpMethod.GET, null, String.class);
        return new ClientHttpResponse() {
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return response.getStatusCode();
            }
            @Override
            public int getRawStatusCode() throws IOException {
                return response.getStatusCodeValue();
            }
            @Override
            public String getStatusText() throws IOException {
                return response.getStatusCode().getReasonPhrase();
            }
            @Override
            public void close() {
            }
            @Override
            public InputStream getBody() throws IOException {
                return new ByteArrayInputStream(response.getBody().getBytes("UTF-8"));
            }
            @Override
            public HttpHeaders getHeaders() {
                return response.getHeaders();
            }
        };
    }
}
@组件
公共类TestZuulFallbackProvider实现了ZuulFallbackProvider{
@凌驾
公共字符串getRoute(){
返回“测试”;
}
@凌驾
public ClientHttpResponse fallbackResponse(){
ResponseEntity response=新建RestTemplate()。exchange(“http://server2:8080/whateverApi2,HttpMethod.GET,null,String.class);
返回新的ClientHttpResponse(){
@凌驾
公共HttpStatus getStatusCode()引发IOException{
返回响应。getStatusCode();
}
@凌驾
public int getRawStatusCode()引发IOException{
返回响应。getStatusCodeValue();
}
@凌驾
公共字符串getStatusText()引发IOException{
返回响应。getStatusCode().GetReasonPhase();
}
@凌驾
公众假期结束(){
}
@凌驾
公共InputStream getBody()引发IOException{
返回新的ByteArrayInputStream(response.getBody().getBytes(“UTF-8”);
}
@凌驾
公共HttpHeaders getHeaders(){
返回response.getHeaders();
}
};
}
}

如果有人试图做类似的事情,那么真正起作用的是Hystrix而不是Zuul组件

在网关API中,我们创建一个门面控制器,该控制器响应我们要设置回退解决方案的服务:

@HystrixCommand(fallbackMethod = "fallbackWhatever")
@PostMapping
ResponseEntity<Object> whatever(final RequestEntity<?> request) {
    return defaultClient.searchSubmissions(request.getHeaders(), request.getBody());
}

ResponseEntity<Object> fallbackWhatever(final RequestEntity<?> request) {
    return fallbackClient.searchSubmissions(request.getHeaders(), request.getBody());
}
@HystrixCommand(fallbackMethod=“fallbackwhere”)
@邮戳
ResponseEntity(最终请求实体请求){
返回defaultClient.searchSubmissions(request.getHeaders(),request.getBody());
}
ResponseEntity回退(最终请求实体请求){
返回fallbackClient.searchSubmissions(request.getHeaders(),request.getBody());
}
defaultClient和fallbackClient是两个不同的假客户端接口,每个接口指向每个服务端点

就这样!如果关闭主服务,网关API将开始调用回退服务,而不会返回任何不可用的服务,更改只需几毫秒


此外,如果您重新打开它,它在准备就绪几毫秒后也会返回响应。

需要添加可检索

zuul:
  routes:
    whatever:
      path: /whatever/**
      sensitiveHeaders: Cookie,Set-Cookie
      url: http://server1:8080/whatever
      retryable=true

类似的

谢谢你的回答。我还没有将其标记为解决方案,只是想看看是否有“仅配置”的替代方案。我将使用您的解决方案,甚至可能在使用full Spring云基础设施时直接使用一个Netflix OSS组件而不是RestTemplate。但是ZuulFallbackProvider是一个静态回退。是否有任何Zuul、Ribbon或Hystrix将请求重定向到我的回退服务?我不希望将其添加到Ribbon服务器列表中,因为我希望它作为最后手段执行,仅当服务器关闭时才执行。如果您不想使用hystrix fallback,我认为另一种可能的方法是创建自己的完整
路由
post
过滤器。在过滤器中,您可以检查原始请求的结果,然后使用适当的参数将请求转发到备用服务器。