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五大组件之一)
发布评论