Spring Cloud Zuul概述了解springcloud架构可以加求求:三五三六二四七二五九
当我们在使用微服务的时候,完成一个业务可能需要同时调用多个微服务,则需要分别请求多个服务接口,首先客户端需要请求不同的微服务,客户端的复杂性增大认证复杂,每个微服务都需要自己独立的认证方式,某些情况下会存在一些问题,例如跨域问题,每个服务存在的协议可能不同。
Spring Cloud Zuul是基于Netflix Zuul实现的API网关,他可以和Eureka,Ribbon,Hystrix等组件配合使用。他除了Zuul Core还整合了actuator,hystrix,ribbon,提供动态路由,监控,弹性,安全等的边缘服务。Zuul组件的核心是一系列的过滤器,这些过滤器可以完成以下功能:
身份认证和安全: 识别每一个资源的验证要求,并拒绝那些不符的请求
审查与监控:
动态路由:动态将请求路由到不同后端集群
压力测试:逐渐增加指向集群的流量,以了解性能
负载分配:为每一种负载类型分配对应容量,并弃用超出限定值的请求
静态响应处理:边缘位置进行响应,避免转发到内部集群
多区域弹性:跨域AWS Region进行请求路由,旨在实现ELB(ElasticLoad Balancing)使用多样化
Zuul使用的默认是Apache的HTTP Client,也可以使用Rest Client或者okhttp3.OkHttpClient。使用Rest Client可以设置ribbon.restclient.enabled=true,使用okhttp3.OkHttpClient可以设置ribbon.okhttp.enabled=true。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>Application类上
@EnableZuulProxy //打开zuulyml
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream自定义指定微服务的访问路径
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
provider-server: /provider/**
consumer-server: /consumer/**
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream忽略指定微服务,代理其他微服务
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
ignored-services: provider-server,consumer-server
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream忽略所有微服务,路由指定微服务
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
ignored-services: '*' # 使用'*'可忽略所有微服务
routes:
provider-server: /provider/**
consumer-server: /consumer/**
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream同时指定微服务的serviceId和对应路径
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
provider-route: # 该配置方式中,provider-route只是给路由一个名称,可以任意起名。
service-id: provider-server
path: /provider/** # service-id对应的路径
consumer-route: # 该配置方式中,consumer-route只是给路由一个名称,可以任意起名。
service-id: consumer-server
path: /consumer/** # service-id对应的路径
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream同时指定path和url
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
provider-route: # 该配置方式中,provider-route只是给路由一个名称,可以任意起名。
url: http://localhost:8000/ # 指定的url
path: /provider/** # url对应的路径。
consumer-route: # 该配置方式中,consumer-route只是给路由一个名称,可以任意起名。
url: http://localhost:8010/ # 指定的url
path: /consumer/** # url对应的路径。
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream同时指定path和URL,并且不破坏Zuul的Hystrix、Ribbon特性
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
provider-route: # 该配置方式中,provider-route只是给路由一个名称,可以任意起名。
path: /provider/**
service-id: provider-server
ribbon:
eureka:
enabled: false # 禁用掉ribbon的eureka使用
provider-server:
ribbon:
listOfServers: localhost:8000,localhost:8001
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream为Zuul添加映射前缀1
strip-prefix是剥离前缀的意思,设置为false就是不剥离前缀,Zuul默认是剥离前缀的。
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
prefix: /api
strip-prefix: false
routes:
provider-server: /provider/**
logging:
level:
com.netflix: DEBUG
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream
# 访问Zuul的/api/provider-server/1路径,请求将会被转发到provider-server的/api/1,可查看日志打印,有助于理解。为Zuul添加映射前缀2
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
provider-server:
path: /provider/**
strip-prefix: false
logging:
level:
com.netflix: DEBUG
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream
# 这样访问Zuul的/provider/1路径,请求将会被转发到provider-server的/provider/1,可查看日志打印,有助于理解。忽略某些路径
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
ignoredPatterns: /**/admin/** # 忽略所有包括/admin/的路径
routes:
provider-server: /provider/**
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream本地转发
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
route-name:
path: /path-a/**
url: forward:/path-b
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream
使用正则表达式指定zuul的路由匹配规则
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
@Bean
public PatternServiceRouteMapper serviceRouteMapper() {
// 调用构造函数PatternServiceRouteMapper(String servicePattern, String routePattern)
// servicePattern指定微服务的正则
// routePattern指定路由正则
return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)", "${version}/${name}");
}
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}Zuul的安全与Heard
如果不希望敏感Headers向下游泄漏到外部服务器,可以在路由配置中指定忽略的Headers列表
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
provider-route: # 该配置方式中,provider-route只是给路由一个名称,可以任意起名。
url: http://localhost:8000/ # 指定的url
path: /provider/** # url对应的路径。
sensitive-headers: Cookie,Set-Cookie,Authorization # 过滤
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.streamsensitive-headers是黑名单,默认不为空,因此要使Zuul发送所有Headers(“忽略”的Headers除外),您必须将其明确设置为空。 如果要将cookie或授权Headers传递给后端,则必须执行此操作。
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
routes:
provider-route: # 该配置方式中,provider-route只是给路由一个名称,可以任意起名。
url: http://localhost:8000/ # 指定的url
path: /provider/** # url对应的路径。
sensitive-headers: # 不过滤,则须显式设为空。
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream也可以通过设置zuul.sensitive-headers来全局设置敏感的Headers。 如果在路由上设置了sensitive-headers,则会覆盖全局sensitive-headers设置。
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
sensitive-headers: # 全局不过滤
routes:
provider-route: # 该配置方式中,provider-route只是给路由一个名称,可以任意起名。
url: http://localhost:8000/ # 指定的url
path: /provider/** # url对应的路径。
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream
忽略某些head
server:
port: 8040
spring:
application:
name: gateway-zuul-server
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
zuul:
ignored-headers: header1,header2 # header1,header2不会传播到其他服务
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*" #因为springboot2.1.必须加上,支持访问/actuator/hystrix.stream来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69952307/viewspace-3164486/ ,如需转载,请注明出处,否则将追究法律责任。