springcloud微服务feign组件报错

本文记录了在使用Spring Cloud和Feign搭建微服务过程中遇到的错误,并给出了详细的解决方案,涉及Spring Boot和Spring Cloud版本匹配的问题。

今天在用springcloud搭建微服务时,利用feign做通讯组件,结果报错

java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.openfeign.ribbon.FeignRibbonClientAutoConfiguration] from ClassLoader [sun.misc.Launcher$AppClassLoader@58644d46]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:659) ~[spring-core-5.0.0.RC3.jar:5.0.0.RC3]
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:556) ~[spring-core-5.0.0.RC3.jar:5.0.0.RC3]
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:541) ~[spring-core-5.0.0.RC3.jar:5.0.0.RC3]
    at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:599) ~[spring-core-5.0.0.RC3.jar:5.0.0.RC3]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:728) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:670) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:638) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1507) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:421) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:391) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:97) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:693) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
    at com.wugui.spring.cloud.weather.Application.main(Application.java:14) [classes/:na]
Caused by: java.lang.NoClassDefFoundError: org/springframework/cloud/client/loadbalancer/LoadBalancedRetryFactory
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_31]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2693) ~[na:1.8.0_31]
    at java.lang.Class.getDeclaredMethods(Class.java:1967) ~[na:1.8.0_31]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:641) ~[spring-core-5.0.0.RC3.jar:5.0.0.RC3]
    ... 19 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372) ~[na:1.8.0_31]
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361) ~[na:1.8.0_31]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_31]
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360) ~[na:1.8.0_31]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_31]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) ~[na:1.8.0_31]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_31]
    ... 23 common frames omitted

大致原因是找不到相关类LoadBalancedRetryFactory

解决办法:

  • 提供springboot版本和springcloud对应版本,原来:
        springBootVersion = '2.0.0.M3'
        springCloudVersion = 'Finchley.M2'
  • 改为:
    springBootVersion = '2.0.0.M3'
    springCloudVersion = 'Finchley.M9'

转载于:https://www.cnblogs.com/peterpoker/p/9478893.html

### 解决 Feign 与 `@GetMapping` 组合使用时报错 "Request method 'POST' not supported" 当遇到 Feign 客户端调用带有 `@GetMapping` 注解的服务接口却收到 "Request method 'POST' not supported" 错误时,通常是因为客户端发送请求的方式不匹配服务端定义的 HTTP 方法。 #### 原因分析 此问题可能源于以下几个方面: - **HTTP方法不一致**:Feign 默认情况下会根据配置来决定使用的 HTTP 请求方式。如果 Feign 配置不当,则可能会尝试以 POST 方式发起请求给只支持 GET 的 API 接口[^1]。 - **路径映射冲突**:有时 URL 映射可能存在歧义或重复,这也会导致框架选择错误的方法处理请求。 #### 解决策略 为了修正这个问题,可以采取如下措施之一或多者结合的方式来解决问题: ##### 修改 Feign Client 中的注解声明 确保 Feign client 使用正确的 HTTP 动词进行远程过程调用 (RPC),即应显式指定为 `GET` 请求而不是默认行为下的其他动词。可以通过设置 `method=RequestMethod.GET` 参数做到这一点: ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name="exampleService", url="${service.url}") public interface ExampleClient { @GetMapping(value="/api/exampleEndpoint", consumes="application/json") String fetchExampleData(); } ``` ##### 检查并调整 Spring Boot 应用程序中的组件扫描范围 确认启用了 Feign Clients 扫描功能的应用上下文中包含了实际定义了 Feign clients 的 Java 包位置。例如,在启动类上添加 `@EnableFeignClients(basePackages={"com.example.clients"})` 来指明哪些包内含有待注册的 Feign interfaces[^2]: ```java @SpringBootApplication @EnableFeignClients(basePackages = {"com.yyl.order"}) public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } } ``` 通过上述更改,应用程序能够正确识别和实例化所需的 Feign 客户端,并按照预期发出适当类型的 HTTP 请求至目标微服务
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值