美文网首页
SpringCloud 集成网关Zuul

SpringCloud 集成网关Zuul

作者: kaixingdeshui | 来源:发表于2020-11-13 20:25 被阅读0次

SpringCloud 集成网关Zuul

Zuul简介

1.zuul是Netflix开源的一个APIGateway服务器,本质是一个web应用;
2.Zuul是开源提供动态链路,监控,弹性,安全等边缘服务的框架;
3.Zuul相当于是服务和所有web请求的大门

官方说明文档
https://github.com/Netflix/zuul/wiki

集成示例:
1.pom导入依赖

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

2.启动类

@SpringBootApplication
//
@EnableZuulProxy
@EnableDiscoveryClient
public class LearnZuulApplication {
    public static void main(String[] args) {
        SpringApplication.run(LearnZuulApplication.class, args);
    }
}
  1. application配置
server:
  port: 7000
spring:
  application:
    name: zuul-demo

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

#####
##网关配置
zuul:
  host:
    ##代理普通的http请求 连接超时时间
    connect-timeout-millis: 2000
    # socket超时时间
    socket-timeout-millis: 1000
    # 最大连接200
    max-total-connections: 200
    # 每个地址最大连接数
    max-per-route-connections: 20
    #信号量 模式
  ribbon-isolation-strategy: semaphore
  semaphore:
    #最大的信号量数
    max-semaphores: 100
    # 路由
  routes:
    route1:
      # 代理路径 http://localhost:7000/test1/client1 即转到 http://localhost:8081/client1
      path: /test1/**
      url: http://localhost:8081
    route2:
      # http://localhost:7000/test2/client_1 代理路径 http://localhost:8081/client_1
      path: /test2/**
      serviceId: helloserver
    user-token:
      path: /token/byPhone
      serviceId: user-demo
  #      忽略services
  ignored-services: helloserver

study:
  zuul:
    token-filter:
      noAuthenticationRoutes: user-token

token:
  jwt:
    key: kkk123
    iss: 11
    expm: 10

#容错机制 资源隔离模式下默认是信号量
hystrix:
  command:
    execution:
      timeout:
        enabled: true
    isolation:
      thread:
        timeoutInmilliseconds: 100

zuul执行流程

ZuulServlet/ZuulServletFilter

preRoute() //前置
route() //中置
postRoute() // 返回结果
error() //

URL请求的拦截

image.png

循环处理filter


image.png image.png
image.png
image.png

处理单个的filter


image.png

IZuulFilter 下的所有Filter实现类


image.png

serviceId 的拦截
Ribbon转发


image.png
image.png

获取serviceId,创建转发对象HttpClient


image.png
发起请求
image.png
ZuulServerAutoConfiguration
FilterRegistrationBean
image.png
image.png

相关文章

网友评论

      本文标题:SpringCloud 集成网关Zuul

      本文链接:https://www.haomeiwen.com/subject/dojpbktx.html