美文网首页
SpringCloud GateWay Zuul网关使用

SpringCloud GateWay Zuul网关使用

作者: 火星移民局局长 | 来源:发表于2020-09-02 11:17 被阅读0次

step1:

添加pom依赖

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-boot-starter-netflix-zuul</artifactId>

</dependency>

step2:

配置yml

zuul:

  host:

    connect-timeout-millis: 20000 //连接超时时间

    socket-timeout-millis: 60000

  routes:

    order:

      path: /api/order/**

      service-id: order-server        //对应拦截的微服务id

    product:

      path: /api/procut/**

      service-id: product-server

    verification:

      path: /api/verification/**

      service-id: verification-server

step3:

编写一个类继承自ZuulFilter:

shouldFilter();判断当前拦截的请求是否拦截;

run();对被拦截的请求进行逻辑处理

filterOrder();拦截优先级,数字越小优先级越高

zuul需要注意的问题:

1,run();中只需要return null

2,在run();中需要RequestContext.getCurrentContext()得到当前请求对象,需要拦截或者放行需要对其对象setSendZuulRersponce(true/false);

3,需要拦截当前请求并返回json的时候需要对RequestContext.getCurrentContext()得到的对象进行setResponceStatusCode(200);如果不为200,请求将无json返回;需要定义的json可以setResponceBody(json);

相关文章

网友评论

      本文标题:SpringCloud GateWay Zuul网关使用

      本文链接:https://www.haomeiwen.com/subject/sjwisktx.html