Spring Cloud Netflix 05 Zuul

7 Zuul

7.1 是什么

  • Zuul包含了对请求的路由和过滤两个最主要的功能:

    • 其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础,而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验,服务聚合等功能的基础。Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后
      的访问微服务都是通过Zuul跳转后获得。
  • 注意:Zuul服务最终还是会注册进Eureka

  • 提供:代理 + 路由 + 过滤 三大功能

7.2 集成zuul

  • pom.xml

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>
    
  • 配置文件

    server:
      port: 7289
    
    spring:
      application:
        name: gateway-zuul
    
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7000:7000/eureka/, http://eureka7001:7001/eureka/, http://eureka7002:7002/eureka/, http://eureka7003:7003/eureka/, http://eureka7004:7004/eureka/
        fetch-registry: true
        register-with-eureka: true
      instance:
        instance-id: zuul.com
        prefer-ip-address: true
    
    zuul:
      # 忽略所有的服务名
      ignored-services: "*"
      # 设置访问前缀
      prefix: /pbx
      # 路由设置,Zuul和Eureka结合使用,可以实现路由的自动配置,自动配置的路由以服务名称为匹配路径
      routes:
        [新访问路径]: [原访问路径]
    
  • 启动类

    @SpringBootApplication
    @EnableZuulProxy
    public class GatewayApplication {
        public static void main(String[] args) {
            SpringApplication.run(GatewayApplication.class, args);
        }
    }
    
posted @ 2021-04-12 17:13  PrimaBruceXu  阅读(50)  评论(0编辑  收藏  举报