Zuul路由网关(SpringCloud五大组件之一)

编程知识 行业动态 更新时间:2024-06-13 00:22:06

Zuul路由网关

概念

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

为何需要

微服务架构体系中,通常一个业务系统会有很多的微服务,比如:OrderService、ProductService、UserService…,为了让调用更简单,一般会在这些服务前端再封装一层,类似下面这样:

前面这一层俗称为“网关层”,其存在意义在于,将"1对N"问题 转换成了"1对1”问题(路由),同时在请求到达真正的微服务之前,可以做一些预处理(过滤),比如:来源合法性检测,权限校验,反爬虫之类…


官方资料(https://github/Netflix/zuul)

使用

1.创建zuul_gatewar_9527模块(例)

2.导jar包

<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>

3.yml配置

server:
  port: 9527 #端口号
spring:
  application:
    name: microservice-zuul #名称
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka 
zuul:
	  routes:
	    user-provider: /user/** #以/user开头的所有路径都转发给user-provider进行处理
	  ignored-services: "*" #忽略掉服务名访问方式 使用上面/user的方式来访问
	  prefix: "/services" #添加 前缀访问  localhost:9527/services/user/provider/user/2

4.创建启动类

@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication9527 {

    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication9527.class);
    }
}

5.启动测试 http://localhost:9527/services/user/provider/user/1 (例)

更多推荐

Zuul路由网关(SpringCloud五大组件之一)

本文发布于:2023-04-01 17:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/120ca345a0a432a87f50f066a5fd0ac4.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:五大   网关   路由   组件   Zuul

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!