nacos(七): gateway(单体)
这篇文章将从gateway的搭建、自动路由匹配、路由数组、跨域和路由过滤器五个方面对gateway项目展开讨论。
1、gateway的搭建
gateway的项目基本的搭建过程与消费者的搭建过程基本一致,细节部分可参考《nacos(四): 创建第一个消费者Conumer(单体)》。
搭建完成后,在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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>gateway</artifactId>
<packaging>jar</packaging> <properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.7.6</spring-boot.version>
<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
</properties>
<dependencies> <!-- 服务发现 -->
<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-loadbalancer</artifactId>
<version>3.1.5</version>
</dependency> <!--⽹关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> <dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.gateway.GatewayApplication</mainClass>
<skip>false</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> </project>
注意:在gateway项目中,不能引入spring mvc或者start web类的关于web的依赖,否则会报错“Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway.”。
另外,gateway的版本也需要进行匹配,在前几篇文章中,我们也一再强调微服务项目对于库的版本要求非常精细。版本稍微不一致可能会报出奇怪的问题,比如”Error processing condition on org.springframework.cloud.gateway.config.GatewayAutoConfiguration.propertiesRouteDefinitionLocator“。
接下来,在application.yml中,开启自动路由匹配:
server:
port: 8087 spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
config:
server-addr: 127.0.0.1:8848
file-extension: properties
username: nacos
password: nacos
loadbalancer:
nacos:
enabled: true
gateway:
discovery:
locator:
enabled: true application:
name: gateway
最后,我们在启动类上添加@EnableDiscoveryClient注解。
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
配置完成后,启动gateway,可以看到nacos的管理后台里gateway已经注册成功。
2、路由自动匹配
在文章的第1部分里,application.yml中开启的路的自动匹配功能。
如个例子:
当前在nacos中有一个product1服务,服务提提供了/hello的地址。
通过gateway,我们可以访问http://127.0.0.1:8087/product1/hello,通过gateway访问到product1的hello地址。如下图:
其中:
127.0.0.1:8087:分别是gateway的IP地址和端口号;
product1: 是注册在nacos的里的服务名;
/hello:则是product1服务提供的资源地址。
3、路由数组
在application.yml中配置routes配置节,可开启路由数组,如下
server:
port: 8087 spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
config:
server-addr: 127.0.0.1:8848
file-extension: properties
username: nacos
password: nacos
loadbalancer:
nacos:
enabled: true
gateway:
discovery:
locator:
enabled: true
routes:
- id: product1_hello
uri: lb://product1 # 指的是从nacos中按照名称获取微服务,并遵循负载均衡策略
order: 1 # 路由优先级 数字越低优先级越高
predicates: # 断言
- Path= /p1/** # 当请求路径满⾜Path指定的规则时,才进⾏路由换发
filters:
- StripPrefix=1 # 拼接好url之后去掉1层路径也就是p1
- id: order_route
uri: lb://server-order
order: 1
predicates:
- Path=/order/**
filters:
- StripPrefix=1
- AddRequestHeader=msg,abc application:
name: gateway
上面的这个配置文件例子中,路由数组里配置了两个路由。各项配置的具体配置的意义,看文件中的注释。其中- StripPrefix=1配置项需要注意。
这一项的意思是,当用户访问http://127.0.0.1:8087/p1/hello,因为/p1/路径触发了第一个路由规则,路由去掉p1这一项,重新拼接为lb://product1/hello去获取访问的结果。
断言proeicates还有如果规则可以使用:
过滤器filters还有如下规则可以使用:
4、跨域
可以通过对application.yml的配置进行跨域项的设置,如下:
spring:
cloud:
gateway:
globalcors: # 全局的跨域处理
add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题
corsConfigurations:
'[/**]':
allowedOrigins: # 允许哪些⽹站的跨域请求
- "http://localhost:8090"
allowedMethods: # 允许的跨域ajax的请求⽅式
- "GET"
- "POST"
- "DELETE"
- "PUT"
- "OPTIONS"
allowedHeaders: "*" # 允许在请求中携带的头信息
allowCredentials: true # 是否允许携带cookie
maxAge: 360000 # 这次跨域检测的有效期
5、路由过滤器
gateway也可以自定义自己的filter,下面用两个例子来说明过如何自定义filter。
示例一:判断请求参数中是否有authorization, authorization参数值是否为admin。如果同时满⾜则放⾏,否则拦截。
@Component
@Order(-1) // 用来控制优先级 数字越小优先级越高
public class AuthorizeFilter implements GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String token = exchange.getRequest().getHeaders().get("authorization").get(0);
if (token.equals("admin")){
// 放行
return chain.filter(exchange);
}
// 拦截 禁止访问
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
}
示例二:判断请求参数中是否有token,如果没有则拦截并报错。
@Component
public class TokenFilter implements GlobalFilter, Ordered { @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest req= exchange.getRequest();
ServerHttpResponse resp= exchange.getResponse(); MultiValueMap<String, String> params= req.getQueryParams();
if(!params.containsKey("token")){
//输出错误信息
Map<String, Object> map = new HashMap<>();
map.put("msg", "Not Logged in!!!!");
map.put("code", 4000); /*
//3.3作JSON转换
byte[] bytes = JSON.toJSONString(map).getBytes(StandardCharsets.UTF_8
);
//3.4调用bufferFactory方法,生成DataBuffer对象
DataBuffer buffer = response.bufferFactory().wrap(bytes);
*/ //4.调用Mono中的just方法,返回要写给前端的JSON数据
DataBuffer buffer =resp.bufferFactory().wrap("error".getBytes(StandardCharsets.UTF_8)); return resp.writeWith(Mono.just(buffer));
}
return chain.filter(exchange);
} @Override
public int getOrder() {
return 0;
}
}
本文关于gateway的内容到这里就结束了,下一篇我们将一起讨论哨兵sentinel的使用: )
nacos(七): gateway(单体)的更多相关文章
- 学习一下 SpringCloud (六)-- 注册中心与配置中心 Nacos、网关 Gateway
(1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...
- 微服务时代之网关相关技术选型及部署(nacos+gateway)
1.场景描述 因要用到微服务,关于注册中心这块,与同事在技术原型上做了讨论,初步定的方案是使用:阿里巴巴的nacos+springcloud gateway,下面表格是同事整理的注册中心对比,以前用的 ...
- Spring Cloud GateWay基于nacos如何去做灰度发布
如果想直接查看修改部分请跳转 动手-点击跳转 本文基于 ReactiveLoadBalancerClientFilter使用RoundRobinLoadBalancer 灰度发布 灰度发布,又称为金丝 ...
- Spring Cloud Alibaba学习笔记(15) - 整合Spring Cloud Gateway
Spring Cloud Gateway 概述 Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于Netty.Reactor以及WEbFlux构建,它 ...
- 微服务实战系列(四)-注册中心springcloud alibaba nacos
1.场景描述 因要用到微服务,关于注册中心这块,与同事在技术原型上做了讨论,初步定的方案是使用:阿里巴巴的nacos+springcloud gateway,下面表格是同事整理的注册中心对比,以前用的 ...
- Linux部署Nacos
此处演示Nacos在Linux(CentOS7)环境中单机版部署,此处演示1.3.0版本. 一.官网下载压缩包 https://github.com/alibaba/nacos/releases 二. ...
- Gateway服务网关 (入门到使用)
Gateway服务网关 Gateway也要作为微服务注册到nacos中 Zuul也是网关但比较老是一种阻塞式编程:Spring Cloud Gateway 是 Spring Cloud 的一个全新项目 ...
- Gateway服务网关+过滤器
为什么需要网关 Gateway网关是我们服务的守门神,所有微服务的统一入口. 网关的核心功能特性: 请求路由 权限控制 限流 架构图: 权限控制:网关作为微服务入口,需要校验用户是是否有请求资格,如果 ...
- 微服务学习计划——SpringCloud
微服务学习计划--SpringCloud 在学习并掌握了众多基础框架之后,我们的项目繁杂且难以掌握,那么我们就需要开启一门新的课程,也就是我们常说的微服务架构 随着互联网行业的发展,对服务的要求也越来 ...
- JavaScript高级 引用类型(二)《JavaScript高级程序设计(第三版)》
五.Function类型 是JS中最重要的一种引用类型 构造方式:(三种) 函数声明: 函数表达式定义: 函数构造器: 没有重载: 如果有两个相同函数名的函数,执行时,执行最近被定义的一次. 函数声明 ...
随机推荐
- node-koa2 微信支付-企业付款到银行卡
微信支付用的V2版本 微信支付说明文档:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay_yhk.php?chapter=24_2 参数详细 ...
- pycharm选择conda虚拟环境出错:python的SDK无效
检查项如下: 0.安装了python,并在系统环境变量中配置了python 0.5 正确配置了conda的系统环境变量 1.安装conda的文件夹又读写权限(不需要管理员模式运行也能进行读写) 直接在 ...
- Linux安装EasyConnect
首先下载并安装EasyConnect客户端 wget http://download.sangfor.com.cn/download/product/sslvpn/pkg/linux_767/Easy ...
- 【sprinb-boot】@ComponentScan 跳过扫描 excludeFilters
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Contr ...
- 基于Three.js的大屏3D地图(一)
依赖安装 yarn add three yarn add @types/three yarn add d3-geo three库安装后在node_modules下其还包含核心three/src和插件t ...
- Qt/C++音视频开发52-采集本地屏幕桌面的终极设计
一.前言 最开始设计的时候,只考虑了一个屏幕的情况,这种当然是最理想的情况,实际上双屏或者多屏的用户也不在少数,比如我这两个屏幕,屏幕1是1080P,屏幕2是2K分辨率,打印两个屏幕的区域是 QRec ...
- Qt编写地图综合应用6-百度在线地图
一.前言 百度在线地图的应用老早就做过,后面经过不断的完善才到今天的这个程序,除了基本的可以载入地图并设置一些相关的属性以外,还增加了各种js函数直接异步加载数据比如动态添加点.矩形.圆形.行政区划等 ...
- Datawhale AI 夏令营-天池Better Synth多模态大模型数据合成挑战赛-task2探索与进阶(更新中)
在大数据.大模型时代,随着大模型发展,互联网数据渐尽且需大量处理标注,为新模型训练高效合成优质数据成为新兴问题."天池 Better Synth - 多模态大模型数据合成挑战赛"应 ...
- Solution -「CF 1366E2」Chiori and Doll Picking (hard version)
\(\mathscr{Description}\) Link. 给定 \(\{a_n\}\), 值域 \([0,2^m)\). 对于每个 \(i\in[0,m]\), 求有多少个 \(\{a_ ...
- Java中hashCode() 和 equals()
该文章为转载(原文链接在结尾),虽然篇幅偏长,但是却能使你真正理解hashCode和queals各自的作用以及之间的联系,尤其是第四部分,读完肯定会让你有所收获. 第1部分 equals() 的作用 ...