springcloud学习(六)之zuul

一、介绍

Spring Cloud Zuul路由是微服务架构的不可或缺的一部分,提供动态路由,监控,弹性,安全等的边缘服务。Zuul是Netflix出品的一个基于JVM路由和服务端的负载均衡器。

二、开发

1、新建项目hjduan.spring.cloud.zuul pom如下

<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.0modelVersion>
    <parent>
        <groupId>com.hjduan.spring.cloudgroupId>
        <artifactId>hjduan.spring.cloudartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <artifactId>hjduan.spring.cloud.zuulartifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zuulartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

2、application.yml配置

server:
  port: 8767
eureka:
  client:
    serviceUrl:
      defaultZone: http://admin:admin@localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}

spring:
  application:
    name: hjduan-spring-cloud-zuul
#zuul 充当路由
zuul:
  routes:
    api-a:
      path: /api-a/**
      serviceId: HJDUAN-SPRING-CLOUD-RIBBON
    api-b:
      path: /api-b/**
      serviceId: HJDUAN-SPRING-CLOUD-FEIGN

3、编写一个过滤器MyFilter.java

package com.hjduan.spring.cloud.zuul.filter;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

/**
 * filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:
 * 
 * pre:路由之前
 * 
 * routing:路由之时
 * 
 * post: 路由之后
 * 
 * error:发送错误调用
 * 
 * filterOrder:过滤的顺序
 * 
 * shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
 * 
 * run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。
 * 
 * 作者: 段浩杰 2017年7月25日
 */
@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;
    }
}

4、编写启动类

package com.hjduan.spring.cloud.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ServiceZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceZuulApplication.class, args);
    }
}

5、启动hjduan.spring.cloud.eureka,hjduan.spring.cloud.consumer,修改端口再次启动hjduan.spring.cloud.consumer,hjduan.spring.cloud.feign,hjduan.spring.cloud.hystrix,hjduan.spring.cloud.ribbon,hjduan.spring.cloud.zuul,然后运行http://localhost:8767/api-a/hi/123和http://localhost:8767/api-b/hi/123观察结果

你可能感兴趣的:(springcloud)