Zuul路由网关的简介和简单使用

it2025-12-11  2

文章目录

Zuul路由网关一、概述二、Zuul的简单使用

Zuul路由网关


一、概述

什么是Zuul?

Zuul 包含了对请求的路由和过滤两个最主要的功能:

路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础过滤功能负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础

Zuul 和Eureka进行整合,将Zuul自身注册为 Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。

总之:Zuul服务最终仍会注册到Eureka;提供代理、路由、过滤三大功能。


二、Zuul的简单使用

1、导入依赖:

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> <version>1.4.7.RELEASE</version> </dependency>

2、编写配置信息:

server: port: 9527 eureka: client: service-url: defaultZone: http://localhost:7001/eureka/ instance: instance-id: zuul-9527.com prefer-ip-address: true # 可以显示服务的ip地址 info: app.name: xingyu-springcloud company.name: xingyu-XJTU zuul: routes: mydept.serviceId: springcloud-provider-dept mydept.path: /mydept/** ignored-services: "*" #不能使用该路径进行访问,忽略 prefix: /xingyu #设置公共前缀

3、主启动类中开启Zuul,使用注解@EnableZuulProxy:

@SpringBootApplication @EnableZuulProxy public class ZuulApplcation9527 { public static void main(String[] args) { SpringApplication.run(ZuulApplcation9527.class, args); } }

4、启动Zuul,之后客户端的所有访问都可以通过Zuul(端口9527)进行

最新回复(0)