springboot集成Gateway「初试牛刀」

856 阅读1分钟

Gateway相当于所有服务的门户,将客户端请求与服务端应用相分离,客户端请求通过gateway后由定义的路由和断言进行转发,路由代表需要转发请求的地址,断言相当于请求这些地址时所满足的条件,只有同时符合路由和断言才给予转发。

一、Nginx网关转发

以前用Nginx做网关,来实现请求转发。

http {
    server {
            server_name example.com;

            location /mail/ {
                    proxy_pass http://example.com:protmail/;
            }

            location /com/ {
                    proxy_pass http://example.com:portcom/main/;
            }

            location / {
                    proxy_pass http://example.com:portdefault;
            }
    }

以上便是以前通过Nginx做网关,转发请求的简单方式。

二、微服务Gateway网关

开发环境

  • Spring Boot:2.1.12.RELEASE
  • Spring Cloud:Greenwich.SR5
  • Spring Cloud Alibaba:2.1.2.RELEASE

1. 引入POM依赖

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR5</spring-cloud.version>
        <spring-cloud-alibaba.version>2.1.2.RELEASE</spring-cloud-alibaba.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>

2. application.yml 配置

server:
  port: 9001
  servlet:
    context-path: /sgs
spring:
  application:
    name: springboot-gateway-server
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: test_route
          uri: http://www.zhihu.com
          predicates:
            - Query=name

转发规则:请求含有name参数,转发至 www.zhihu.com

发布 springboot-gateway-server 工程,访问 http://localhost:9001/?name=123

还有其它匹配方式转发,可参考:Spring Cloud GateWay 路由转发规则介绍详解

三、可能遇到的问题

Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found.

问题:spring-cloud-starter-gateway 与 spring-boot-starter-web 依赖冲突 原因:gateway构建与Spring 5+,基于Spring boot 2.x响应式的、非阻塞式的API,同时,他支持webSockets和spring框架紧密集成,启动时默认使用了spring-boot-starter-web的内置容器,不支持非阻塞。

解决方案:gateway服务端,去掉 spring-boot-starter-web 依赖。

其它方案:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- Maven整个生命周期内排除内置容器,排除内置容器导出成war包可以让外部容器运行spring-boot项目-->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

springboot集成Gateway DEMO: gitee.com/renxiaoshi/…