SpringCloud---zuul路由网关

zuul网关

zuul网关定义

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

zuulw网关作用

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

Zuul过滤器

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

过滤器执行周期

  • 正常流程:

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

  • 异常流程:

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

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

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

代码实现zuul网关

pom.xml

XML 复制代码
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>Springcloud-Netflix</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>SpringCloud-Gateway</artifactId>
    <packaging>jar</packaging>

    <name>SpringCloud-Gateway</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!--springboot支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <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-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
</project>

application.yml

XML 复制代码
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"

启动类

java 复制代码
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);
    }
}
相关推荐
天远数科2 分钟前
零信任架构实战:基于天远车辆过户详版查询构建自动化抵押车权属合规审查网关
人工智能·python·架构·自动化
冰美式只要糖12 分钟前
同一个 Seed,为什么插入另一条请求会改变采样结果?
后端
Xmi16 分钟前
LLM 多步任务总是漏步骤?一次 Prompt Chaining 改造的完整记录
后端
sean_wang1218 分钟前
Rust 轻量级虚拟机 smolvm 实战:200ms 冷启动、OCI 镜像直跑、单文件打包
开发语言·后端·rust·大鲸华章
badhope26 分钟前
3 个 Agent 并行发文章:我用 SQLite 任务表止住重复
后端·python·ai编程
Gopher_HBo29 分钟前
手写 cobra 命令树与 Find
后端
Xmi35 分钟前
Windows容器部署EKS:从 Dockerfile 到 CloudFront 的一次打通
后端
青山木1 小时前
JVM 类加载全解:Class 文件结构、加载链接初始化与双亲委派
java·jvm·后端·类加载·双亲委派机制
十铭忘2 小时前
Agent 架构分层:大脑、手脚,以及它们各自该放在哪
人工智能·架构
二宝哥3 小时前
07.Spring AI 接入智谱文生图:从配置到实战
java·人工智能·spring