首页 > 其他分享 >openfeign接口Springboot启动Bean报错未找到Singleton bean creation not allowed while singletons of this factory

openfeign接口Springboot启动Bean报错未找到Singleton bean creation not allowed while singletons of this factory

时间:2024-05-09 20:23:00浏览次数:10  
标签:String OpenFeign bean 接口 factory xx 报错 test 注解

检查步骤

  1. 检查springboot启动类是否标注@EnableFeignClients注解,未标注该注解会导致无法注入bean
  2. 检查远程调用模块是否标注注解@FeignClient
  3. 检查@FeignClient注解中是否写了正确的微服务名称(区分大小写)
  4. 检查@FeignClient注解中标识的微服务是否启动

​ ​ 原因:此处接口可以声明@@Component注入ioc容器;并且启动类注解@EnableFeignClients若声明了接口所扫描package范围;则需要对应加上否则feign调用不生效启动bean报错s。

​ ​ OpenFeign是一个显示声明式的WebService客户端。使用OpenFeign能让编写Web Service客户端更加简单。使用时只需定义服务接口,然后在上面添加注解。OpenFeign也支持可拔插式的编码和解码器。spring cloud对feign进行了封装,使其支持MVC注解和HttpMessageConverts。和eureka(服务注册中心)和ribbon组合可以实现负载均衡。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求,非常的方便。

作用:

  • ​ OpenFeign的设计宗旨式简化Java Http客户端的开发。Feign在restTemplate的基础上做了进一步的封装,由其来帮助我们定义和实现依赖服务接口的定义。在OpenFeign的协助下,我们只需创建一个接口并使用注解的方式进行配置(类似于Dao接口上面的Mapper注解)即可完成对服务提供方的接口绑定,大大简化了Spring cloud Ribbon的开发,自动封装服务调用客户端的开发量。

  • ​ OpenFeign集成了Ribbon,利用ribbon维护了服务列表,并且通过ribbon实现了客户端的负载均衡。与ribbon不同的是,通过OpenFeign只需要定义服务绑定接口且以申明式的方法,优雅而简单的实现了服务调用。

    入门使用:

<!--Open feign-->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
  
<!--服务发现客户端-->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>

  <!--web服务-->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

主类主要注解:

@EnableFeignClients({"com.xx.xx", "com.xx.xx"})

代码示例:

A服务

controller层

@RequiredArgsConstructor
@Slf4j
@RestController
@RequestMapping("/inter/Demo")
public class Demo {
   @Autowired
   TestInterface testInterface;
   @PostMapping("/test")
   public String test(@RequestBody String param) {
      String str= testInterface.test(param);
      System.out.println("输出 str:{} " + str);
      return str;
   }
}

Interface

@FeignClient(
      name = "${xx.name:xx}",
      url = "${xx.application.xx.url:}",
      contextId = "testInterface"
)
public interface TestInterface {
   @PostMapping({"/test"})
   String test(@RequestBody String var1);
}

url是应用服务对应调用url

B服务

@FeignClient(
      name = "${xx.name:xx}",
      url = "${xx.application.xx.url:}",
      contextId = "testInterface"
)
public interface TestInterface {
   @PostMapping({"/test"})
   String test(@RequestBody String var1);
}
@RestController
public class TestInterfaceImpl implements TestInterface {

   @Override
   public String test(String var1) {
     
      System.out.println("var1:{} " + var1);
      return "OKOKOK";
   }
}

标签:String,OpenFeign,bean,接口,factory,xx,报错,test,注解
From: https://www.cnblogs.com/chillymint/p/18183013

相关文章

  • ubuntu22 python2 pyinstaller 打包报错:'NoneType' object has no attribute 'groups'
    前言最近有个需求,需要在ubnutu22上使用pyinstaller打包一个python2的文件。中间遇到了一些问题:pip2installpyinstaller报错解决方案:pip2installpyinstaller==3.6python2和python3的pyinstaller如何同时存在,我想把python2的pyinstaller命名为pyin......
  • 实验1-波士顿房价预测部分报错解决方法
    运行sgd=SGDRegressor()sgd.fit(x_train,y_train)print("r2scoreofLinearregressionis",r2_score(y_test,sgd.predict(x_test)))时出现DataConversionWarning:Acolumn-vectorywaspassedwhena1darraywasexpected.Pleasechangetheshapeofyt......
  • 深度学习tf2.0版本报错——实验24
    深度学习:fromtensorflow.contrib.rnnimportLSTMStateTuple没有contrib模块_indylstm-CSDN博客把原先的tf引用换成蓝色框框,然后把红框的代码改成星星处代码——————针对LSTMCell报错 tf2.0里面缺少crf包是因为把这个contrib包删除了所以要下载一个新的库:安装之后就不会......
  • LLaMA-Factory 训练 Llama3-Chinese-8B-Instruct 相关报错问题解决
    模型路径up主为llama中文社区模型地址https://www.modelscope.cn/models/FlagAlpha/Llama3-Chinese-8B-Instruct/summarysysinfov10032gnvcc--versioncuda11.8pythonimporttorchprint(torch.version)13.11pipinstallflash_attntimeout2下载whl报这个错......
  • text-generation-webui 推理模型Qwen1.5-7B-Chat相关报错问题解决
    推理代码text-generation-webui推理模型Qwen1.5-7B-Chatsysinfo nvcc--versioncuda11.8importtorch>>>print(torch.__version__)1路径错误2依赖没安装ImportError:Thismodelingfilerequiresthefollowingpackagesthatwerenotfoundinyourenvironme......
  • Mybatis if判断中使用了Ognl关键字导致报错解决方法
    mybatisxml中使用OGNL解析参数,如果直接使用了关键字则会导致解析失败。常见的关键字有:字段mybatis关键字bor(字符|)的英文xor字符^的英文and字符&&band字符&ed字符==neg字符!=lt字符<gt字符>lte字符<=......
  • idea使用svn报错-Error:Can not get current revision for file
    idea中使用svn结果报错:Error:CannotgetcurrentrevisionforfileD:/IDEADire…,并且idea提示一下警告:解决方案:安装svn的时候要主要勾选上第二个选项,如下图所示:最后在idea中配置svn的安装路径下的svn.exe,File->settings->VersionControl–>Subversion......
  • Spring如何控制Bean的加载顺序
    前言正常情况下,Spring容器加载Bean的顺序是不确定的,那么我们如果需要按顺序加载Bean时应如何操作?本文将详细讲述我们如何才能控制Bean的加载顺序。场景我创建了4个Class文件,分别命名为FirstInitializationSecondInitializationThirdInitializationForthInit......
  • 解决 node-gyp 错误问题|node与python版本不匹配报错|node-gyp|vue
    项目要用到node-gyp(给爷爬)mac上没问题windows有问题看上面的日志,提到了python2.7看来这个模块用的上古时代的python2.7的技术windows的同学可以先安装python2这里下载:https://www.python.org/ftp/python/2.7/python-2.7.amd64.msi不要修改路径!!不要修改路径!!等......
  • 解决HtmlUnit执行JS报错提示ScriptException
    问题描述HtmlUnit作为一款比Selenium更轻量的HeadLess的Java版本浏览器模拟器,不需要在服务器上安装部署浏览器及其Driver程序。但是,众所周知,HtmlUnit对JS脚本的支持并不是很有话,GitHub中大部分的issue都和JS执行错误有关。笔者在实际使用(HtmlUnit4.1.0版本)过程中也遇到了JS执......