如何搭建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

相关推荐
研究司马懿3 天前
【云原生】Gateway API介绍
云原生·gateway
研究司马懿3 天前
【云原生】Gateway API路由、重定向、修饰符等关键操作
云原生·gateway
研究司马懿3 天前
【云原生】初识Gateway API
云原生·gateway
七夜zippoe4 天前
API网关设计模式实战 Spring Cloud Gateway路由过滤限流深度解析
java·设计模式·gateway·路由·api网关
汪碧康4 天前
一文讲解kubernetes的gateway Api的功能、架构、部署、管理及使用
云原生·容器·架构·kubernetes·gateway·kubelet·xkube
大佐不会说日语~4 天前
Docker Compose 部署 Spring Boot 应用 502 Bad Gateway 问题排查与解决
spring boot·docker·gateway·maven·故障排查
Dontla6 天前
Kubernetes流量管理双雄:Ingress与Gateway API解析(Nginx与Ingress与Gateway API的关系)
nginx·kubernetes·gateway
JavaLearnerZGQ6 天前
Gateway网关将登录用户信息传递给下游微服务(完整实现方案)
微服务·架构·gateway
Ares-Wang7 天前
网络》》BGP Border Gateway Protocol,边界网关协议
网络·gateway
一方_self8 天前
cloudflare AI gateway实战代理任意第三方大模型服务提供商
人工智能·gateway