zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用。
Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门。
下面介绍如何在springcloud项目中使用zuul进行服务路由
1.pom.xml中引入zuul与eureka依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
2.在application.yml文件中配置服务发现中心与服务路由配置(这里直接使用了与eureka集成的配置没有进行传统路由的配置)
spring:
application:
name: gateway
#zuul与eureka整合
eureka:
client:
serviceUrl:
defaultZone: http://localhost:50001/eureka/
#配置hello-service与feign-consumer服务的路由
zuul:
routes:
hello-service:
serviceId: hello-service
feign-consumer:
serviceId: feign-consumer
server:
port: 5555
3.在主类中开启zuul路由服务
package com.cfh.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy//开启Zuul路由
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
在浏览器访问zuul gateway服务(http://localhost:5555/路由名称/接口)。zuul会根据服务注册中心提供的详细路由提供服务的实例。