spring-cloud-zuul及其应用

概述

网关为我们管理api接口提供的主要功能

  • 管理api接口
  • 适配协议
  • 安全认证
  • 转发路由
  • 限制流量
  • 监控日志
  • 防止爬虫
  • 灰度发布
  • 服务聚合

不建议使用zuul1作为线上网关使用,大家可以使用zuul2或者是spring-cloud-gateway作为微服务的网关

假如你使用zuul2作为网关的话,zuul1可以学习使用,其实基本功能类似,只是在底层改为netty去转发http请求

zuul1提供的功能

zuul的核心功能是过滤器,通过过滤器实现

  • 动态路由
  • 请求监控
  • 认证鉴权
  • 压力测试
  • 灰度发布

坑一

  • 注意zuul1和springboot的版本适配问题(zuul后面已经被spring-cloud干掉了,不在支持集成使用)
<dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>

基础的重定向,从path重定向到url

spring.application.name=zuul-gateway-static
server.port=9011
server.address=127.0.0.1

debug=true

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=ALWAYS

zuul.routes.test1.path=/abc/**
zuul.routes.test1.url=http://www.example.com/

zuul.routes.localpath.path=/a/**
zuul.routes.localpath.url=forward:/a

zuul.routes.userinfo.path=/user/**
zuul.routes.userinfo.service-id=zuul-user
zuul.routes.userinfo.stripPrefix=false
zuul.routes.userinfo.sensitive-headers=true
zuul.routes.userinfo.customSensitiveHeaders=true

zuul.retryable=true
ribbon.okhttp.enabled=true


eureka.instance.ip-address=127.0.0.1
eureka.client.serviceUrl.defaultZone=http://tom:123456@localhost:9010/eureka/
eureka.instance.preferIpAddress=true
eureka.instance.instance-id=${spring.application.name}:${server.address}:${server.port}
eureka.client.healthcheck.enabled=true
eureka.instance.lease-expiration-duration-in-seconds=20
eureka.instance.lease-renewal-interval-in-seconds=15

logging.config=classpath:logback.xml

查看所有routers的配置

  1. 访问服务:
  2. http://localhost:9011/abc/123
  3. 基础的匹配
zuul.routes.test1.path=/abc/**
zuul.routes.test1.url=http://www.example.com/
  1. 本地跳转zuul.routes.localpath.path=/a/**
    zuul.routes.localpath.url=forward:/a
  2. 使用eureka的匹配
  3. http://localhost:9011/user/1234
zuul.routes.userinfo.path=/user/**
zuul.routes.userinfo.service-id=zuul-user
zuul.routes.userinfo.stripPrefix=false
zuul.routes.userinfo.sensitive-headers=true
zuul.routes.userinfo.customSensitiveHeaders=true
  1. 自定义过滤器
  2. pre 请求被路由之前调用
  3. route 请求路由时调用
  4. post 在route和error过滤器之后调用
  5. error 在请求发生错误时调用

代码路径:

https://github.com/beckbikang/spring-cloud/tree/main/kzuul

实现动态路由的网关才是真正可用的网关

  1. 先上代码,基于redis存储了配置信息
  2. 继承SimpleRouteLocator类和实现了RefreshableRouteLocator接口

最最核心的代码了,如果需要实现动态路由,可以基于这个去改造了

package cn.beckbi.route;

import cn.beckbi.route.entity.ZuulEntity;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.*;


/**
 * @program: spring-cloud
 * @description:
 * @author: bikang
 * @create: 2022-08-14 13:04
 */
public class DynamicRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator {

    private static final String ZUUL_EKY = "LOCAL_ZUUL_RULES_REDIS_KEY";

    private ZuulProperties zuulProperties;

    public static String getRouteKey() {
        return ZUUL_EKY;
    }

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public DynamicRouteLocator(String servletPath, ZuulProperties properties) {
        super(servletPath, properties);
        this.zuulProperties = properties;
    }

    @Override
    public void refresh() {
        doRefresh();
    }

    @Override
    protected Map<String, ZuulProperties.ZuulRoute> locateRoutes() {
        Map<String, ZuulProperties.ZuulRoute> routesMap = new LinkedHashMap(20);

        zuulProperties.getRoutes().clear();

        routesMap.putAll(super.locateRoutes());

        routesMap.putAll(locateRoutesFromRedis());

        zuulProperties.getRoutes().putAll(routesMap);

        return routesMap;
    }

    private Map<? extends String, ? extends ZuulProperties.ZuulRoute> locateRoutesFromRedis() {
        LinkedHashMap routesMap = new LinkedHashMap();

        String routeData = stringRedisTemplate.opsForValue().get(ZUUL_EKY);
        List<ZuulEntity> zuulEntityList = JSONObject.parseObject(routeData, new TypeReference<List<ZuulEntity>>(){});

        for (ZuulEntity zuulEntity: zuulEntityList){
            routesMap.put(zuulEntity.getPath(),new ZuulProperties.ZuulRoute(
                    zuulEntity.getId(),
                    zuulEntity.getPath(),
                    zuulEntity.getServiceId(),
                    zuulEntity.getUrl(),
                    zuulEntity.isStripPrefix(),
                    zuulEntity.getRetryable(),
                    new LinkedHashSet(1)));
        }
        return routesMap;
    }

    @Override
    public Collection<String> getIgnoredPaths() {
        return super.getIgnoredPaths();
    }

    @Override
    public List<Route> getRoutes() {
        return super.getRoutes();
    }

    @Override
    public Route getMatchingRoute(String path) {
        return super.getMatchingRoute(path);
    }
}

刷新路由

直接转发

基于eureka的路由

具体代码可以看这个:

https://github.com/beckbikang/spring-cloud/tree/main/kzuul

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容