zuul主要功能以及基本配置

一、Zuul主要有的两大功能:对请求路由和过滤

二、基本使用

 

一、Zuul主要有的两大功能:对请求路由和过滤

其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.

Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得.

注意:Zuul服务最终还是会注册进Eureka

二、基本使用

2.1最基本的zuul

我们首先在Eureka注册一个user服务

我们直接访问http://localhost:7900/simple/1是可以访问的

我们创建一个项目,使用zuul进行反向代理

添加依赖pom.xml

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>

启动类添加@EnableZuulProxy注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

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

application.yml配置,将zuul注册到eureka中

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

启动zuul,那么它会将注册到eruka的user服务进行反向代理,具体可以使用

localhost:ZUUL_PORT/abc/simle/1,其中abc为服务名。我们将zuul也注册到eruka中

我们访问user服务可以通过网关zuul进行访问http://localhost:8040/microservice-provider-user/simple/1

 

2.2、忽略指定微服务使用ignored-services:属性,如

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  ignored-services: microservice-provider-user,microservice-consumer-movie
management:
  security:
    enabled: false

2.3、忽略所有微服务,只路由指定的微服务

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  ignored-services: '*'   # 使用'*'可忽略所有微服务
  routes:
    microservice-provider-user: /user/**
management:
  security:
    enabled: false

2.4、同时指定微服务的serviceId和对应路径

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  routes:
    user-route:                   # 该配置方式中,user-route只是给路由一个名称,可以任意起名。
      service-id: microservice-provider-user
      path: /user/**              # service-id对应的路径
management:
  security:
    enabled: false

通过这个属性,如上面的例子我们可以直接使用

2.5、同时指定path和url

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  routes:
    user-route:                   # 该配置方式中,user-route只是给路由一个名称,可以任意起名。
      url: http://localhost:8000/ # 指定的url
      path: /user/**              # url对应的路径。
management:
  security:
    enabled: false

2.6、为Zuul添加映射前缀,使用zuul.prefix

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
zuul:
  prefix: /api
  strip-prefix: false
  routes:
    microservice-provider-user: /user/**
logging:
  level:
    com.netflix: DEBUG
management:
  security:
    enabled: false

# 访问Zuul的/api/microservice-provider-user/1路径,请求将会被转发到microservice-provider-user的/api/1,可查看日志打印,有助于理解。

2.7、忽略某些路径ignoredPatterns属性(即这些路径不进行反向代理)

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
zuul:
  ignoredPatterns: /**/admin/**   # 忽略所有包括/admin/的路径
  routes:
    microservice-provider-user: /user/**
management:
  security:
    enabled: false

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot Zuul是一个在微服务架构中用于网关路由和过滤的组件。它允许我们通过定义动态路由配置来控制请求的转发。 要在Spring Boot Zuul配置动态路由转发,我们需要执行以下几个步骤: 1. 添加Zuul依赖:在pom.xml文件中添加Zuul的依赖,以便我们可以使用它的功能。 2. 创建Zuul配置类:创建一个Java类来配置Zuul。在该类上添加`@EnableZuulProxy`注解,以启用Zuul代理和路由功能。可以使用`@Configuration`注解标记该类为配置类。 3. 配置动态路由:在配置类中,使用`@ConfigurationProperties`注解来加载动态路由的配置,如下所示: ```java @ConfigurationProperties("zuul") public class ZuulConfig { private List<Route> routes; // Getters and setters public static class Route { private String path; private String url; private boolean stripPrefix; // Getters and setters } } ``` 在`ZuulConfig`类中,我们定义了一个内部类`Route`,它表示一条动态路由。我们需要配置每个路由的`path`、`url`和`stripPrefix`属性。 4. 启用动态路由配置:在配置类中添加`@Bean`注解来创建一个`ZuulConfiguration` bean,并使用它来启用动态路由配置。代码如下所示: ```java @Configuration public class ZuulConfig { // ... @Bean public ZuulConfiguration zuulConfiguration() { ZuulConfiguration zuulConfiguration = new ZuulConfiguration(); zuulConfiguration.setRoutes(this.routes); return zuulConfiguration; } } ``` 在`zuulConfiguration()`方法中,我们创建了一个`ZuulConfiguration`对象,并将动态路由配置赋值给它。 5. 测试路由转发:完成以上步骤后,我们可以通过访问定义的动态路由来测试路由转发是否正常工作。根据配置的`path`属性,Zuul将会把请求转发到相应的`url`。 这是使用Spring Boot Zuul配置动态路由转发的基本步骤。通过配置动态路由,我们可以动态地将请求转发到不同的后端服务,并根据路由规则对请求进行过滤和重定向。这使得网关服务在微服务架构中起到关键的作用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值