踩坑Gateway服务搭建、配置网关路由、路径重写和解决跨域问题(java类实现跨域)
由于项目需要,需要使用Gateway,话不多说直接干
引入依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
踩坑开始,开始是继承父工程,工程里存在spring-boot-starter-web依赖
报错:
Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.
由于springcloudgateway的内部是通过netty+webflux实现的,webflux实现和springmvc配置依赖冲突。
需要 去除依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></exclusion></exclusions>
</dependency>
网上大部分网友给的解决方案是如下操作,但是我的工程显示缺少spring-boot-starter-webflux依赖,所以采用了自己的方式 只祛除>spring-boot-starter-web即可,实际情况实际分析,也有的不需要祛除依赖直接引入即可
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></exclusion><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId></exclusion></exclusions></dependency>
由于我的工程是使用的nacos集群,所以依赖里引入nacos注册发现和配置管理的依赖
完整的pom文件
<?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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>shuomall-gateway</artifactId><version>0.0.1-SNAPSHOT</version><name>shuomall-gateway</name><description>shuomall-gateway</description><properties><java.version>1.8</java.version><spring-cloud.version>Hoxton.SR5</spring-cloud.version></properties><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>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>--></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.0.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>--></project>
application.properties 配置nacos
spring.cloud.nacos.server-addr=127.0.0.1:8848
spring.application.name=shuomall-gateway
server.port=88
启动类开启注册发现 把shuomall-gateway服务注册到nacos
/*** 1.开启服务注册发现*/
@EnableDiscoveryClient
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class ShuomallGatewayApplication {public static void main(String[] args) {SpringApplication.run(ShuomallGatewayApplication.class, args);}}
application.yml 配置 实现路径重写
spring:cloud:gateway:routes:- id: admin_routeuri: lb://renren-fastpredicates:- Path=/api/**filters:- RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}
- id:名字自己定义
uri: lb://renren-fast //lb负载均衡 renrenfast 指定负载均衡的服务名称
predicates:
- Path=/api/** //按照路径进行断言,可以再前端项目发送请求时进行设置,添加api路径
filters:
- RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment} //路径重写 满足api路径请求,重写成自定义的路径,根据自己的需求进行定义
至此路由的路径重写已经实现,但是访问前端页面的时候发现有跨域问题
创建java类 ShuomallCorsConfiguration 解决跨域问题
package com.ws.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;@Configuration
public class ShuomallCorsConfiguration {@Beanpublic CorsWebFilter corsWebFilter(){UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();CorsConfiguration corsConfiguration = new CorsConfiguration();//配置跨域//头跨域corsConfiguration.addAllowedHeader("*");//请求方式跨域corsConfiguration.addAllowedMethod("*");//来源跨域corsConfiguration.addAllowedOrigin("*");corsConfiguration.setAllowCredentials(true);source.registerCorsConfiguration("/**",corsConfiguration);return new CorsWebFilter(source);}}
可能存在的问题 包含多个值,说明在解决跨域的问题问题上可能有两个类,需要把另一个注释掉就可以了
如果配置文件是yml格式的注意缩进
java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'
2022-03-12 09:58:11.471 ERROR 4732 --- [ main] o.s.boot.SpringApplication : Application run failedjava.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:545) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadForFileExtension(ConfigFileApplicationListener.java:494) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load
注意多路径重写,最详细的路径优先否则报错
{"msg":"invalid token","code":401}
需要把明确路径重写要优先于模糊的路径重写
spring:cloud:gateway:routes:- id: product_routeuri: lb://shuomall-productpredicates:- Path=/api/product/**filters:- RewritePath=/api/(?<segment>.*),/$\{segment}- id: admin_routeuri: lb://renren-fastpredicates:- Path=/api/**filters:- RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}
# - id: product_route
# uri: lb://shuomall-product
# predicates:
# - Path=/api/product/**
# filters:
# - RewritePath=/api/(?<segment>.*),/$\{segment}
踩坑Gateway服务搭建、配置网关路由、路径重写和解决跨域问题(java类实现跨域)相关推荐
- nacos动态路由配置(二)-微服务启动自动配置网关路由
经过上一篇我们发现nacos通过配置动态路由routes-api-gateway.yaml配置json,监听动态路由变化即可实现动态路由,非常的银杏化. 那么有的小伙伴发现配置json也比较麻烦,有没 ...
- MyCat踩坑记(Mycat搭建,使用及配置)
1.介绍 MyCat是目前最流行的基于Java语言编写的数据库中间件,是一个实现了MySql协议的服务器,其核心功能是分库分表.配合数据库的主从模式还可以实现读写分离 2.下载安装 下载地址:http ...
- 踩坑 微信小程序开发mpvue使用iconfont,顺便解决偶现图标显示不正确
刚刚接触iconfont, 发现它真是个好东西. 使用字体图标的好处: 改颜色,改大小 都可以随时所欲,写个多态按钮分分钟搞定,爽的不要太过分! 阿里的字体图库 https://www.iconfon ...
- 微服务框架搭建(网关路由)
文章目录 前言 1.创建gateway工程 2.引入pom文件 3.注册进入nacos 4.添加路由规则 5.过滤器的使用 github地址 前言 当使用微服务的时候,需要区分哪些接口对外提供服务有些 ...
- EMQ踩坑之路-搭建/测试/nginx配置websocket的ws及wss/web页面测试连通性、微信小程序使用wss--MQTT推送
最近公司要做推送平台,我接手做这个任务.在使用过程中遇到一些坑,这里分享给大家. 一.MQTT协议介绍 推送平台一般是基于轻量级的mqtt协议搭建的.mqtt协议是物联网领域常用的,是实现长链接的一种 ...
- Vue路由history模式踩坑记录:nginx配置解决404问题
问题背景: vue-router 默认是hash模式,使用url的hash来模拟一个完整的url,当url改变的时候,页面不会重新加载.但是如果我们不想hash这种以#号结尾的路径时候的话,我们可以使 ...
- 网关gateway服务端配置 1
1:配置中心 config-center-3344 <dependency><groupId>org.springframework.cloud</groupId> ...
- linux中openssh服务搭建,配置OPenSSH服务器
一.安装机配置OPenSSH服务器 1.安装与启动OpenSSH 首先查询系统是否安装了与OpenSSH相关的软件包: #rpm -qa| grep openssh 安装完成后,可以使用下述命令启动: ...
- SLAM踩坑记录 | 编译框架 | 配置环境等
本文主要是记录个人在学习SLAM过程中遇到的坑及解决办法,仅供参考 31.VS Code 下python中无法加载在其他文件下自定义的包 [解决] 在python的安装路径下的site-package ...
最新文章
- linux awk(good)
- PT60报错(在表 $ 中午关键字$的输入项目T555Z)
- Linux内核模块的概念和基本的编程方法
- 以计算机为话题写作文,以我的发现为话题作文(通用3篇)
- js面向对象与PHP面向对象总结
- 1095 解码PAT准考证 (25 分)
- Linux基础之bash脚本编程初级-变量与算术运算
- ORB_SLAM2+ZED 2
- 佳能hdr_佳能发布Cinema EOS系统首款RF卡口 4K数字电影摄影机EOS C70
- Spring AOP高级——源码实现(2)Spring AOP中通知器(Advisor)与切面(Aspect)
- 玩客云刷Armbian5.9.0安装青龙提示“面版解决服务异常,请手动执行ql check检查服务状态”
- idea vscode快捷键
- 微信卡包系列-核销微信卡券优惠券
- CentOS7安装oh-my-zsh(github start Top 10)
- 单片机篮球记分牌c语言程序和实训报告,单片机实现篮球记分牌的设计
- python九九乘法表如何对齐_python怎么样输出九九乘法表
- 【Java并发编程】并发编程大合集
- There is a problem with this Windows Installer package 卸载软件提示
- ZBar源码分析(二)
- i.MX8QM环境搭建
热门文章
- C# Winform软件多语言(汉语、英语。。。)界面的切换,低耦合 - 转
- [Angular] 使用 ng-alain
- Centos 7安装Squid代理服务及构建传统代理
- 使用pydub的坑-----Permission denied: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\tmpmt80te3g.wav'
- 解决 linux du: Argument list too long 参数列表过长的办法
- 关于智能共享出行,政界、学界和业界的专家都说了什么? | SMC 2018
- IDC排名(牛不会回来)
- 【2020秋招】提前批陌陌机器学习算法工程师面试经验
- 奥鹏福建师范大学2021年2月考试《计算机应用基础》作业56754
- Blink/Flink作业 性能优化配置及原理