springCloud学习04之api服务网关zuul回退fallback
2017-08-11 14:56
726 查看
在前面学习hystrix的时候,我们知道hystrix有fallback回退能力,及如果服务调用出现了异常,则执行指定的fallback方法。那现在zuul对api服务集群进行了反向代理,集成了hystrix,那zuul也能fallback了。
在上一篇博文中,已经建立有zuul项目了,复制一份命名为zuul-fallback,pom.xml,application.properties不需要变,新创建类ServerFallback.java
完整代码在github上
package com.fei.springcloud.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;
import com.alibaba.fastjson.JSONObject;
/**
* 向用户管理api-user-server路由发起请求失败时的回滚处理
* hystrix的回滚能力
* @author Jfei
*
*/
@Component
public class ServerFallback implements ZuulFallbackProvider{
@Override
public String getRoute() {
return "api-user-server";//api服务id,如果需要所有调用都支持回退,则return "*"或return null
}
/**
* 如果请求用户服务失败,返回什么信息给消费者客户端
*/
@Override
public ClientHttpResponse fallbackResponse() {
return new ClientHttpResponse(){
@Override
public InputStream getBody() throws IOException {
JSONObject r = new JSONObject();
r.put("state", "9999");
r.put("msg", "系统错误,请求失败");
return new ByteArrayInputStream(r.toJSONString().getBytes("UTF-8"));
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
//和body中的内容编码一致,否则容易乱码
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
return headers;
}
/**
* 网关向api服务请求是失败了,但是消费者客户端向网关发起的请求是OK的,
* 不应该把api的404,500等问题抛给客户端
* 网关和api服务集群对于客户端来说是黑盒子
*/
@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() {
}
};
}
}
启动eureka服务,启动zuul-fallback,eureka-api,请求访问http://127.0.0.1:8888/user-api/user/find/1,得到正常结果;把eureka-api服务停止掉。稍等一会,刷新页面,得到就是ServerFallback中指定的返回的内容。
如果把ServerFallback.java去掉,会发现页面得到的时候异常信息,zuul的日志或控制台打印refuse connection的异常信息。
既然zuul集成了hystrix,那hystrix的dashboard也是可以集成的了,pom.xml中加入
<!-- 健康检查 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- hystrix dashboard的支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>application.properties中,可以设置dashboard的刷新频率
#hystrix dashboard的信息收集频率,默认500毫秒
hystrix.stream.dashboard.intervalInMilliseconds=5000
启动类中添加 @EnableHystrixDashboard
其他的和之前学习hystrix的监控一样查看了,这里就不重复了。
完整源码
在上一篇博文中,已经建立有zuul项目了,复制一份命名为zuul-fallback,pom.xml,application.properties不需要变,新创建类ServerFallback.java
完整代码在github上
package com.fei.springcloud.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;
import com.alibaba.fastjson.JSONObject;
/**
* 向用户管理api-user-server路由发起请求失败时的回滚处理
* hystrix的回滚能力
* @author Jfei
*
*/
@Component
public class ServerFallback implements ZuulFallbackProvider{
@Override
public String getRoute() {
return "api-user-server";//api服务id,如果需要所有调用都支持回退,则return "*"或return null
}
/**
* 如果请求用户服务失败,返回什么信息给消费者客户端
*/
@Override
public ClientHttpResponse fallbackResponse() {
return new ClientHttpResponse(){
@Override
public InputStream getBody() throws IOException {
JSONObject r = new JSONObject();
r.put("state", "9999");
r.put("msg", "系统错误,请求失败");
return new ByteArrayInputStream(r.toJSONString().getBytes("UTF-8"));
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
//和body中的内容编码一致,否则容易乱码
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
return headers;
}
/**
* 网关向api服务请求是失败了,但是消费者客户端向网关发起的请求是OK的,
* 不应该把api的404,500等问题抛给客户端
* 网关和api服务集群对于客户端来说是黑盒子
*/
@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() {
}
};
}
}
启动eureka服务,启动zuul-fallback,eureka-api,请求访问http://127.0.0.1:8888/user-api/user/find/1,得到正常结果;把eureka-api服务停止掉。稍等一会,刷新页面,得到就是ServerFallback中指定的返回的内容。
如果把ServerFallback.java去掉,会发现页面得到的时候异常信息,zuul的日志或控制台打印refuse connection的异常信息。
既然zuul集成了hystrix,那hystrix的dashboard也是可以集成的了,pom.xml中加入
<!-- 健康检查 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- hystrix dashboard的支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>application.properties中,可以设置dashboard的刷新频率
#hystrix dashboard的信息收集频率,默认500毫秒
hystrix.stream.dashboard.intervalInMilliseconds=5000
启动类中添加 @EnableHystrixDashboard
其他的和之前学习hystrix的监控一样查看了,这里就不重复了。
完整源码
相关文章推荐
- springCloud学习03之api服务网关zuul反向代理及重试配置
- springCloud学习05之api网关服务zuul过滤器filter
- SpringCloud之API网关服务Spring Cloud Zuul实例
- Springcloud学习——Zuul服务网关及路由权限控制
- 07.Spring Cloud学习笔记之API服务网关组件Zuul
- springcloud(十):服务网关zuul初级篇
- SpringCloud(第 018 篇)Zuul 服务 API 网关微服务之代理与反向代理
- SpringCloud(第 022 篇)Zuul 网关微服务的 regexmapper 属性测试, 类似测试 zuul 的自定义路径规则一样
- spring cloud 学习(6) - zuul 微服务网关
- 独立使用zuul网关分发不同服务的请求、权限控制,非SpringCloud
- SpringCloud实战之Zuul网关服务
- Spring Cloud Zuul - API网关服务
- springcloud(十):服务网关zuul初级篇
- API网关服务:Spring Cloud Zuul
- springCloud(15):使用Zuul构建微服务网关-Header与文件上传和过滤器
- SpringCloud调研系列5.2:服务网关Zuul组合API之Filter研究
- SpringCloudAPI网关服务Zuul
- Spring Cloud学习:04路由网关(Zuul)
- spring cloud Zuul(API网关服务)
- springcloud(十):服务网关zuul初级篇