第十五章 Spring Cloud GateWay 初识

Spring Cloud作为Spring Cloud官方推荐的第二代框架,取代Zuul网关。网关作为流量控制,在微服务架构当中有非常重要的作用。网关常用的功能有路由转发、权限校验和限流控制等。
本案例来源于官网,在官网的基础之上进行实现。

1、准备工作

本篇文章需要父项目(spring-cloud-brimen-gateway)和子工程(spring-cloud-gateway-first),关于父项目的创建过程以及相关依赖不再赘述,请查看 第二章 Spring Cloud 服务的注册与发现(Eureka Server)

1、创建一个简单的路由

第一步:创建一个普通的Spring Boot工程

创建一个普通的Spring Boot工程,并命名为:spring-cloud-gateway-first

第二步:引入相关依赖

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <groupId>com.brimen</groupId>
        <artifactId>spring-cloud-brimen-gateway</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/>
    </parent>
    <groupId>com.brimen</groupId>
    <artifactId>spring-cloud-gateway-first</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-gateway-first</name>
    <description>Spring Cloud GateWay 初体验</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

第三步:实现功能

Spring Cloud 使用RouteLocatorBuilder 进行路由转发,将请求进行处理,最后转发到下游目标服务当中去,本案例转发到
http://httpbin.org:80 ,在转发的过程当中,添加了Header,key为Hello,value为World。
RouteLocatorBuilder不仅能够进行路由转发,还可以添加各种predicatesfilters,predicates定义各种请求规则,满足规则的前提下,再去由router去处理。而filters用于过滤请求,可以添加各种判断,做各种拦截,例如权限校验。

package com.brimen.springcloudgatewayfirst;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringCloudGatewayFirstApplication {

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

    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(p -> p
                        .path("/get")
                        .filters(f -> f.addRequestHeader("Hello", "World"))
                        .uri("http://httpbin.org:80"))
                .build();
    }
}

第四步:启动项目,进行访问

在这里插入图片描述
从图中结果我们可以看到,当我们请求接口/get时,服务将请求转发到了 httpbin.org ,在转发之前,加了一个filter,filter添加了一个header,header的key为Hello,value为World。

2、使用Hystrix

本功能是以上面的工程为基础实现的。
Spring Cloud GateWay当中可以使用Hystrix,Hystrix是Spring Cloud当中熔断降级的一个组件,在微服务架构系统当中非常的重要,Hystrix是Spring Cloud当中以filter的形式使用的,代码如下:

package com.brimen.springcloudgatewayfirst;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@SpringBootApplication
@RestController
public class SpringCloudGatewayFirstApplication {

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

    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder) {
        String httpUri = "http://httpbin.org:80";
        return builder.routes()
                .route(p -> p
                        .path("/get")
                        .filters(f -> f.addRequestHeader("Hello", "World"))
                        .uri(httpUri))
                .route(p -> p
                        .host("*.hystrix.com")
                        .filters(f -> f
                                .hystrix(config -> config
                                        .setName("mycmd")
                                        .setFallbackUri("forward:/fallback")))
                        .uri(httpUri))
                .build();
    }

    @RequestMapping("/fallback")
    public Mono<String> fallback() {
        return Mono.just("fallback");
    }
}

在上面的代码当中,我们使用了另一个路由,该路由使用host断言是否进入该路由,当请求的host有“*.hystrix.com”时,都有进入该路由,该路由当中有一个filter,filter当中可以配置name和fallback的指向性地址,当满足条件,会调用/fallback方法。
使用curl命令,如下:

curl --dump-header - --header 'Host: www.hystrix.com' http://localhost:8080/delay/3

结果如下,会返回一个fallback的字符串,可见, www.hystrix.com执行了fallback的逻辑。
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值