前言

  Zuul 是在Spring Cloud Netflix平台上提供动态路由,监控,弹性,安全等边缘服务的框架,
是Netflix基于jvm的路由器和服务器端负载均衡器,相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门。本文基于上篇(
SpringCloud系列——Ribbon 负载均衡 <https://www.cnblogs.com/huanzi-qch/p/10136254.html>
)实现Zuul动态路由

  GitHub地址:https://github.com/Netflix/zuul <https://github.com/Netflix/zuul>

  官方文档:
https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.1.0.RC2/single/spring-cloud-netflix.html#_router_and_filter_zuul

<https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.1.0.RC2/single/spring-cloud-netflix.html#_router_and_filter_zuul>

 

  代码编写


  首先我们在springCloud下面新建一个springboot项目:zuul-server,pom继承parent,并且在Eureka上面注册(还不会服务注册与发现的,请戳:
SpringCloud系列——Eureka 服务注册与发现
<https://www.cnblogs.com/huanzi-qch/p/10131985.html>)



   maven引入Zuul
<!-- Zuul --> <dependency> <groupId>org.springframework.cloud</groupId> <
artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
  配置文件
server.port=10010 spring.application.name=zuul-server eureka
.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ #
健康检查(需要spring-boot-starter-actuator依赖) eureka.client.healthcheck.enabled=true #
续约更新时间间隔(默认30秒) eureka.instance.lease-renewal-interval-in-seconds=10 #
续约到期时间(默认90秒) eureka.instance.lease-expiration-duration-in-seconds=10 #zuul代理配置
zuul.routes.服务名.path,服务名要与注册的一致 #应用名映射
zuul.routes.myspringboot.path=/myspringboot/** zuul
.routes.myspringboot.service-id=myspringboot #URL映射
#zuul.routes.myspringboot.path=/myspringboot/**
#zuul.routes.myspringboot-url.url=http://localhost:10087/
  自定义Zuul过滤器

  更多的检查规则后续慢慢健全
/** * Zuul过滤器,实现了路由检查 */ public class AccessFilter extends ZuulFilter { /** *
通过int值来定义过滤器的执行顺序*/ @Override public int filterOrder() { // PreDecoration之前运行
return PRE_DECORATION_FILTER_ORDER - 1; } /** * 过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型:
* public static final String ERROR_TYPE = "error"; * public static final String
POST_TYPE = "post"; * public static final String PRE_TYPE = "pre"; * public
static final String ROUTE_TYPE = "route";*/ @Override public String
filterType() {return PRE_TYPE; } /** * 过滤器的具体逻辑 */ @Override public Object
run() { RequestContext ctx= RequestContext.getCurrentContext();
HttpServletRequest request= ctx.getRequest(); System.out.println(String.format(
"%s AccessFilter request to %s",
request.getMethod(),request.getRequestURL().toString())); String accessToken=
request.getParameter("accessToken"); //有权限令牌 if (!
StringUtils.isEmpty(accessToken)) { ctx.setSendZuulResponse(true);
ctx.setResponseStatusCode(200); //可以设置一些值 ctx.set("isSuccess", true); return
null; } else { ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401);
ctx.setResponseBody("{\"result\":\"accessToken is not correct!\"}"); //可以设置一些值
ctx.set("isSuccess",false); return null; } } /** * 返回一个boolean类型来判断该过滤器是否要执行 */
@Overridepublic boolean shouldFilter() { return true; } }
  启动类

  添加@EnableZuulProxy注解并使用自定义过滤器
@EnableZuulProxy @SpringBootApplication public class ZuulServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class, args); } @Bean public
AccessFilter accessFilter() {return new AccessFilter(); } }
 

   效果演示

  启动所有项目,我们在Eureka上注册了四个服务,相比上篇(SpringCloud系列——Ribbon 负载均衡
<https://www.cnblogs.com/huanzi-qch/p/10136254.html>)多了一个Zuul



 

  浏览器访问
http://localhost:10010/myspringboot/feign/ribbon、http://localhost:10010/myspringboot/feign/ribbon?accessToken=123456

  http://localhost:10010/ 这个端口对外暴露,相对于总入口,后面接不同的路径由,Zuul路由到对应的服务上

  1、没有accessToken是,无法通过检查

  2、携带accessToken时,可正常路由,并且Feign调用、Ribbon负载均衡



 

  后记

  我们为什么要使用Zuul呢?

  1、请求校验、路由转发,接口校验与业务逻辑分离

  2、隐藏诸多服务路径,只暴露统一入口,安全

  更多Zuul配置,请看官方文档

友情链接
ioDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信