springcloudgateway映射失效的解决⽅案
⽹关
解决的问题:
1. ⾃动路由,反向代理,负载均衡鉴权、监控
2. 使⽤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: baidu # ⽬的地址
predicates:
- Query=url, baidu # 断⾔匹配,满⾜就跳转到⽬的地址
-
id: qq_test
uri: qq
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.fig.GatewayAutoConfiguration$NettyConfiguration] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@7b1d7fff] at org.springframework.DeclaredMethods(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.UniqueDeclaredMethods(ReflectionUtils.java:447) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at java.base/urrent.ConcurrentHashMapputeIfAbsent(ConcurrentHashMap.java:170
5) ~[na:na]
at org.springframework.beans.factory.TypeForFactoryMethod(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.BeanNamesForType(DefaultListableBeanFactory.java:477) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.BeansOfType(DefaultListableBeanFactory.java:598) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.BeansOfType(DefaultListableBeanFactory.java:590) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at t.BeansOfType(AbstractApplicationContext.java:1226) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.ExitCodeFromMappedException(SpringApplication.java:905) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.ExitCodeFromException(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/flect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/flect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.start.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.DeclaredMethods0(Native Method) ~[na:na]
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3167) ~[na:na]
at java.base/java.DeclaredMethods(Class.java:2310) ~[na:na]
at org.springframework.DeclaredMethods(ReflectionUtils.java:489) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
... 26 common frames omitted
Caused by: java.lang.ClassNotFoundException: reactorty.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]
spring frameworkat java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
... 30 common frames omitted
Process finished with exit code 0
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。