Spring Cloud Zuul 网关

1、添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>

2、添加启动类

@SpringBootApplication
@EnableZuulProxy // 开启Zuul的网关功能
public class ZullServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZullServerApplication.class,args);
    }
}

3、添加配置

server:
 port: 9003
spring:
 application:
  name: zull-service

4 路由配置

4.1 简单入门案例

#路由配置

zuul:
  routes:
    #以订单微服务进行配置
    #路由ID ,任意指定
    product-server:
      path: /order-service/**  # 映射的路径 localhost:9003/product-service/**
      url: http://127.0.0.1:9001 #映射路径对应的实际微服务url 地址

访问路径:localhost:9003/order-service/order/...

Spring Cloud Zuul 网关
通过访问zuul 服务端口+ /order-service 即可跳转订单微服务调用 buy/1 接口

4.2 面向服务的路由

4.2.1 添加eureka 的依赖

<!--注册中心客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

4.2.2 开启eureka的客户端服务发现

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class ZullServerApplication {

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

4.2.3 在Zuul网关服务中配置eureka 的注册中心相关信息

  #注册中心 客户端
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:9000/eureka/  # 注册多个eurekaserver 之间用, 隔开
  instance:
    prefer-ip-address: true  #使用ip地址注册
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    lease-renewal-interval-in-seconds: 30 # 发送心跳间隔 默认 30
    lease-expiration-duration-in-seconds: 90 # 续约到期时间  默认 90

4.2.4 修改路由中的映射配置

#路由配置
zuul:
  routes:
    #以订单微服务进行配置
    #路由ID ,任意指定
    product-server:
      path: /order-service/**  # 映射的路径 localhost:9003/product-service/**
      #url: http://127.0.0.1:9002 #映射路径对应的实际微服务url 地址
      serviceId: service-order #配置转发的微服务的服务名称

跟上面的配置唯一区别 修改为 serviceId ,调用/order-service/** 转换成微服务模块的ID , 订单的服务模块: service-order

4.2.5 简化路由配置

zuul:
  routes:
     service-order: /order-service/** # servoce-order  表示就是服务模块的ID  

zuul 不进行路由配置,那么会有默认的路由配置
例如: 如果订单模块的服务ID service-order ,默认的请求映射路径 /service-product/** 在调用订单模块的API ,可以loaclhost:8099/service-order/buy/1 也可以调用订单的API接口

配置好eureka ,zull 网关,将服务模块的ID 作为routes 的id ,那么在调用service-order 订单模块API 只需要调用/order-service 即可跳转调用订单模块API接口

5 过滤器说明

Spring Cloud Zuul 网关

Spring Cloud Zuul 网关

pre: 转发到你微服务之前执行的过滤器
routing: 在路由请求时执行的过滤器
post: 执行微服务获取返回值之后执行的过滤器
error: 整个阶段抛出异常的时候执行的过滤器

5.1 自定义过滤器

/*
* 自定义PRE过滤器 
* */
@Component
public class LoginFilter extends ZuulFilter {
    /**
     * pre
     * routing
     * post
     * error
     * */
    @Override
    public String filterType() {
        return "pre";
    }
    @Override
    public int filterOrder() {
        return 1;
    }
    /*
    *当前过滤器是否生效
    * true: 使用此过滤器
    * false : 不适用此过滤器
    * */
    @Override
    public boolean shouldFilter() {
        return true;
    }
    /**
     * 指定过滤器业务逻辑
     * */
    @Override
    public Object run() throws ZuulException {
        System.out.println("在转发到微服务之前执行了过滤器");
        return null;
    }
}

测试案例: 在自定义pre过滤器中做身份验证,如果含有token 默认通过,为空则不通过

/**
     * 指定过滤器业务逻辑
     * */

    @Override
    public Object run() throws ZuulException {
       //获取上下文
        RequestContext requestContext = RequestContext.getCurrentContext();
      //获取当前的http 请求对象
        HttpServletRequest request = requestContext.getRequest();
      //获取参数,验证token
        String token = request.getParameter("access-token");
        System.out.println(token);
        if(token == null){
            System.out.println("身份验证失败");
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
        //身份验证失败,那么将返回401 错误
        }else{
            System.out.println("身份验证通过");
        }

        return null;
    }

ZUUL 网关不足

Spring Cloud Zuul 网关

本作品采用《CC 协议》,转载必须注明作者和本文链接
每天进步一点点
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!