跳至主要內容

Spring Cloud 整合Gateway网关

悟空约 604 字大约 2 分钟...

Spring Cloud 整合Gateway网关

1.Gateway网关介绍

  • 网关:流量的入口
  • 网关常用功能:路由转发,权限校验,限流控制
  • Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架
  • Spring Cloud Gateway取代了netflix的Zuul网关

2.Gateway原理

PassJava项目中,小程序和管理后台请求先访问到API网关.

API网关通过注册中心实时感知微服务的状态的路由地址,准确地将请求路由到各个服务.

Spring Cloud Gateway
Spring Cloud Gateway

官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.2.RELEASE/reference/html/open in new window

Gateway原理
Gateway原理
  • 请求到达网关后,先经过断言Predicate,是否符合某个路由规则

  • 如果符合,则按路由规则路由到指定地址

  • 请求和响应都可以通过过滤器Filter进行过滤

3.创建Gateway 模块

  • 适用Spring 初始化器创建Gateway module
Spring 初始化器
Spring 初始化器
  • 创建module
Gateway module
Gateway module
  • 选择Gateway依赖
选择Gateway依赖
选择Gateway依赖
  • 引入Gateway模块
<module>passjava-gateway</module>

4.配置Gateway

  • 引入Nacos组件

因common模块引入了nacos注册中心组件,所以我们可以直接引用common模块

<dependency>
	<groupId>com.jackson0714.passjava</groupId>
	<artifactId>passjava-common</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>
  • 应用类上添加注解@EnableDiscoveryClient
@RefreshScope
@EnableDiscoveryClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class PassjavaGatewayApplication {
	public static void main(String[] args) {
		SpringApplication.run(PassjavaGatewayApplication.class, args);
	}
}

5.使用Gateway demo

  • 新建application.yml文件

    spring:
      cloud:
        gateway:
          routes:
            - id: route_qq
              uri: http://www.qq.com
              predicates:
                - Query=url,qq
            - id: route_baidu
              uri: http://www.baidu.com
              predicates:
                - Query=url,baidu
    

    第一条路由规则:当请求路径中包含url=qq,则跳转到http://www.qq.comopen in new window

    第二条路由规则:当请求路径中包含url=baidu,则跳转到http://www.baidu.comopen in new window

遇到的问题

which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.

 <dependencies>
   <dependency>
     <groupId>com.test</groupId>
     <artifactId>test-framework-common</artifactId>
     <version>0.0.1-SNAPSHOT</version>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-webflux</artifactId>
     <version>${spring-boot.version}</version>
   </dependency>
   <-- httpClient依赖,缺少此依赖api网关转发请求时可能发生503错误 -->
     <dependency>
       <groupId>org.apache.httpcomponents</groupId>
       <artifactId>httpclient</artifactId>
       <version>4.5.5</version>
     </dependency>

     <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
       <exclusions>
         <exclusion>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
         </exclusion>
       </exclusions>
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-webmvc</artifactId>
       <version>5.2.7.RELEASE</version>
     </dependency>
     <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </dependency>
     <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-gateway</artifactId>
       <exclusions>
         <exclusion>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
         </exclusion>
         <exclusion>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-webflux</artifactId>
         </exclusion>
       </exclusions>
     </dependency>
   </dependencies>
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.3.0