GuLi商城-SpringCloud-Gateway网关核心概念、测试API网关

╰半夏微凉° 2024-03-25 23:30 193阅读 0赞

网关作为流量的入口,功能包括路由转发、权限校验、限流控制等

  • Route(路由):包含独一无二的路由id、目的地的URL、断言集合、过滤器集合,断言用来判断
  • 路由是否能到达目的地URL
  • Predicats(断言):用来判断路由是否能到达目的地URL
  • Filter(过滤器):在请求访问成功之前、响应成功之后,都能进行修改

1、注册“gulimall-gateway”到Nacos

1)创建gulimall-gateway微服务

4c42513d591b4397b484cec3afcd0eed.png

b8eac76527cc4cb898d5f6a0e72f8fc8.png

387171212f6a4ff0b602abfc754304b8.png

引入common依赖,common里面有

b6528d56c7834b559d7ad715a8ccf0fb.png

b6743589aacf4d069cadfab2b261583c.png

2)添加“gulimall-common”依赖和“spring-cloud-starter-gateway”依赖,上图所示

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-gateway</artifactId>
  4. </dependency>

3)GulimallGatewayApplication类上加上@EnableDiscoveryClient注解

38b27497725c48b1a96105d6da60844e.png

4)在Nacos中创建“gateway”命名空间,同时在该命名空间中创建gulimall-gateway.yml”

我这里没有这么做,我还是用dev作为命名空间,在dev下创建gulimall-gateway

9a8780b38f4f4d9183973cdce750ecc3.png

46a451a3a85b42bfaf85ad4da543847d.png

5)创建“bootstrap.properties”文件,添加如下配置,指明配置中心地址和所属命名空间

5ae1a33bdde34041a8959bf98ba68dfa.png

6)创建“application.properties”文件,指定服务名和注册中心地址

  1. spring.application.name=gulimall-gateway
  2. spring.cloud.nacos.discovery.server-addr=192.168.137.14:8848
  3. server.port=88

f35454f2ec664ed6a20fdf4e9e91e79b.png

7)启动“gulimall-gateway”

启动报错:

94eccad2ea8e4158bbe1bd7e7ad4cdb2.png

  1. Description:
  2. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
  3. Reason: Failed to determine a suitable driver class

因为我们在common模块里配置了mybatis相关的依赖,所以默认spring会扫描我们的数据库配置,

为了防止找不到数据库配置报错,我们要排除掉数据库配置

解决方法:在GulimallGatewayApplication中排除和数据源相关的配置

  1. @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

cca04ddd0aaa4f509d346310908c07e1.png

重新启动网关微服务:

访问Nacos,查看到该服务已经注册到了Nacos中

114ac4c14e334b7fa649bb12a98e8ca4.png


过程中出现的问题记录:

fdb3e7d8b68643fbb90c082860a0a0bd.png

解决办法:增加依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-loadbalancer</artifactId>
  4. </dependency>

7ed8ee50b03f46dabccdc960cd3e6754.png


发现网关服务注册到nacos,是在public的命名空间中,不是我们希望的dev中

114ac4c14e334b7fa649bb12a98e8ca4.png

解决办法:

b893db68e406410784bc8b4473eb7a50.png

ed5681f90fa54f4ea216427d2f9abd3a.png


2、案例,测试网关的路由功能:

配置路由规则

6f93301d225b480e8c0b6f4c5d31b72a.png

修改层级:

c9f58fc0813d4ef69a85073c01b9d1ce.png

测试:

http://localhost:88/?uri=baidu

9bdaf8ed0b554b078c265b132b047f85.png

http://localhost:88/?uri=qq

9151daabcb264ce29f5627d3743c78b5.png

参考:

gulimall-learning/谷粒商城—分布式基础.md at master · OYCodeSite/gulimall-learning · GitHub

spring-cloud-gateway官网:https://spring.io/projects/spring-cloud-gateway

spring-cloud-gateway文档:https://docs.spring.io/spring-cloud-

gateway/docs/current/reference/html/

spring-cloud 中文网:https://www.springcloud.cc/

原文链接:https://blog.csdn.net/qq\_51998352/article/details/121916920

https://blog.csdn.net/qq_52476654/category_11866407.html

发表评论

表情:
评论列表 (有 0 条评论,193人围观)

还没有评论,来说两句吧...

相关阅读

    相关 使用 API

    简介 我们假设您正在为一个购物应用开发一个原生移动客户端。您可能需要实现一个产品详细信息页面,用于展示给定商品的信息。 例如,图 2-1 展示了在 Amazon 的 A

    相关 API

    API网关       api gateway 即 api 网关。所有的请求首先会经过这个网关。这有点类似于前端控制器模式,也有点类似于 Facade模式。

    相关 API

    一 网关基本概念 1 API网关介绍 API 网关出现的原因是微服务架构的出现,不同的微服务一般会有不同的网络地址,而外部客户端可能需要调用多个服务的接口才能完成一