Zuul是Netflix提供的一个开源的API网关服务器,SpringCloud对Zuul进行了整合和增强。服务网关Zuul聚合了所有微服务接口,并统一对外暴露,外部客户端只需与服务网关交互即可。相对于内部服务而言,能够防止其被外部客户端直接访问而暴露服务的敏感信息,起到了保护作用。除此之外,Zuul还可以实现身份认证、数据监控、动态路由等功能。
<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>com.cf</groupId>
<artifactId>sc-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>sc-gateway</artifactId>
<dependencies>
<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>
</dependencies>
</project>
package gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
@EnableZuulProxy是@EnableZuulServer的超集,@EnableZuulProxy包含@EnableZuulServer导入的所有过滤器。
@EnableZuulProxy使用反向代理,@EnableZuulServer不使用任何代理。
server:
port: 8088
spring:
application:
name: sc-gateway
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/
zuul:
routes:
sc-provider: /sp/** #将serviceId为sc-provider的服务映射到/sp/**路径
依次启动注册中心sc-eureka、提供者sc-provider、网关sc-gateway,以下是通过Zuul访问提供者和直接访问提供者的结果:
zuul:
ignored-services: serviceId1,serviceId2 #忽略服务serviceId1,serviceId2
zuul:
ignored-services: '*' #*为忽略所有服务,只代理sc-provider服务
routes:
sc-provider: /sp/**
zuul:
prefix: /yc
routes:
sc-provider: /sp/**
zuul:
routes:
sc-provider:
path: /sp/**
url: http://localhost:8081 #指定服务sc-provider的url,不从Eureka注册中心获取。
这种配置方式不会作为HystrixCommand执行,也不会使用Ribbon来平衡多个url的负载。要实现这些目标,可以使用静态服务器列表(listOfServers)或者指定serviceId。
zuul:
routes:
sc-provider:
path: /sp/**
sensitiveHeaders: Cookie,Set-Cookie,Authorization #将会覆盖全局zuul.sensitiveHeaders的值
url: http://localhost:8081
如果要设置全局的敏感Header可以设置zuul.sensitiveHeaders的值。Cookie,Set-Cookie,Authorization为sensitiveHeaders的默认值,如果不想设置敏感header,可以把sensitiveHeaders设置成空列表:
zuul:
routes:
sc-provider:
path: /sp/**
sensitiveHeaders:
url: http://localhost:8081
zuul:
ignoredHeaders: Header1, Header2 #Header1和Header2将不会传播到其他的微服务中
当@EnableZuulProxy和Spring Boot Actuator配合使用时,Zuul会暴露一个路由管理端点/routes,通过这个路由端点可以查看到Zuul当前映射的路由列表信息。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
management:
endpoints:
web:
exposure:
include: 'routes'
依次启动注册中心sc-eureka、提供者sc-provider、网关sc-gateway,然后访问http://localhost:8088/actuator/routes/details,显示路由列表信息如下:
Zuul的核心是一系列过滤器,它们能够在HTTP请求和响应路由期间执行一系列操作。Zuul提供了一个框架来动态读取、编译和运行这些过滤器,过滤器之间不直接通信,它们通过对每个请求惟一的RequestContext共享数据。
新建类gateway.filter.MyZuulFilter:
package gateway.filter;
import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
public class MyZuulFilter extends ZuulFilter{
/**
* 具体执行逻辑
*/
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
if (request.getParameter("name") != null) {
System.out.println("你好," + request.getParameter("name"));
}
return null;
}
/**
* 判断你该过滤器是否要执行
*/
@Override
public boolean shouldFilter() {
return true;
}
/**
* 过滤器执行顺序
*/
@Override
public int filterOrder() {
return 1;
}
/**
* 过滤器类型
*/
@Override
public String filterType() {
return "pre";
}
}
启动类GatewayApplication中添加配置:
@Bean
public MyZuulFilter MyZuulFilter(){
return new MyZuulFilter();
}
依次启动注册中心sc-eureka、提供者sc-provider、网关sc-gateway,然后访问http://localhost:8088/sp/book/list?name=小明,MyZuulFilter过滤器将会执行,控制台输出:你好,小明。
SpringCloud学习笔记(6):使用Zuul构建服务网关
原文:https://www.cnblogs.com/seve/p/11551546.html