Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。
为了备份源码,复制项目使用。
<?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.gf</groupId>
<artifactId>service-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-zuul</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.gf</groupId>
<artifactId>chapter05</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>
</dependencies>
</project>
在其入口applicaton类加上注解@EnableZuulProxy,开启zuul的功能:
package com.gf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableZuulProxy
public class ServiceZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceZuulApplication.class, args);
}
}
加上配置文件application.yml加上以下的配置代码:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 8769
spring:
application:
name: service-zuul
zuul:
routes:
api-a:
path: /api-a/**
serviceId: service-ribbon
api-b:
path: /api-b/**
serviceId: service-feign
首先指定服务注册中心的地址为http://localhost:8761/eureka/,服务的端口为8769,服务名为service-zuul;以/api-a/ 开头的请求都转发给service-ribbon服务;以/api-b/开头的请求都转发给service-feign服务;
依次运行这五个工程;打开浏览器访问:http://localhost:8769/api-a/hi ;浏览器显示:
hello , port is 8762
打开浏览器访问:http://localhost:8769/api-b/hi ;浏览器显示:
hello , port is 8762
这说明zuul起到了路由的作用。
zuul不仅只是路由,并且还能过滤,做一些安全验证。继续改造工程;
package com.gf.config;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Component
public class MyFilter extends ZuulFilter{
private static Logger logger = LoggerFactory.getLogger( MyFilter.class);
/**
* pre : 路由访问之时
*/
@Override
public String filterType() {
return "pre";
}
/**
* filterOrder : 过滤的顺序
*/
@Override
public int filterOrder() {
return 0;
}
/**
* shouldFilter : 这里可以写逻辑判断,是否过滤
*/
@Override
public boolean shouldFilter() {
return true;
}
/**
* run : 过滤的具体逻辑判断,可以很复杂,包括sql,nosql去判断请求有没有权限访问
*/
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
logger.info( String.format("%s >>> %s" , request.getMethod() , request.getRequestURL()) );
Object accessToken = request.getParameter( "token" );
if (null == accessToken) {
logger.warn( "token is empty" );
ctx.setSendZuulResponse( false );
ctx.setResponseStatusCode( 401 );
try {
ctx.getResponse().getWriter().write( "token is empty" );
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
logger.info( "ok" );
return null;
}
}
filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:
这时访问:http://localhost:8769/api-a/hi ;网页显示:
token is empty
访问 http://localhost:8769/api-a/hi?token=22 ;网页显示:
hello , port is 8762
评论