博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
企业级 SpringCloud 教程 (五)路由网关(zuul)
阅读量:6956 次
发布时间:2019-06-27

本文共 4312 字,大约阅读时间需要 14 分钟。

hot3.png

一、Zuul简介

Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。

zuul有以下功能:

  • Authentication
  • Insights
  • Stress Testing
  • Canary Testing
  • Dynamic Routing
  • Service Migration
  • Load Shedding
  • Security
  • Static Response handling
  • Active/Active traffic management

二、准备工作

继续使用上一节的工程。在原有的工程上,创建一个新的工程。

三、创建service-zuul工程

其pom.xml文件如下:

4.0.0
com.forezp
service-zuul
0.0.1-SNAPSHOT
jar
service-zuul
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-zuul
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false

在其入口applicaton类加上注解@EnableZuulProxy,开启zuul的功能:

@EnableZuulProxy@EnableEurekaClient@SpringBootApplicationpublic class ServiceZuulApplication {    public static void main(String[] args) {        SpringApplication.run(ServiceZuulApplication.class, args);    }}

加上配置文件application.yml加上以下的配置代码:

eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/server:  port: 8769spring:  application:    name: service-zuulzuul:  routes:    api-a:      path: /api-a/**      serviceId: service-ribbon    api-b:      path: /api-b/**      serviceId: service-feign

 

四、服务过滤

zuul不仅只是路由,并且还能过滤,做一些安全验证。继续改造工程;

@Componentpublic class MyFilter extends ZuulFilter{    private static Logger log = LoggerFactory.getLogger(MyFilter.class);    @Override    public String filterType() {        return "pre";    }    @Override    public int filterOrder() {        return 0;    }    @Override    public boolean shouldFilter() {        return true;    }    @Override    public Object run() {        RequestContext ctx = RequestContext.getCurrentContext();        HttpServletRequest request = ctx.getRequest();        log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));        Object accessToken = request.getParameter("token");        if(accessToken == null) {            log.warn("token is empty");            ctx.setSendZuulResponse(false);            ctx.setResponseStatusCode(401);            try {                ctx.getResponse().getWriter().write("token is empty");            }catch (Exception e){}            return null;        }        log.info("ok");        return null;    }}

filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下: 

  • pre:路由之前
  • routing:路由之时
  • post: 路由之后
  • error:发送错误调用
  • filterOrder:过滤的顺序
  • shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
  • run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。

转载于:https://my.oschina.net/u/3773422/blog/1614850

你可能感兴趣的文章
javascript预解释中的机制
查看>>
正则表达式pattern的匹配格式
查看>>
JDOM
查看>>
MySQL 最基本的SQL语法/语句
查看>>
洛谷 P2661 信息传递 Label:并查集||强联通分量
查看>>
Linux下搭建ftp服务器(转载)
查看>>
hadoop之 HDFS-Hadoop存档
查看>>
搭建时间服务器
查看>>
php 多进程 父进程的阻塞与非阻塞
查看>>
asp.net core mvc ActionFilterAttribute 获取自动定义Attribute
查看>>
sealed、new、virtual、abstract与override 趣解
查看>>
[bzoj 4650][NOI 2016]优秀的拆分
查看>>
crossplatform---Nodejs in Visual Studio Code 08.IIS
查看>>
OGNL表达式入门
查看>>
Java 诊断工具 Arthas 教程学习笔记
查看>>
bootstrap 2.3版与3.0版的使用区别
查看>>
高并发之 - 全局有序唯一id Snowflake 应用实战
查看>>
菜鸟学习笔记1
查看>>
office2003 安装步骤及注意事项
查看>>
手机抓包图文教程 【转载】
查看>>