SpringCloud整合Gateway冲突解决

在整合SpringCloud与Gateway时,发现Gateway的Webflux与SpringMVC产生冲突,异常如下:

**********************************************************

Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.

**********************************************************

java.lang.IllegalStateException: Failed to load ApplicationContext

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'routeDefinitionRouteLocator' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]: Unsatisfied dependency expressed through method 'routeDefinitionRouteLocator' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'modifyRequestBodyGatewayFilterFactory' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]: Unsatisfied dependency expressed through method 'modifyRequestBodyGatewayFilterFactory' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.http.codec.ServerCodecConfigurer' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:895)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:124)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    ... 24 more

 解决方案:

1. 将pom中或者处于引用的lib包中的spring-boot-starter-web移除掉
<dependency>
            <groupId>com.glink</groupId>
            <artifactId>midware</artifactId>
            <version>1.0.0-RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2. 将WebFlux底层Netty从Gateway依赖中移除即可

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-netty</artifactId>
        </exclusion>
    </exclusions>
</dependency>
### 回答1: Spring Cloud GatewaySpring Cloud生态系统中的一个全新项目,它基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术,旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud的网关,提供了一种简单而有效的方式来管理API请求的路由、过滤和转换。它可以与Eureka、Consul、Zookeeper等服务注册中心集成,支持动态路由、限流、熔断等功能,可以帮助我们快速构建高可用、高性能的微服务架构。 ### 回答2: Spring Cloud GatewaySpring Cloud 的一个全新项目,它基于 Spring 5.0,是一个独立的路由和过滤器网关,领域涵盖协议转换、流量整形、安全、监控/指标、日志等。它可以使用 Java 开发,也可以使用 Groovy 开发。它旨在为微服务架构提供一种简单而有效的方式,以实现路由、监控和安全等诸多目标功能。 Spring Cloud Gateway 的一个关键特性是能够动态路由,它可以根据业务实现和运行情况以及使用者的需求,对请求进行处理和转发。同时,Spring Cloud Gateway 不仅支持各种协议的路由和服务转发,还提供了强大的过滤器机制,可以在请求和响应阶段进行访问控制、操作转换等操作。 使用Spring Cloud Gateway整合Gateway可以快速构建高性能的微服务应用,并实现各种路由和过滤器功能。 首先安装 Spring Cloud Gateway 依赖,同时在pom.xml文件中添加如下代码: ``` <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> </dependencies> ``` 接下来,需要添加路由规则。Spring Cloud Gateway 支持两种方式添加路由规则,一种是采用配置文件方式,另外一种是采用编程方式。对于较为简单的场景,采用配置文件方式会更为便捷和方便。配置文件需要写在 application.properties(或者 application.yml)或自定义的配置文件中,在其中配置路由规则。可以通过在配置文件 application.yml 中,指定 routes 来定义路由: ``` spring: cloud: gateway: routes: - id: demo uri: https://example.com/ predicates: - Path=/demo ``` 上述配置的作用是当访问 http://localhost:8080/demo 时,转发到 https://example.com/。 需要注意的是,Spring Cloud Gateway 的路由规则是可以动态修改的,只需要将新增、修改、删除的路由规则写入相应的配置文件,无需重启服务即可生效。 最后,需要添加过滤器,Spring Cloud Gateway 提供了两种过滤器,一种是全局过滤器,另外一种是基于路由的过滤器。全局过滤器能够匹配所有路由,而基于路由的过滤器仅匹配特定的路由。在使用过滤器时,需要注意过滤器的执行顺序和配置顺序有关,过滤器的执行顺序范围为0-10000。 Spring Cloud Gateway 实现了网关的基本功能,可以轻松地实现反向代理、路由转发、日志监控等功能,为 Spring Cloud 应用带来了更高效的边缘服务体系。通过使用 Spring Cloud Gateway 整合 Gateway,可以更迅速地进行微服务架构的开发和部署。 ### 回答3: 随着微服务的广泛应用,微服务架构下的网关服务也逐渐变得重要起来。Spring Cloud Gateway作为一种轻量级、高效、易扩展的网关服务,已成为业界热门的选择。在使用Spring Cloud Gateway时,需要将其与Spring Cloud的其它组件进行整合,以达到更好的服务支持和扩展性。 一、整合方式 1.在pom.xml文件中加入以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> ``` 2.配置路由规则,可以在application.yml或application.properties文件中进行配置,在以下例子中,我们定义了一个简单的路由规则: ``` spring: cloud: gateway: routes: - id: route1 uri: http://example.org predicates: - Path=/foo/** - id: route2 uri: http://localhost:8080 predicates: - Path=/bar/** ``` 这里我们定义了两个路由规则,route1和route2,分别匹配以/foo和/bar开头的请求。其中uri属性指定了路由的目标地址,predicates属性指定了路由规则。 3.自定义过滤器,Spring Cloud Gateway提供了Filter接口,可以实现基于请求和响应的过滤器。以下是一个自定义的LoggingFilter,用来记录请求和响应的日志: ``` @Component public class LoggingFilter implements GlobalFilter, Ordered { private static final Logger log = LoggerFactory.getLogger(LoggingFilter.class); private static final String START_TIME = "startTime"; @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { exchange.getAttributes().put(START_TIME, System.currentTimeMillis()); return chain.filter(exchange).then( Mono.fromRunnable(() -> { Long startTime = exchange.getAttribute(START_TIME); if (startTime != null) { log.info(exchange.getRequest().getURI().getRawPath() + ": " + (System.currentTimeMillis() - startTime) + " ms"); } }) ); } @Override public int getOrder() { return -1; } } ``` 二、总结 Spring Cloud Gateway整合Spring Cloud的组件和功能,提供了一种轻量级、高效、易扩展的网关服务。通过配置路由规则和自定义过滤器,可以实现网关的基本功能,并且可以方便地扩展和定制化。同时,Spring Cloud Gateway还提供了丰富的插件,支持更多的业务场景和功能需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值