一、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