一)网关过滤器异常统一处理
1.注意此异常处理和全局异常异常处理是不同的,Error过滤器只是针对Zuul在过滤请求的过程中如果出现异常,所有的过滤器都会去找有没有过滤类型为error的过滤器,并做异常的统一处理。
2.在此强调,ERROR类型的过滤器是处理用户请求到达网关的时候,在过滤器中出现了异常
二)测试Error类型过滤器
1.创建异常类型的过滤器
package com.sxt.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* @project_name:springcloud2
* @date:2019/9/7:11:41
* @author:shinelon
* @Describe:
*/
@Component
public class ErrorFilter extends ZuulFilter
{
private final Logger logger = LoggerFactory.getLogger(getClass());
@Override
public String filterType()
{
return "error";
}
@Override
public int filterOrder()
{
return 0;
}
@Override
public boolean shouldFilter()
{
return true;
}
@Override
public Object run() throws ZuulException
{
logger.info("------------error---------------");
return null;
}
}
2.人为的在其他的过滤器中抛出异常
package com.sxt.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;
/**
* @project_name:springcloud2
* @date:2019/9/7:10:42
* @author:shinelon
* @Describe:
*/
@Component
public class TestFiler extends ZuulFilter
{
@Override
public String filterType()
{
return "pre";
}
@Override
public int filterOrder()
{
return 1;
}
@Override
public boolean shouldFilter()
{
return true;
}
@Override
public Object run() throws ZuulException
{
RequestContext requestContext = RequestContext.getCurrentContext();
System.out.println(requestContext.getRequest().getRequestURL().toString());
System.out.println("----------pre2..第二层过滤器......");
throw new RuntimeException("测试error类型的过滤器"); //异常的抛出
}
}
3.首先观察执行顺序
1)在第二的过滤器上抛出了异常信息
2)异常信息
3)执行Error后,最后执行post
4.页面显示
如果用户在过滤期间出现了异常,那么肯定不能以这样的方式将信息展现给用户那么怎么解决呢?
在页面内我们看,页面在出现异常的会访问一个的/error的路径,只要我们重写这个的路径即可。
5.解决异常页面的显示的问题
1.创建一个类实现接口ErrorController
package com.sxt.filter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @project_name:springcloud2
* @date:2019/9/7:11:50
* @author:shinelon
* @Describe:
*/
@RestController
public class ErrorController implements org.springframework.boot.web.servlet.error.ErrorController
{
@Override
public String getErrorPath()
{
//将返回的路径设置为/error
return "/error";
}
//编写返回的数据,将路径设置为上面的路径地址
@GetMapping(value = "/error")
public String error(){
return "{'result:error 500'}";
}
}
五)测试对Zuul的监控
1.Zuul无缝Hystrix,在实际的开发的时候,网关无疑是压力最大的一个的服务,所以我们需要随时监听Zuul的生命信息。
2.在导入Zuul的时候其实的已经将Hystrix的监控信息放入Zuul中
3.修改启动类
@SpringBootApplication
@EnableZuulProxy
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registration.addUrlMappings("/hystrix.stream");
return registration;
}
}
访问数据源获取数据信息