查看原文
其他

SpringCloud Alibaba微服务实战十 - 服务网关

单一色调 JAVA日知录 2022-09-06

在SpringCloud体系架构中,我们需要部署一个单独的网关服务对外提供访问入口,然后网关服务根据配置好的规则将请求转发至具体的后端服务,本章内容主要是给我们的微服务加上网关SpringCloud Gateway。

前言背景

我们有了三个服务account-serviceproduct-serviceorder-service。现在有客户端WEB应用APP应用需要访问后端服务获取数据那么就需要在客户端维护好三个服务的访问路径。

这样的架构会有如下几个典型的问题:

  • 每个微服务都需要配置单独的访问域名,开通外网访问权限,每新增一个服务都需要先让运维人员配置好域名映射
  • 客户端需要维护所有微服务的访问地址,试想一下如果微服务有几十几百个呢?
  • 当服务需要对接口进行权限控制,必须要认证用户才能调用,那么所有的权限逻辑在服务端都要重新编写一套。
  • 。。。

所以我们需要在微服务之前加一个网关服务,让所有的客户端只要访问网关,网关负责对请求进行转发;将权限校验逻辑放到网关的过滤器中,后端服务不需要再关注权限校验的代码;只需要对外提供一个可供外网访问的域名地址,新增服务后也不需要再让运维人员进行网络配置了,这样上面的架构就变成了如下所示:

创建网关模块

在项目中建立cloud-gateway模块, spring-cloud-gateway 作为微服务体系中的一环也需要将自身注册进Nacos并集成Nacos配置中心。

  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<artifactId>cloud-aliaba</artifactId>
<groupId>com.jianzh5.cloud</groupId>
<version>1.0.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>cloud-gateway</artifactId>

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

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

</dependencies>

</project>
  • 启动类
@SpringBootApplication
@EnableDiscoveryClient
public class GateWayApplication {
public static void main(String[] args) {
SpringApplication.run(GateWayApplication.class, args);
}
}
  • bootstap.yml
spring:
application:
name: cloud-gateway
cloud:
nacos:
config:
server-addr: 10.0.10.48:8848
file-extension: yml
namespace: 7e8ccc22-6f51-42fa-bcf1-db45f99dbf57
  • 在nacos中建立网关的路由配置
server:
port: 8090
spring:
cloud:
nacos:
discovery:
server-addr: 10.0.10.48:8848
gateway:
discovery:
locator:
enabled: true
routes:
- id: product-service
uri: lb://product-service
predicates:
- Path=/product/**
- id: account-service
uri: lb://account-service
predicates:
- Path=/account/**
- id: order-service
uri: lb://order-service
predicates:
- Path=/order/**

配置详解:

id: 在所有路由定义中需要唯一,不能重复
uri: lb://**  lb://为固定写法,表示开启负载均衡; ** 即服务在Nacos中注册的名字
predicates:- Path=/product/** 使用"Path Route Predicate Factory",规则为/product/** 的请求都还转发至微服务product-service中。

上面的配置逻辑为:
① 以http://localhost:8090/product/** 的访问路径会转发到product-service微服务的/**
② 以http://localhost:8090/account/** 的访问路径会转发到account-service微服务的/**
③ 以http://localhost:8090/order/** 的访问路径会转发到order-service微服务的/**

  • 启动所有服务,确认是否转发正如日志所示,SpringCloud Gateway 负责后端转发并开启了负载均衡。

好了,各位朋友们,本期的内容到此就全部结束啦,能看到这里的同学都是优秀的同学,下一个升职加薪的就是你了! 
如果觉得这篇文章对你有所帮助的话请扫描下面二维码加个关注。
 "转发""在看",养成好习惯!咱们下期再见!

 

系列文章

☞ SpringCloud Alibaba微服务实战九 - Seata容器化
☞ SpringCloud Alibaba微服务实战八 - Seata整合Nacos
☞ SpringCloud Alibaba微服务实战七 - 分布式事务
☞ SpringCloud Alibaba微服务实战六 - 配置隔离
☞ SpringCloud Alibaba微服务实战五 - 限流熔断
☞ SpringCloud Alibaba微服务实战四 - 版本管理
☞ SpringCloud Alibaba微服务实战三 - 服务调用
☞ SpringCloud Alibaba微服务实战二 - 服务注册
☞ SpringCloud Alibaba微服务实战一 - 环境准备



您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存