如何搭建Gateway服务

  1. Gateway的简单介绍

    1. Spring Cloud Gateway是Spring Cloud的一个项目,该项目是基于Spring,Spring Boot和Project Reactor等响应式编程和事件流技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。
    2. Gateway网关可以是所有微服务的统一入口,具有请求路由、权限控制、限流的功能特性
      1. 权限控制:网关作为微服务入口,需要校验用户是否有请求资格,如果没有则进行拦截
      2. 路由和负载均衡:一切请求都必须先经过gateway,但网关不处理业务,而是根据某种规则,把请求转发到某个微服务,这个过程叫做路由。当然路由的目标服务有多个时,还需要做负载均衡
      3. 限流:当请求流量过高时,在网关中按照下游的微服务能够接受的速度来放行请求,避免服务压力过大
    3. 在Spring Cloud中网关的实现有gateway、zuul,但Zuul是基于Servlet的实现,属于阻塞式编程,而Spring Cloud Gateway则是基于Spring5中提供的WebFlux,属于响应式编程的实现,具备更好的性能
  2. 那么现在我们就开始简单的搭建gateway服务

    1. 首先创建gateway的maven项目,并在maven的pom文件中添加依赖
      1.

      XML 复制代码
              <!-- Nacos依赖 -->
              <dependency>
                  <groupId>com.alibaba.cloud</groupId>
                  <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
              </dependency>
      
              <!--网关-->
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-gateway</artifactId>
              </dependency>
    2. 编写gateway的spring boot的启动类
      1.

      java 复制代码
      package com.app.user;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      /**
       * spring boot启动类
       *
       * @author Administrator
       */
      @SpringBootApplication
      public class GatewayApplication {
          public static void main(String[] args) {
              SpringApplication.run(GatewayApplication.class, args);
          }
      }
    3. 创建application.yml文件,并编写基础的配置信息以及路由规则
      1.

      bash 复制代码
      server:
        port: 12080
      spring:
        application:
          #应用的名称
          name: gateway-service
        cloud:
          nacos:
            # Nacos Server 启动监听的ip地址和端口
            server-addr: 192.168.xxx.xxx:8848
            discovery:
              # nacos开启鉴权之后的用户名
              username: nacos
              # nacos开启鉴权之后的用户登录密码
              password: nacos
          gateway:
            # 网关的路由配置
            routes:
              # 路由id,自定义,只要唯一即可
              - id: nacos-feign-user-service
                # 路由的目标地址,lb就是负载均衡,后面跟服务名称
                uri: lb://nacos-feign-user-service
                # 路由断言,也就是判断请求是否符合路由规则的条件
                predicates:
                  # 这个是按照路径匹配,只要以/users/开头就符合要求
                  - Path=/users/**
  3. 启动gateway-service服务,访问http://127.0.0.1:12080/users/2时,符合/users/\*\*规则,然后请求转发到uri:http://nacos-feign-user-service/users/2

相关推荐
hanniuniu138 小时前
网络安全厂商F5推出AI Gateway,化解大模型应用风险
人工智能·web安全·gateway
stormsha11 小时前
Proxmox Mail Gateway安装指南:从零开始配置高效邮件过滤系统
服务器·网络·网络安全·gateway
背太阳的牧羊人19 小时前
backend 服务尝试连接 qdrant 容器,但失败了,返回 502 Bad Gateway 问题排查
docker·gateway·qdrant
曼彻斯特的海边2 天前
RequestRateLimiterGatewayFilterFactory
spring cloud·gateway·限流
·云扬·2 天前
【PmHub面试篇】Gateway全局过滤器统计接口调用耗时面试要点解析
面试·职场和发展·gateway
devil_mf2 天前
gateway 网关 路由新增 (已亲测)
gateway
wmd131643067122 天前
Gateway 搭建
gateway
KubeSphere 云原生4 天前
云原生周刊:探索 Gateway API v1.3.0
云原生·gateway
曼彻斯特的海边5 天前
spring-cloud-alibaba-sentinel-gateway
gateway·sentinel·springcloud
xujinwei_gingko6 天前
网关Gateway
微服务·gateway