跳至主要內容

SpringCloud 整合 Gateway API 网关

悟空项目实战passjava面试刷题项目约 466 字大约 2 分钟...

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

后续在PassJava项目中使用Gateway的强大功能.

代码地址

https://github.com/Jackson0714/PassJava-Platformopen in new window

公众号

公众号
公众号
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.3.0