spring-cloud-gateway(2)--->spring-cloud-gateway如何实现动态路由

1、什么是动态路由,也就是在网关服务不停机的状态下,动态的添加route或者删除route,这就是动态路由的定义,我们在前面实战zuul的时候也提到过这个问题,当时的实现动态路由的方式是,使用配置中心进行route的配置,然后在增删路由的时候,使用actuator进行手动刷新。 当然在 spring-cloud-gateway中也可以这样实现,但是 spring-cloud-gateway中还为我们提供了另一种方式,那就是直接使用actuator API,官网地址如下:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#actuator-api

 

2、使用 spring-cloud-gateway 的 actuator API来实现动态路由实战:

      2.1、在项目中引入spring-boot-starter-actuator的依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

       2.2、暴露端点:

management:
  endpoints:
    web:
      exposure:
        include: "*"

        2.3、使用 spring-cloud-gateway 的 actuator API来查看已经配置的路由信息,地址为 http://网关服务ip:网关服务地址/actuator/gateway/routes   可得出已经配置的路由列表:

[
    {
        "predicate": "Paths: [/gateway-server/**], match trailing slash: true",
        "metadata": {
            "jmx.port": "62460",
            "management.port": "6060"
        },
        "route_id": "ReactiveCompositeDiscoveryClient_GATEWAY-SERVER",
        "filters": [
            "[[RewritePath /gateway-server/(?<remaining>.*) = '/${remaining}'], order = 1]"
        ],
        "uri": "lb://GATEWAY-SERVER",
        "order": 0
    },
    {
        "predicate": "Paths: [/orderapi/**], match trailing slash: true",
        "route_id": "orderapi",
        "filters": [
            "[[StripPrefix parts = 1], order = 1]",
            "[[Hystrix name = 'fallbackcmd', fallback = forward:/orderapi/order/mock], order = 2]",
            "[org.springframework.cloud.gateway.filter.factory.RequestRateLimiterGatewayFilterFactory$$Lambda$878/488624810@2b12211e, order = 3]"
        ],
        "uri": "http://localhost:7070",
        "order": 0
    },
    {
        "predicate": "Paths: [/userapi/**], match trailing slash: true",
        "route_id": "userapi",
        "filters": [
            "[[StripPrefix parts = 1], order = 1]"
        ],
        "uri": "lb://user-service",
        "order": 0
    },
    {
        "predicate": "(Paths: [/customer/predicate/**], match trailing slash: true && com.wzy.gateway.AuthRoutePredicateFactory$1@27f952b1)",
        "route_id": "costomer_predicate",
        "filters": [
            "[[StripPrefix parts = 2], order = 1]"
        ],
        "uri": "http://www.baidu.com:80",
        "order": 0
    }
]

         2.4、使用 spring-cloud-gateway 的 actuator API来添加路由:

                 请求地址:http://localhost:6060/actuator/gateway/routes/api_route

                 请求方式:post   application/json

                 入参格式:

{
	"id": "first_route",
	"predicates": [{
		"name": "Path",
		"args": {
			"_genkey_0": "/api1"
		}
	}],
	"filters": [{
		"name": "StripPrefix",
		"args": {
			"_genkey_0": 1
		}
	}],
	"uri": "https://www.baidu.com",
	"order": 0
}

           添加完成后再次查看路由列表,如下:

[
    {
        "predicate": "Paths: [/gateway-server/**], match trailing slash: true",
        "metadata": {
            "jmx.port": "62818",
            "management.port": "6060"
        },
        "route_id": "ReactiveCompositeDiscoveryClient_GATEWAY-SERVER",
        "filters": [
            "[[RewritePath /gateway-server/(?<remaining>.*) = '/${remaining}'], order = 1]"
        ],
        "uri": "lb://GATEWAY-SERVER",
        "order": 0
    },
    {
        "predicate": "Paths: [/orderapi/**], match trailing slash: true",
        "route_id": "orderapi",
        "filters": [
            "[[StripPrefix parts = 1], order = 1]",
            "[[Hystrix name = 'fallbackcmd', fallback = forward:http://localhost:6060/fallback], order = 2]",
            "[org.springframework.cloud.gateway.filter.factory.RequestRateLimiterGatewayFilterFactory$$Lambda$878/1456947885@4c8a8fa0, order = 3]"
        ],
        "uri": "http://localhost:7070",
        "order": 0
    },
    {
        "predicate": "Paths: [/userapi/**], match trailing slash: true",
        "route_id": "userapi",
        "filters": [
            "[[StripPrefix parts = 1], order = 1]"
        ],
        "uri": "lb://user-service",
        "order": 0
    },
    {
        "predicate": "(Paths: [/customer/predicate/**], match trailing slash: true && com.wzy.gateway.AuthRoutePredicateFactory$1@2ce02e80)",
        "route_id": "costomer_predicate",
        "filters": [
            "[[StripPrefix parts = 2], order = 1]"
        ],
        "uri": "http://www.baidu.com:80",
        "order": 0
    },
    {
        "predicate": "Paths: [/api1], match trailing slash: true",
        "route_id": "api_route",
        "filters": [
            "[[StripPrefix parts = 1], order = 1]"
        ],
        "uri": "https://www.baidu.com:443",
        "order": 0
    }
]

               可见我们添加的路由api_route已经添加进去了,其他的删除的接口也都有。

 

3、spring-cloud-gateway 的 actuator API来实现动态route的扩展:

      上面我们讲解了使用 actuator API来实现动态路由的实战,接下来我们来针对这种方式进行扩展,为什么要扩展呢?原因就是actuator API来实现动态路的方式,在默认情况下是没有之持久化的,也就是说一旦重启网关服务的话,动态添加的路由就没有了,因此我们需要进行扩展。

       我们就实现使用redis来保存路由信息吧,实现方式如下,在spring-cloud-gateway中提供了一个接口 RouteDefinitionRepository 路由定义仓库,类图如下:

             

            默认值提供了存在在内存中的仓库。

        接下来我们来实现存储在redis中的实现类:

@Component
public class RedisRouteDefRepository implements RouteDefinitionRepository {

    public static final String GATEWAY_ROUTE_REDIS_KEY = "gateway_route_redis_key";
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Flux<RouteDefinition> getRouteDefinitions() {

        List<RouteDefinition> list = new ArrayList <>();
        redisTemplate.opsForHash().values(GATEWAY_ROUTE_REDIS_KEY).stream().forEach(route->{
            list.add(JSON.parseObject(route.toString(), RouteDefinition.class));
        });

        return Flux.fromIterable(list);
    }

    @Override
    public Mono<Void> save(Mono <RouteDefinition> route) {

       return route.flatMap(routeDefinition ->{
            redisTemplate.opsForHash().put(GATEWAY_ROUTE_REDIS_KEY, routeDefinition.getId(), JSON.toJSONString(routeDefinition));
            return Mono.empty();
        });
    }

    @Override
    public Mono <Void> delete(Mono <String> routeId) {

        return routeId.flatMap(id->{
            redisTemplate.opsForHash().delete(GATEWAY_ROUTE_REDIS_KEY, id);
            return Mono.empty();
        });
    }
}

         这样动态添加的路由信息就会被储存在redis中,而在配置文件中定义的路由信息是不会储存在redis中的:

              

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值