SpringCloud---GateWay---Zuul(API网关)

介绍

Zuul是Netflix出品的一个基于JVM路由和服务端的负载均衡器。Spring Cloud Zuul路由是微服务架构的不可或缺的一部分,提供动态路由,监控,弹性,安全等的边缘服务,从而达到负载均衡、反向代理、权限认证的一个API gateway

目的

在基于微服务的项目中,为了简化前端的调用逻辑,通常会引入API Gateway作为轻量级网关,同时会将权限检验放到网关中做统一处理,从而简化服务内部的逻辑,

SpringCloud---GateWay---Zuul(API网关)_第1张图片
gateway.png

实现

  1. 启动类 添加@EnableZuulProxy注解

    @SpringBootApplication
    @EnableZuulProxy
    public class GetwayZuulApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(GetwayZuulApplication.class, args);
        }
    }
    
  2. yml中

      spring:
        application:
          name: getway-zuul
    eureka:
      client:
        serviceUrl:
          defaultZone: http://ralap:hjx969190@localhost:8761/eureka/
      instance:
        prefer-ip-address: true
    server:
    port: 8050
    

    启动zuul访问 locahost:8085/user-provider/getUser/1直接就可以访问到user服务的数据,user-provider是代理服务(user)的applicaitonName

  3. 修改访问路径

    1. 修改代理服务的applicaitonName

      zuul:
        ignoredServices: '*' #取消反向代理
        routes:
           aaa:
              path: /user/**
             serviceId: user-provider
          #### url: http://localhost:8081/ 不能负载均衡
      

      这时访问locahost:8085/user/getUser/1

    2. 负载均衡

      ribbon:
        eureka:
          enabled: false
      user-provider:
         ribbon:
            listOfServers: localhost:8081,localhost:8082
      

      注意:如果使用的是url,则负载均衡无效

    3. 添加prefix

      zuul:
        prefix: /api
        strip-prefix: false
      
    ```
     >只添加prefix这时访问locahost:8085/api/user/getUser/1
     >如果strip-prefix: false这时访问http://localhost:8050/api/user/getUser/1实际访问的是user微服务的http://localhost:8082/api/getUser/1,所以当user中
     server:
         port: 8082
         context-path: /api
    

才能正确访问

上传

  1. 配置zuul
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000

原本上传路径是localhost:8050/file-uplaod/upload,但是在传大文件的时候会timeout,这是添加/zuul节点就可以成功解决问题localhost:8050/zuul/file-uplaod/upload,

Zuul Fileter

  1. 自定义Fileter

    • 添加Filter类
      @Component
      public class MyFilter extends ZuulFilter {
      
          public static final Logger logger =         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() {
            HttpServletRequest reques = RequestContext.getCurrentContext().getRequest();
            String host = reques.getRemoteHost();
            MyFilter.logger.info("请求的host:" + host);
            return null;
        }  
      }
      
  2. 禁用Zuul默认的Filter

    Zuul for Spring Cloud comes with a number of ZuulFilter
    beans enabled by default in both proxy and server mode. See the zuul filters package for the possible filters that are enabled. If you want to disable one, simply set zuul...disable=true
    . By convention, the package after filters
    is the Zuul filter type. For example to disable org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter
    setzuul.SendResponseFilter.post.disable=true

官方文档说的很清楚了,如果想禁用某个Filter,添加配置
zuul...disable=true

你可能感兴趣的:(SpringCloud---GateWay---Zuul(API网关))