-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientFallback.java
More file actions
54 lines (44 loc) · 1.5 KB
/
ClientFallback.java
File metadata and controls
54 lines (44 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.github.zuul.handler;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
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;
/**
* @author <a href="http://github.com/athc">dujf</a>
* @date 2018/9/19
* @since JDK1.8
*/
@Component
public class ClientFallback implements FallbackProvider {
@Override public String getRoute() {
return "service-eureka-client";
}
@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 200;
}
@Override public String getStatusText() throws IOException {
return "SUCCESS";
}
@Override public void close() {
}
@Override public InputStream getBody() throws IOException {
return new ByteArrayInputStream("The service is unavailable.".getBytes());
}
@Override public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
}