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);
    }
}
相关推荐
Wang's Blog3 分钟前
Java 项目实战: 交友项目 - 技术架构与选型剖析
java·架构·交友
QAQmmttyyy10 分钟前
ChatFly 开发实录 ①|五天,从零到一个能卖的 Mac 工具
架构
海宇数据17 分钟前
零信任架构实战:基于海宇企业综合风险报告构建自动化供应商资质审查网关
运维·人工智能·架构·自动化
省钱兄--zs21 分钟前
无人自助拼豆系统架构设计与功能实现落地部署全流程实战指南
java·数据仓库·spring boot·系统架构·需求分析
骇客野人23 分钟前
企业4A架构(业务架构、应用架构、数据架构、安全架构)完整设计与搭建方案
安全·架构·安全架构
云策数链25 分钟前
用友U8+ UAP开发平台技术架构
架构·erp·用友·用友u8·云策数链
霍格沃兹测试学院-小舟畅学27 分钟前
AI Agent 运行时架构:Harness、Loop 与任务调度机制
人工智能·架构
Java爱好狂.28 分钟前
挑战一周速通Spring+SpringBoot+SpringCloud!
java·后端·spring·springboot·架构师·java面试·java程序员
杭州领祺科技34 分钟前
分布式光伏『四可』接入改造:绿电直连场景下的二次系统通信与安全架构(27 号令 4 区落位 + 7 产品边界)
分布式·安全·安全架构·光伏·电力·绿电直连·二次系统
柠檬味拥抱34 分钟前
基于 YOLO11 的 18 类户外生活垃圾智能检测系统:从模型到 Streamlit 全流程实战
后端