SpringCloud---zuul路由网关

07-17 1360阅读

zuul网关

zuul网关定义

Zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet(filter)应用。Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门入口,也要注册入Eureka.

zuulw网关作用

对前端请求进行拦截和过滤,对于符合要求的请求进行放行,并通过网关分发到对应的后端微服务中。

SpringCloud---zuul路由网关

Zuul过滤器 

Zuul作为网关的其中一个重要功能,就是实现请求的鉴权(认证授权)。而这个动作我们往往是通过Zuul提供的过滤器来实现的。

过滤器执行周期

- 正常流程:

  - 请求到达首先会经过pre类型过滤器,而后到达routing类型,进行路由,请求就到达真正的服务提供者,执行请求,返回结果后,会到达post过滤器。而后返回响应。

- 异常流程:

  - 整个过程中,pre或者routing过滤器出现异常,都会直接进入error过滤器,再error处理完毕后,会将请求交给POST过滤器,最后返回给用户。

  - 如果是error过滤器自己出现异常,最终也会进入POST过滤器,而后返回。

  - 如果是POST过滤器出现异常,会跳转到error过滤器,但是与pre和routing不同的时,请求不会再到达POST过滤器了。

代码实现zuul网关

pom.xml

    4.0.0
    
        org.example
        Springcloud-Netflix
        1.0-SNAPSHOT
    
    SpringCloud-Gateway
    jar
    SpringCloud-Gateway
    http://maven.apache.org
    
        UTF-8
    
    
        
            junit
            junit
            3.8.1
            test
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

application.yml

server:
  port: 8848
spring:
  application:
    name: Zuul-GateWay
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:10072/eureka/
    instance:
      prefer-ip-address: true
zuul:
  routes:
    user.serviceId: user-service
    user.path: /user/**
    order.serviceId: order-service
    order.path: /order/**
  ignored-services: "*"
  prefix: "/test"

启动类

package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
 * @ClassName ZuulGateWayStart
 * @Author 23
 * @Date 2024/7/12 10:30
 * @Version 1.0
 * @Description TODO
 **/
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulGateWayStart {
    public static void main(String[] args) {
        SpringApplication.run(ZuulGateWayStart.class,args);
    }
}

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]