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

請求過來通過handler mapping來判斷是否可以處理,能處理通過handler去處理,處理前經過一系列filter
gateway配置
- id代表某一條規(guī)則
- uri代表目的地
- predicate代表路由規(guī)則,解析請求頭、請求方法、請求時間、請求路徑等請求參數(shù)才可路由到uri的目的地
- filter代表過濾器,可在請求過程中額外添加參數(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 # 斷言匹配,滿足就跳轉到目的地址
- id: qq_test
uri: https://qq.com
predicates:
- Query=url, qq
3、具體路由參考官方文檔
踩坑:chrome 默認會把url失敗后轉為https開頭,而一般本地請求都是http 導致請求出錯
springcloud的gateway報以下錯誤
原因:
springcloud的版本和springboot的版本對應不上,可能因為springboot版本過低或者springcloud版本過高,去springcloud官網(wǎng)查看對應springboot版本重新依賴即可解決
報錯版本:
springcloud:Greenwich.SR5 springboot:2.1.4
調整后版本:
springcloud:Greenwich.SR5 springboot:2.1.8(如果springboot版本大于等于2.2.0還是會報錯)
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
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringCloud Gateway 路由配置定位原理分析
- springcloud gateway如何實現(xiàn)路由和負載均衡
- SpringCloud gateway如何修改返回數(shù)據(jù)
- 詳解SpringCloud新一代網(wǎng)關Gateway
- SpringCloud Gateway加載斷言predicates與過濾器filters的源碼分析
- 詳解SpringCloud Gateway 2020.0.2最新版
- SpringCloud Gateway 利用 Mysql 實現(xiàn)動態(tài)路由的方法
- SpringCloud Gateway使用redis實現(xiàn)動態(tài)路由的方法
相關文章
Spring+MyBatis實現(xiàn)數(shù)據(jù)庫讀寫分離方案
本文主要介紹了Spring+MyBatis實現(xiàn)數(shù)據(jù)庫讀寫分離方案。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01
spring中向一個單例bean中注入非單例bean的方法詳解
Spring是先將Bean對象實例化之后,再設置對象屬性,所以會先調用他的無參構造函數(shù)實例化,每個對象存在一個map中,當遇到依賴,就去map中調用對應的單例對象,這篇文章主要給大家介紹了關于spring中向一個單例bean中注入非單例bean的相關資料,需要的朋友可以參考下2021-07-07
Spring中BeanFactory?FactoryBean和ObjectFactory的三種的區(qū)別
關于FactoryBean?和?BeanFactory的對比文章比較多,但是對ObjectFactory的描述就比較少,今天我們對比下這三種的區(qū)別,感興趣的朋友跟隨小編一起看看吧2023-01-01
Java concurrency之鎖_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了Java concurrency之鎖的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06

