springcloud gateway 映射失效的解決方案
網(wǎng)關(guān)
解決的問題:
- 自動(dòng)路由,反向代理,負(fù)載均衡鑒權(quán)、監(jiān)控
- 使用spring cloud gateway
路由匹配predicate 并到達(dá)指定位置

請(qǐng)求過來通過handler mapping來判斷是否可以處理,能處理通過handler去處理,處理前經(jīng)過一系列filter
gateway配置
- id代表某一條規(guī)則
- uri代表目的地
- predicate代表路由規(guī)則,解析請(qǐng)求頭、請(qǐng)求方法、請(qǐng)求時(shí)間、請(qǐng)求路徑等請(qǐng)求參數(shù)才可路由到uri的目的地
- filter代表過濾器,可在請(qǐng)求過程中額外添加參數(shù)
使用:
1、添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
2、添加路由配置
spring:
cloud:
gateway:
routes:
- id: query_route # 規(guī)則id
uri: https://baidu.com # 目的地址
predicates:
- Query=url, baidu # 斷言匹配,滿足就跳轉(zhuǎn)到目的地址
- id: qq_test
uri: https://qq.com
predicates:
- Query=url, qq
3、具體路由參考官方文檔
踩坑:chrome 默認(rèn)會(huì)把url失敗后轉(zhuǎn)為https開頭,而一般本地請(qǐng)求都是http 導(dǎo)致請(qǐng)求出錯(cuò)
springcloud的gateway報(bào)以下錯(cuò)誤
原因:
springcloud的版本和springboot的版本對(duì)應(yīng)不上,可能因?yàn)閟pringboot版本過低或者springcloud版本過高,去springcloud官網(wǎng)查看對(duì)應(yīng)springboot版本重新依賴即可解決
報(bào)錯(cuò)版本:
springcloud:Greenwich.SR5 springboot:2.1.4
調(diào)整后版本:
springcloud:Greenwich.SR5 springboot:2.1.8(如果springboot版本大于等于2.2.0還是會(huì)報(bào)錯(cuò))
java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.gateway.config.GatewayAutoConfiguration$NettyConfiguration] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@7b1d7fff] at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:507) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:404) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:389) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:447) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE] at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1705) ~[na:na] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:738) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:679) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:647) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1518) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:507) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:477) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:598) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:590) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1226) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.boot.SpringApplication.getExitCodeFromMappedException(SpringApplication.java:905) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.springframework.boot.SpringApplication.getExitCodeFromException(SpringApplication.java:891) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.springframework.boot.SpringApplication.handleExitCode(SpringApplication.java:877) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:826) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE] at com.changgou.GatewayWebApplication.main(GatewayWebApplication.java:15) ~[classes/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.1.4.RELEASE.jar:2.1.4.RELEASE] Caused by: java.lang.NoClassDefFoundError: reactor/netty/http/client/HttpResponseDecoderSpec at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na] at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3167) ~[na:na] at java.base/java.lang.Class.getDeclaredMethods(Class.java:2310) ~[na:na] at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:489) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE] ... 26 common frames omitted Caused by: java.lang.ClassNotFoundException: reactor.netty.http.client.HttpResponseDecoderSpec at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) ~[na:na] at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na] at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na] ... 30 common frames omitted Process finished with exit code 0
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringCloud Gateway 路由配置定位原理分析
- springcloud gateway如何實(shí)現(xiàn)路由和負(fù)載均衡
- SpringCloud gateway如何修改返回?cái)?shù)據(jù)
- 詳解SpringCloud新一代網(wǎng)關(guān)Gateway
- SpringCloud Gateway加載斷言predicates與過濾器filters的源碼分析
- 詳解SpringCloud Gateway 2020.0.2最新版
- SpringCloud Gateway 利用 Mysql 實(shí)現(xiàn)動(dòng)態(tài)路由的方法
- SpringCloud Gateway使用redis實(shí)現(xiàn)動(dòng)態(tài)路由的方法
相關(guān)文章
Spring+MyBatis實(shí)現(xiàn)數(shù)據(jù)庫讀寫分離方案
本文主要介紹了Spring+MyBatis實(shí)現(xiàn)數(shù)據(jù)庫讀寫分離方案。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
mybatis 運(yùn)行時(shí)加載自定義mapper文件方式
這篇文章主要介紹了mybatis 運(yùn)行時(shí)加載自定義mapper文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
spring中向一個(gè)單例bean中注入非單例bean的方法詳解
Spring是先將Bean對(duì)象實(shí)例化之后,再設(shè)置對(duì)象屬性,所以會(huì)先調(diào)用他的無參構(gòu)造函數(shù)實(shí)例化,每個(gè)對(duì)象存在一個(gè)map中,當(dāng)遇到依賴,就去map中調(diào)用對(duì)應(yīng)的單例對(duì)象,這篇文章主要給大家介紹了關(guān)于spring中向一個(gè)單例bean中注入非單例bean的相關(guān)資料,需要的朋友可以參考下2021-07-07
Java中String字符串轉(zhuǎn)具體對(duì)象的幾種常用方式
String對(duì)象可以用來存儲(chǔ)任何字符串類型的數(shù)據(jù),包括HTML、XML等格式的字符串,下面這篇文章主要給大家介紹了關(guān)于JavaString字符串轉(zhuǎn)具體對(duì)象的幾種常用方式,需要的朋友可以參考下2024-03-03
Spring中BeanFactory?FactoryBean和ObjectFactory的三種的區(qū)別
關(guān)于FactoryBean?和?BeanFactory的對(duì)比文章比較多,但是對(duì)ObjectFactory的描述就比較少,今天我們對(duì)比下這三種的區(qū)別,感興趣的朋友跟隨小編一起看看吧2023-01-01
Java concurrency之鎖_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java concurrency之鎖的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

