Spring Cloud项目(八)——使用gateway作为服务网关

项目结构

在这里插入图片描述

代码编写

1、pom文件

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>

2、yml文件

server:
  port: 8888

spring:
  application:
    name: nacos-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      routes:
        - id: abc
          uri: http://localhost:8081
          predicates:
            - Path=/consumer/**

3、启动类
在这里插入图片描述

4、父模块pom文件
在这里插入图片描述

之前是SR3,改成SR1。用SR3会报错,说是reactor-netty这个jar包不存在。。。

测试结果

依次启动:gateway、consumer、provider

在这里插入图片描述
在这里插入图片描述

Gateway简介

在这里插入图片描述

Spring Cloud Gateway 是 Spring 官方基于 Spring 5.0,WebFlux 等技术开发的Spring Cloud 生态系中的网关,目标是替代 Netflflix ZUUL,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:路由,过滤,和限流等。

Spring Cloud Gateway 的功能很强大,前面我们只是使用了 predicates 进行了简单的条件匹配,其实Spring Cloud Gataway 帮我们内置了很多 Predicates 功能。在 Spring Cloud Gateway 中 Spring 利用Predicate 的特性实现了各种路由匹配规则,有通过 Header、请求参数等不同的条件来进行作为条件匹配到对应的路由。

附:Spring Cloud学习系列

Spring Cloud项目(一)——集成Nacos作为注册中心
Spring Cloud项目(二)——集成Nacos作为配置中心
Spring Cloud项目(三)——实现Nacos数据信息持久化到MySQL
Spring Cloud项目(四)——使用Ribbon作为负载均衡
Spring Cloud项目(五)——使用openFeign作为服务调用
Spring Cloud项目(六)——使用sentinel作为流量管理
Spring Cloud项目(七)——使用sentinel作为限流和熔断
Spring Cloud项目(八)——使用gateway作为服务网关

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

--流星。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值