Spring Cloud Zuul路由转发(四)
ZUUL服务网关:
通过服务网关统一向外界提供REST API 接口的方式
具备路由,负载均衡,请求拦截,权限控制等功能
自身也是一个微服务,与其他服务一样注册在Eureka上,可以互相发现
zuul能感知其它服务在线情况,根据配置的路由规则,将请求自动转发到指定的后台服务上,对于一些比如权限认证,token校验,可以放在ZuulFilter过滤去中实现
创建一个新的项目
pox文件配置
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.leo</groupId> <artifactId>zuulservice</artifactId> <version>0.0.1-SNAPSHOT</version> <name>zuulservice</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </properties> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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>
在启动类中添加注解 @EnableZuulProxy,开启zuul的功能:
@SpringBootApplication @EnableZuulProxy @EnableEurekaClient public class ZuulserviceApplication { public static void main(String[] args) { SpringApplication.run(ZuulserviceApplication.class, args); } }
修改资源文件中的配置
server.port=1005 spring.application.name=service-zuul eureka.client.serviceUrl.defaultZone=http://xxxx/eureka/ zuul.routes.api-bibbon.path=/api-bibbon/** zuul.routes.api-bibbon.service-id=service-ribbon zuul.routes.api-feigin.path=/api-feigin/** zuul.routes.api-feigin.service-id=service-feigin
eureka.client.serviceUrl.defaultZone 指定注册中心地址
spring.application.name 指定当前项目的服务名称
zuul.routes.api-bibbon.path=/api-bibbon/** 表示所有以/api-bibbon/ 开头的请求全部转发到 service-ribbon服务上
将client、feign、ribbon、zuul项目全部启动起来,erueka服务全部正常
这里请求api-feign服务并转入参数
zuul请求转发
服务过滤
使用zuul的过滤器来做token验证,新写一个class去继承ZullFilter 会要实现过滤器中的方法
@Component public class MyFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(MyFilter.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(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString())); Object accessToken = request.getParameter("token"); if(accessToken == null) { log.warn("token is empty"); ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); try { ctx.getResponse().getWriter().write("token is empty"); }catch (Exception e){} return null; } log.info("ok"); return null; } }
filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:
pre:路由之前
routing:路由之时
post: 路由之后
error:发送错误调用
filterOrder:过滤的顺序
shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。
此时请求时,参数没有token的时候会抛出filter中订单的token为空的错误
加上token,调用成功