zuul的主要功能是路由转发和过滤,比如让所有/api-a/*的请求都转发到服务器a上,所有/api-b/*的请求都转发到服务器b上,zuul默认和ribbon结合实现了负载均衡的功能。

1 zuul的路由转发

  1.1 创建springboot项目,引入相关依赖

<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.beifeng.hadoop</groupId>
    <artifactId>beifeng-spring-cloud-zuul</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>beifeng-spring-cloud-zuul</name>
    <url>http://maven.apache.org</url>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 声明为web项目 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 配置eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        
        <!-- 配置zuul -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

  1.2 在配置文件中配置路由转发策略

eureka: 
 client: 
  serviceUrl: 
   defaultZone: http://localhost:8761/eureka/ #注册服务器地址
server:
  port: 8766
spring:
  application:
    name: cloud-zuul
zuul: 
 routes: 
  api-a: 
   path: /api-a/** #所有的/api-a开头的请求都转发到cloud-consumer-ribbon服务器上
   serviceId: cloud-consumer-ribbon
  api-b: 
   path: /api-b/** #所有的/api-b开头的请求都转发到cloud-consumer-feign服务器上
   serviceId: cloud-consumer-feign

  1.3 在启动类中声明启用zuul

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class CloudZuul {
    public static void main(String[] args) {
        SpringApplication.run(CloudZuul.class, args);
    }
}

  1.4 启动查看结果

    依次启动eureka-server、eureka-client、cloud-consumer-ribbon、cloud-consumer-feign、cloud-zuul项目

    

     

 2  路由的过滤

  2.1 创建自定义的路由过滤器,指定要过滤的请求和如何过滤

@Component
public class MyZuulFilter extends ZuulFilter {
    
    Logger logger=LoggerFactory.getLogger(MyZuulFilter.class);

    //过滤器的具体逻辑
    public Object run() {
        RequestContext context=RequestContext.getCurrentContext();
        HttpServletRequest request=context.getRequest();
        logger.info("url:"+request.getRequestURL().toString());
        Object token=request.getParameter("token");
        if (token==null||StringUtils.isEmpty(token.toString())) {
            logger.info("token is empty");
            context.setSendZuulResponse(false);
            context.setResponseStatusCode(401);
            try {
                context.getResponse().getWriter().write("token is empty");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        logger.info("ok");
        return null;
    }

    //是否要过滤,可以写逻辑判断
    public boolean shouldFilter() {
        RequestContext context=RequestContext.getCurrentContext();
        HttpServletRequest request=context.getRequest();
        String url=request.getRequestURL().toString();
        return url.contains("/api-a/");//只对api-a的请求进行验证
    }

    //过滤的顺序
    @Override
    public int filterOrder() {
        return 0;
    }

    //返回过滤器的类型,pre:路由前,routing:路由时,post:路由之后,error:发生错误时调用
    @Override
    public String filterType() {
        return "pre";
    }
}

  2.2 启动查看效果

     

              

 

posted on 2018-05-09 15:33  YL10000  阅读(281)  评论(0编辑  收藏  举报