首页 > 其他分享 >springcloud集成 openfeign报错

springcloud集成 openfeign报错

时间:2023-12-20 11:34:29浏览次数:34  
标签:5.3 java openfeign spring jar springframework 报错 springcloud org

集成 openfeign 做远程调用和负载均衡

如果没有学 feign 的同学,可以使用 restTemplate 来做

出处有坑

步骤回显

  • 引入openfeign的依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  • 被调用的微服务提供接口
@RestController
@RequestMapping("/test")
public class CouponController {
    @RequestMapping("/list")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtils page = couponService.queryPage(params);
        return Result.ok().put("page", page);
    }
}
  • 调用方编写feign的接口
@FeignClient("test1-demo")
public interface TestFeign {

    @RequestMapping("/test/list")
    Result list();

}

项目用的alibaba的nacos作为注册中心,故@FeignClient(“test1-demo”)的名称为被调用服务接口放的在nacos上的注册名称。

  • 开启openFeign
    只需要在启动类上如下注解就好了
@SpringBootApplication
@EnableDiscoveryClient
//加入如下注解,开启openFeign
@EnableFeignClients
public class Test2DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(Test2DemoApplication.class, args);
    }

}

发现报错

缺少依赖,启动报错

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.bearjun.gulimail.member.feign.CouponFeign': Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:537) ~[spring-beans-5.3.6.jar:5.3.6]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.6.jar:5.3.6]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.6.jar:5.3.6]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.6.jar:5.3.6]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.6.jar:5.3.6]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.6.jar:5.3.6]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.6.jar:5.3.6]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.6.jar:5.3.6]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.6.jar:5.3.6]
    ... 21 common frames omitted
Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
    at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:333) ~[spring-cloud-openfeign-core-3.0.2.jar:3.0.2]

问题分析

由于SpringCloud Feign在Hoxton.M2 RELEASED版本之后不再使用Ribbon而是使用spring-cloud-loadbalancer,所以不引入spring-cloud-loadbalancer会报错。
链接:https://blog.csdn.net/weixin_43556636/article/details/110653989

解决方法

加入spring-cloud-loadbalancer依赖 并且在nacos中排除ribbon依赖,不然loadbalancer无效

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>

依赖冲突

java.lang.AbstractMethodError: org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.choose(Ljava/lang/String;Lorg/springframework/cloud/client/loadbalancer/Request;)Lorg/springframework/cloud/client/ServiceInstance;
    at org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient.execute(FeignBlockingLoadBalancerClient.java:88) ~[spring-cloud-openfeign-core-3.0.2.jar:3.0.2]
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:119) ~[feign-core-10.10.1.jar:na]
    at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89) ~[feign-core-10.10.1.jar:na]
    at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100) ~[feign-core-10.10.1.jar:na]
    at com.sun.proxy.$Proxy91.list(Unknown Source) ~[na:na]
    at com.bearjun.gulimail.member.controller.MemberController.list(MemberController.java:44) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_261]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_261]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_261]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_261]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.6.jar:5.3.6]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) ~[spring-web-5.3.6.jar:5.3.6]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.3.6.jar:5.3.6]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894) ~[spring-webmvc-5.3.6.jar:5.3.6]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.6.jar:5.3.6]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.6.jar:5.3.6]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060) ~[spring-webmvc-5.3.6.jar:5.3.6]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962) ~[spring-webmvc-5.3.6.jar:5.3.6]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.6.jar:5.3.6]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.6.jar:5.3.6]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.45.jar:4.0.FR]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.6.jar:5.3.6]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.45.jar:4.0.FR]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.45.jar:9.0.45]...........

问题分析

nacos中 pring-cloud-starter-netflix-ribbon会与它冲突,造成loadbalanc包失效

解决方法

我们只需要再nacos的服务注册与发现的依赖中去掉ribbon

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <!-- 排除ribbon的依赖 -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Maven显示中央仓库找不到spring-cloud-starter-alibaba-nacos-discovery,去阿里云镜像发现上面版本信息写的unknown,有点懵逼于是去MavenRepository找一下版本号,看一下与SpringCloud适配的版本,Nacos1.3,完事

版本不匹配解决方法如下:
com.alibaba.nacos.api.exception.NacosException: failed to req API解决方案

标签:5.3,java,openfeign,spring,jar,springframework,报错,springcloud,org
From: https://www.cnblogs.com/yayuya/p/17916158.html

相关文章

  • VMware Ubuntu虚拟机打开报错问题
    问题描述昨天虚拟机卡死,我把VMwareWorkstation的进程用任务管理器杀掉了,今天重新打开虚拟机却发现以下报错报错内容另一个程序已锁定文件的一部分,进程无法访问打不开磁盘“E:\VMware\Linux\ubuntu-18.04.6\ubuntu-18.04.6.vmdk”或它所依赖的某个快照磁盘。模块“Disk”启......
  • 如何解决excel导入大文件报错
    开发过程中我们经常会遇到Excel导入大文件报错的问题,经过不断的摸索,我发现我们可以利用缓存区来实现大文件的上传,下面是我本人封装的一个实现工具,有兴趣的小伙伴可以看看,希望能对大家有所帮助!publicclassExcelImportBigDataUtil{/***每次放入缓冲区最大行数......
  • mapstruct报错 No property named "XXXX" exists in source parameter(s). Type "XXXX
    1、问题现象java:Nopropertynamed"XXXX"existsinsourceparameter(s).Type"XXXX"hasnoproperties.2、相关环境依赖版本jdk:17maven:3.8.8springboot:3.1.4lombok:1.18.30mapstruct:1.5.53、解决办法在pom.xml中加入如下配置<annotationProcessor......
  • pip的一种报错
    最近在使用pip镜像源下载东西时,报错。报错信息WARNING:Therepositorylocatedatpypi.douban.comisnotatrustedorsecurehostandisbeingignored.IfthisrepositoryisavailableviaHTTPSwerecommendyouuseHTTPSinstead,otherwiseyoumaysilencethis......
  • android 接入 GreenDao 报错Unresolved reference: DaoMaster
    这是我在51CTO博客的第一篇博文。大家好,我是永远的回忆丶,混迹移动端多年的安卓开发工程师。今天我来分享一个踩坑记录,关于GreenDao接入的问题。出现场景:接入GreenDao并且使用更多插件之后,很容易产生报错Unresolvedreference:DaoMaster解决方法:plugins{id'com.android.......
  • 打工笔记--------------------winform程序报错CLR20r3签名System.I0.IOException
    先看问题编写了一个程序在我本机运行没有问题,放到别人电脑上就有可能报这种错误System.I0.IOException  首先我问了一下ChatPgt:他说:CLR20r3是一个通用的错误代码,表示在.NETFramework中发生了未处理的异常。System.IO.IOException是与输入/输出操作相关的一个常见......
  • 启用微服务报错:Method has too many Body parameters
    启用微服务报错:MethodhastoomanyBodyparameterspackagecom.llq.springcloud.controller;@RestControllerpublicclassStorageController{@ResourceprivateStorageServicestorageService;//扣减库存你@PostMapping("/storage/reduce")......
  • 启动微服务报错:java.lang.IllegalStateException: Service id not legal hostname (se
    Order微服务通过opneFeign调用storage和account时报错原因分析:服务名称不能有下划线,可以使用中划线,Springcloud无法识别下划线,将下划线改为中划线即可注意:这个改了之后,你在进行远程调用的时候,接口对应的方法接口也需要相应改变【分布式开发,一个微服务模块修改了,很可能......
  • 高德地图api报错 Get geolocation timeout.Get ipLocation failed.
    使用了https报错Getgeolocationtimeout.GetipLocationfailed. 报错是定位超时,由于JSAPI使用的是浏览器提供的定位服务,所以定位的准确度和成功率都会对浏览器有很大的依赖。由于Chrome在国内没有提供服务,因此使用Chrome定位服务的浏览器,比如:Chrome、火狐、安卓原生WebVie......
  • DevEco Studio 报错only contain [a-zA-z0-9_].
    DevEcoStudio报错onlycontain[a-zA-z0-9_].一、操作环境操作系统: Windows10专业版IDE:DevEcoStudio3.1SDK:HarmonyOS3.1二、错误>hvigorERROR:Failed:entry:default@CompileResource... >hvigorERROR:Toolsexecutionfailed.Error:'��̲1'onlycontain[a-zA......