springcloud gateway 映射失效的解決方案
網關
解決的問題:
- 自動路由,反向代理,負載均衡鑒權、監控
- 使用spring cloud gateway
路由匹配predicate 並到達指定位置
請求過來通過handler mapping來判斷是否可以處理,能處理通過handler去處理,處理前經過一系列filter
gateway配置
- id代表某一條規則
- uri代表目的地
- predicate代表路由規則,解析請求頭、請求方法、請求時間、請求路徑等請求參數才可路由到uri的目的地
- filter代表過濾器,可在請求過程中額外添加參數
使用:
1、添加依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
2、添加路由配置
spring: cloud: gateway: routes: - id: query_route # 規則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官網查看對應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
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Spring整合Mybatis 掃描註解創建Bean報錯的解決方案
- 解決tk mapper 通用mapper的bug問題
- 關於springboot中nacos動態路由的配置
- 解決springboot啟動失敗的問題('hibernate.dialect' not set)
- 基於SpringBoot中activeMq的JmsTemplate的實例