本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3
本文基于前两篇文章eureka-server、eureka-client、eureka-ribbon和eureka-feign的实现。 参考
Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,例如将请求/api/goods转发到商品服务上、/api/order转发到订单服务上等。
Zull默认和Ribbon结合实现了负载均衡功能。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> 复制代码
spring: application: name: eureka-zuul server: port: 8400 eureka: instance: hostname: localhost lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 10 client: service-url: defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/ zuul: routes: eureka-ribbon: #对应服务名称,可以自定义(最好保持一致) path: /ribbon/* serviceId: eureka-ribbon #对应服务名称 eureka-feign: path: /eureka-feign/* serviceId: eureka-feign #对应服务名称 复制代码
package spring.cloud.demo.eurekazuul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableZuulProxy @SpringBootApplication public class EurekaZuulApplication { public static void main(String[] args) { SpringApplication.run(EurekaZuulApplication.class, args); } } 复制代码
@EnableZuulProxy:开启Zuul网关注解
打开浏览器,分别输入http://localhost:8400/ribbon/sayHello, http://localhost:8400/eureka-feign/feign/sayHello,显示结果分别如下:
可以看到Zuul按照路由转发的配置规则,把/ribbon/*的请求转发到eureka-ribbon服务上,把/eureka-feign/*的请求转发到eureka-feign的服务上。
至此,一个简单基于Zuul路由网关就搭建完成了。
##添加自定义过滤器
Zuul过滤器有四种类型分别是
package spring.cloud.demo.eurekazuul.fillter; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; import org.apache.commons.lang.StringUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 简单filter过滤器 * @auther: maomao * @DateT: 2019-09-17 */ public class CommonFilter extends ZuulFilter { /** * 在请求被路由之前调用 * @return */ @Override public String filterType() { return "pre"; } /** * filter执行顺序,通过数字指定 ,优先级为0,数字越大,优先级越低 * @return */ @Override public int filterOrder() { return 0; } /** * 是否执行该过滤器,此处为true,说明需要过滤 * @return */ @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { RequestContext requestContext = RequestContext.getCurrentContext(); HttpServletRequest request = requestContext.getRequest(); //获取请求参数 String token = request.getParameter("token"); //如果有token参数并且token值为miniooc,才进行路由 if (StringUtils.isNotBlank(token) && token.equals("maomao")) { requestContext.setSendZuulResponse(true); requestContext.setResponseStatusCode(200); requestContext.set("code", 1); } else { requestContext.setSendZuulResponse(false); requestContext.setResponseStatusCode(401); HttpServletResponse response = requestContext.getResponse(); response.setHeader("content-type", "text/html;charset=utf-8"); requestContext.setResponseBody("网关认证失败,停止路由"); requestContext.set("code", 0); } return null; } } 复制代码
package spring.cloud.demo.eurekazuul.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import spring.cloud.demo.eurekazuul.fillter.CommonFilter; /** * @auther: maomao * @DateT: 2019-09-17 */ @Configuration public class ZuulFilterConfig { @Bean public CommonFilter commonFilter() { return new CommonFilter(); } } 复制代码
在浏览器分别输入http://localhost:8400/ribbon/sayHello, http://localhost:8400/eureka-feign/feign/sayHello,显示结果如下:
显示结果证明,过滤器已经生效。因为过滤CommonFilter中要求输入参入带token=maomao,所以请求被拦截,Zuul停止转发。
在浏览器中URL中增加token参数,显示如下:
显示结果证明,在token正确的情况下,Zuul转发成功。
至此,自定义的过滤器就演示完成。