美文网首页Spring Cloud
使用路由网关统一访问接口

使用路由网关统一访问接口

作者: 撸帝 | 来源:发表于2019-01-16 06:33 被阅读201次

学习完整课程请移步 互联网 Java 全栈工程师

本节视频

什么是 Spring Cloud Gateway

Spring Cloud Gateway 是 Spring 官方基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,Spring Cloud Gateway 旨在为微服务架构提供一种简单而有效的统一的 API 路由管理方式。Spring Cloud Gateway 作为 Spring Cloud 生态系中的网关,目标是替代 Netflix ZUUL,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。

Spring Cloud Gateway 功能特征

  • 基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
  • 动态路由
  • Predicates 和 Filters 作用于特定路由
  • 集成 Hystrix 断路器
  • 集成 Spring Cloud DiscoveryClient
  • 易于编写的 Predicates 和 Filters
  • 限流
  • 路径重写

Spring Cloud Gateway 工程流程

客户端向 Spring Cloud Gateway 发出请求。然后在 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。

过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(pre)或之后(post)执行业务逻辑。

POM

<?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.funtl</groupId>
        <artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>hello-spring-cloud-gateway</artifactId>
    <packaging>jar</packaging>

    <name>hello-spring-cloud-gateway</name>
    <url>http://www.funtl.com</url>
    <inceptionYear>2018-Now</inceptionYear>

    <dependencies>
        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->

        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!-- Spring Cloud End -->

        <!-- Commons Begin -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <!-- Commons Begin -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.funtl.hello.spring.cloud.gateway.GatewayApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

主要增加了 org.springframework.cloud:spring-cloud-starter-gateway 依赖

特别注意

  • Spring Cloud Gateway 不使用 Web 作为服务器,而是 使用 WebFlux 作为服务器,Gateway 项目已经依赖了 starter-webflux,所以这里 千万不要依赖 starter-web
  • 由于过滤器等功能依然需要 Servlet 支持,故这里还需要依赖 javax.servlet:javax.servlet-api

Application

package com.funtl.hello.spring.cloud.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

application.yml

spring:
  application:
    # 应用名称
    name: spring-gateway
  cloud:
    # 使用 Naoos 作为服务注册发现
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    # 使用 Sentinel 作为熔断器
    sentinel:
      transport:
        port: 8721
        dashboard: localhost:8080
    # 路由网关配置
    gateway:
      # 设置与服务注册发现组件结合,这样可以采用服务名的路由策略
      discovery:
        locator:
          enabled: true
      # 配置路由规则
      routes:
        # 采用自定义路由 ID(有固定用法,不同的 id 有不同的功能,详见:https://cloud.spring.io/spring-cloud-gateway/2.0.x/single/spring-cloud-gateway.html#gateway-route-filters)
        - id: NACOS-CONSUMER
          # 采用 LoadBalanceClient 方式请求,以 lb:// 开头,后面的是注册在 Nacos 上的服务名
          uri: lb://nacos-consumer
          # Predicate 翻译过来是“谓词”的意思,必须,主要作用是匹配用户的请求,有很多种用法
          predicates:
            # Method 方法谓词,这里是匹配 GET 和 POST 请求
            - Method=GET,POST
        - id: NACOS-CONSUMER-FEIGN
          uri: lb://nacos-consumer-feign
          predicates:
            - Method=GET,POST

server:
  port: 9000

# 目前无效
feign:
  sentinel:
    enabled: true

# 目前无效
management:
  endpoints:
    web:
      exposure:
        include: "*"

# 配置日志级别,方别调试
logging:
  level:
    org.springframework.cloud.gateway: debug

注意:请仔细阅读注释

测试访问

依次运行 Nacos 服务、NacosProviderApplicationNacosConsumerApplicationNacosConsumerFeignApplicationGatewayApplication

打开浏览器访问:http://localhost:9000/nacos-consumer/echo/app/name 浏览器显示

Hello Nacos Discovery nacos-consumer i am from port 8082

打开浏览器访问:http://localhost:9000/nacos-consumer-feign/echo/hi 浏览器显示

Hello Nacos Discovery Hi Feign i am from port 8082

注意:请求方式是 http://路由网关IP:路由网关Port/服务名/**

至此说明 Spring Cloud Gateway 的路由功能配置成功

相关文章

  • 使用路由网关统一访问接口

    学习完整课程请移步 互联网 Java 全栈工程师 本节视频 【视频】Spring Cloud Alibaba-Sp...

  • 创建路由网关统一访问接口

    学习完整课程请移步 互联网 Java 全栈工程师 本节视频 【视频】Spring Cloud Alibaba-My...

  • Linux网络管理:net-tools VS iproute2

    查看网卡及IP 激活和停止网络接口 设置IP地址 查看网关路由 设置网关(默认路由)

  • springcloud-gateway 网关异常处理

    在使用springcloud-gateway网关路由到微服时出现异常如何对异常进行统一封装处理? 问题:项目中使用...

  • cisco Switching-三层交换配置HSRP

    问题 以三层交换机代替路由器作为网关设备。 方案 以三层交换机代替路由器作为网关设备,以vlan1作为网关接口 步...

  • 利用parrot进行SSLstrip

    介绍 我们的目标是尝试访问使用 https 网站的用户. 路由器(网关)会把受害者的访问请求发送给我们,而攻击者会...

  • Spring JDBC

    统一的数据访问异常体系 问题:DAO模式下,数据访问接口需要抛出异常,如果异常特定于某种实现,那么使用这个接口的代...

  • spring cloud gateway

    客户端访问服务器资源时, 通过网关访问服务器资源, 网关主要有过滤, 鉴权,日志记录, 路由,限流等的功能, 其中...

  • openwrt设置旁路由,

    禁用网卡,重新获取IP,检查网关是否为:10.10.10.252 打开油管,访问油管OK,则主路由旁路由设置成功,...

  • Zuul路由网关与过滤器

    技术需求点:1.介绍Zuul网关路由和过滤器两大功能的工作原理;2.模拟Zuul路由网关功能;3.使用Zuul网关...

网友评论

    本文标题:使用路由网关统一访问接口

    本文链接:https://www.haomeiwen.com/subject/ajlpdqtx.html