API网关服务SpringCloudZuul
文章目录
快速入门
创建项目名为 zuul
pom 依赖
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.zk.springcloud.zuul</groupId>
<artifactId>springcloud-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-zuul</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class SpringcloudZuulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudZuulApplication.class, args);
}
}
配置文件
spring.application.name=api-gateway
server.port=9002
eureka.client.service-url.defaultZone=http://localhost:9001/eureka/,http://localhost:9004/eureka/
请求路由
传统路由方式
添加如下配置
zuul.routes.feign-consumer.path=/feign-consumer/**
zuul.routes.feign-consumer.url=http://localhost:9010/
访问 http://localhost:9002/feign-consumer/feign-consumer 可获取到数据
面向服务的路由
添加如下配置
zuul.routes.feign-consumer.path=/feign-consumer/**
zuul.routes.feign-consumer.service-id=FEIGN-CONSUMER
访问 http://localhost:9002/feign-consumer/feign-consumer 获得相同结果
请求过滤
package com.zk.springcloud.zuul;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
public class AccessFilter extends ZuulFilter {
private static Logger log = LoggerFactory.getLogger(AccessFilter.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info("send {} request to {}", request.getMethod(), request.getRequestURL().toString());
Object accessToken = request.getParameter("accessToken");
if(accessToken == null) {
log.warn("access token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
return null;
}
log.info("access token ok");
return null;
}
}
启动类添加
@Bean
public AccessFilter accessFilter(){
return new AccessFilter();
}
访问 http://localhost:9002/feign-consumer/feign-consumer?accessToken 正常返回数据
访问 http://localhost:9002/feign-consumer/feign-consumer 返回 401
路由详解
传统路由配置
单例模式
zuul.routes.feign-consumer.path=/feign-consumer/**
zuul.routes.feign-consumer.url=http://localhost:9010/
多实例配置
zuul.routes.feign-consumer.path=/service-user/**
zuul.routes.feign-consumer.service-id=SERIVCE-USER-CUSTOM
ribbon.eureka.enabled=false
SERIVCE-USER-CUSTOM.ribbon.listOfServers=http://localhost:9003,http://localhost:9006
服务路由配置
zuul.routes.feign-consumer.path=/feign-consumer/**
zuul.routes.feign-consumer.service-id=FEIGN-CONSUMER
也可以使用
zuul.routes.SERIVCE-USER=/service-user/**
忽略表达式
zuul.ignored-patterns=/**/hello/**
zuul.routes.service-user.path=/service-user/**
zuul.routes.service-user.service-id=SERIVCE-USER
访问 http://localhost:9002/service-user/hello?accessToken 此时这个链接不能访问
路由前缀
增加前缀
zuul.prefix=/api
关闭移除代理前缀
zuul.stripPrefix=false
关闭指定路由移除代理前缀
zuul.routes.<route>.strip-prefix=true
Cookie 与头信息
默认的敏感头信息通过 zuul.sensitiveHeaders 参数定义,包括 Cookie、Set-Cookie、Authorization 进行过滤
设置 Cookie 可传递
通过设置全局参数为空来覆盖默认值
zuul.sensitiveHeaders=
或者通过指定路由的参数配置
# 方法一:对指定路由开启自定义敏感头
zuul.routes.<router>.customSensitiveHeaders=true
# 方法二:将指定路由的敏感头设置为空
zuul.routes.<router>.sensitiveHeaders=
重定向问题
zuul.addHostHeader=true
只在 Camden 版本起作用
Hystrix 和 Ribbon 支持
#设置API网关中路由转发请求的 HystrixCommand 执行超时时间,单位为毫秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
#设置路由转发,创建请求连接的超时时间
ribbon.ConnectTimeout
#设置路由转发请求的超时时间
ribbon.ReadTimeout
#关闭重试机制
zuul.retryable=false
zuul.routes.<route>.retryable=false