springcloud gateway 映射失效的解決方案
網(wǎng)關(guān)
解決的問(wèn)題:
- 自動(dòng)路由,反向代理,負(fù)載均衡鑒權(quán)、監(jiān)控
- 使用spring cloud gateway
路由匹配predicate 并到達(dá)指定位置
請(qǐng)求過(guò)來(lái)通過(guò)handler mapping來(lái)判斷是否可以處理,能處理通過(guò)handler去處理,處理前經(jīng)過(guò)一系列filter
gateway配置
- id代表某一條規(guī)則
- uri代表目的地
- predicate代表路由規(guī)則,解析請(qǐng)求頭、請(qǐng)求方法、請(qǐng)求時(shí)間、請(qǐng)求路徑等請(qǐng)求參數(shù)才可路由到uri的目的地
- filter代表過(guò)濾器,可在請(qǐng)求過(guò)程中額外添加參數(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開(kāi)頭,而一般本地請(qǐng)求都是http 導(dǎo)致請(qǐng)求出錯(cuò)
springcloud的gateway報(bào)以下錯(cuò)誤
原因:
springcloud的版本和springboot的版本對(duì)應(yīng)不上,可能因?yàn)閟pringboot版本過(guò)低或者springcloud版本過(guò)高,去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與過(guò)濾器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)文章
SpringBoot實(shí)現(xiàn)EMQ設(shè)備的上下線告警
EMQX?的上下線系統(tǒng)消息通知功能在客戶端連接成功或者客戶端斷開(kāi)連接,需要實(shí)現(xiàn)設(shè)備的上下線狀態(tài)監(jiān)控,所以本文給大家介紹了如何通過(guò)SpringBoot實(shí)現(xiàn)EMQ設(shè)備的上下線告警,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-10-10Java Web端程序?qū)崿F(xiàn)文件下載的方法分享
這篇文章主要介紹了Java Web端程序?qū)崿F(xiàn)文件下載的方法分享,包括一個(gè)包含防盜鏈功能的專門(mén)針對(duì)圖片下載的程序代碼示例,需要的朋友可以參考下2016-05-05springboot單獨(dú)使用feign簡(jiǎn)化接口調(diào)用方式
這篇文章主要介紹了springboot單獨(dú)使用feign簡(jiǎn)化接口調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03springboot+thymeleaf 文件上傳功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了springboot+thymeleaf 文件上傳功能的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11form-data與x-www-form-urlencoded的區(qū)別以及知識(shí)延伸
這篇文章主要給大家介紹了關(guān)于form-data與x-www-form-urlencoded的區(qū)別以及知識(shí)延伸,form-data和x-www-form-urlencoded都是HTTP請(qǐng)求中用于傳輸表單數(shù)據(jù)的編碼格式,需要的朋友可以參考下2023-11-11Java連接MySQL數(shù)據(jù)庫(kù)并實(shí)現(xiàn)數(shù)據(jù)交互功能
在現(xiàn)代應(yīng)用中,數(shù)據(jù)庫(kù)是不可或缺的一部分,Java 作為一種廣泛使用的編程語(yǔ)言,提供了豐富的 API 來(lái)與各種數(shù)據(jù)庫(kù)進(jìn)行交互,本文將詳細(xì)介紹如何在 Java 中連接 MySQL 數(shù)據(jù)庫(kù),并實(shí)現(xiàn)基本的數(shù)據(jù)交互功能,需要的朋友可以參考下2024-10-10SpringBoot+Vue+Flowable模擬實(shí)現(xiàn)請(qǐng)假審批流程
這篇文章主要為大家詳細(xì)介紹了如何利用SpringBoot+Vue+Flowable模擬實(shí)現(xiàn)一個(gè)請(qǐng)假審批流程,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-08-08