最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Cloud 中@FeignClient注解中的contextId屬性詳解

 更新時(shí)間:2021年09月25日 10:10:05   作者:y_bccl27  
這篇文章主要介紹了Spring Cloud 中@FeignClient注解中的contextId屬性詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

@FeignClient注解中的contextId屬性

在使用@FeignClient注解前,我們需要先引入其相關(guān)依賴(lài),版本為3.0.1

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>3.0.1</version>
</dependency>

例如我們有一個(gè)user服務(wù),user服務(wù)中有很多個(gè)接口,我們通過(guò)@FeignClient來(lái)實(shí)現(xiàn)接口的調(diào)用,不想將所有的調(diào)用接口都定義在一個(gè)接口類(lèi)中,因此構(gòu)建了下述兩個(gè)Feign接口類(lèi):

@FeignClient(name = "user-server")
public interface UserServerClient1 {
 @GetMapping("/user/get")
 public User getUser(@RequestParam("id") int id);
}
@FeignClient(name = "user-server")
public interface UserServerClient2 {
 @GetMapping("/user/getAll")
 public List<User> getAllUser();
}

這種情況下啟動(dòng)項(xiàng)目,項(xiàng)目就會(huì)報(bào)錯(cuò),因?yàn)锽ean的名稱(chēng)沖突了,具體錯(cuò)誤如下:

Description:

The bean 'user-server.FeignClientSpecification' could not be registered. A bean with that name has already been defined and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

解決方法一

在yml配置文件中新增下述配置,允許出現(xiàn)beanName一樣的BeanDefinition

spring:
  main:
    allow-bean-definition-overriding: true

解決方法二

為每個(gè)FeignClient手動(dòng)指定不同的contextId,contextId的作用是用來(lái)區(qū)分FeignClient實(shí)例

@FeignClient(contextId = "userService1",name = "user-server")
public interface UserServerClient1 {
 @GetMapping("/user/get")
 public User getUser(@RequestParam("id") int id);
}
@FeignClient(contextId = "userService1",name = "user-server")
public interface UserServerClient2 {
 @GetMapping("/user/getAll")
 public List<User> getAllUser();
}

FeignClient注解及參數(shù)問(wèn)題

在用分布式架構(gòu)SpringBoot的SpringCloud技術(shù)開(kāi)發(fā)過(guò)程中,@FeignClient 是一個(gè)常用的注解,且很重要的功能。它是Feign客戶(hù)端提供負(fù)載均衡的熱插拔注解,通過(guò)該注解可以動(dòng)態(tài)代理創(chuàng)建Feign客戶(hù)端。

簡(jiǎn)單理解就是,分布式架構(gòu)服務(wù)之間,各子模塊系統(tǒng)內(nèi)部通信的核心。

一般在一個(gè)系統(tǒng)調(diào)用另一個(gè)系統(tǒng)的接口時(shí)使用,如下:

注解

@FeignClient("XXX")
public interface XX{
   ....
}

該注解一般創(chuàng)建在 interface 接口中,然后在業(yè)務(wù)類(lèi)@Autowired進(jìn)去使用非常簡(jiǎn)單方便。

問(wèn)題背景

創(chuàng)建好interface接口后,當(dāng)然要把調(diào)用該服務(wù)的接口方法定義出來(lái),該方法對(duì)應(yīng)本FeignClient的controller接口,必須重寫(xiě)該接口方法(返回對(duì)象,參數(shù)值完全一樣)。

啟動(dòng)項(xiàng)目出現(xiàn)如下報(bào)錯(cuò)時(shí),咋一看以為是在業(yè)務(wù)類(lèi)中調(diào)用該接口方法時(shí),傳參為空null而報(bào)錯(cuò)。

FactoryBean threw exception on object creation; nested exception is

java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0

當(dāng)把傳參用數(shù)據(jù)代替時(shí),重新啟動(dòng)時(shí);任然報(bào)如上錯(cuò)誤。

貼一個(gè)報(bào)錯(cuò)全截圖

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'withdrawCountRecordController': Unsatisfied dependency expressed through field 'withdrawCountService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'withdrawCountServiceImpl': Unsatisfied dependency expressed through field 'cumClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.epaylinks.efps.pas.clr.client.CumClient': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)

解決辦法

在@FeignClien("XX") 接口類(lèi)中,檢查每個(gè)方法的參數(shù)定義時(shí):

是否有如下情形

@RequestMapping(value="/XXX/query", method = RequestMethod.GET)
    public PageResult<XXutionResp> query(@RequestParam(required = false) String XXCode,
                                             @RequestParam(value = "XXnName",required = false) String institutionName,
                                             @RequestParam(value = "startTime",required = false) String startTime,

問(wèn)題就在這里:

@RequestParam(required = false) String XXCode

這個(gè)參數(shù)少了個(gè)value = "XXCode", 這個(gè)是Spring 4.0版本后,@RequestParam 注解對(duì)參數(shù)傳值有了很好的封裝特性并嚴(yán)格校驗(yàn)。

改為:

@RequestParam(value = "XXCode", required = false) String XXCode

之后,問(wèn)題完美解決;重啟項(xiàng)目正常。

另外,插一句:當(dāng)在項(xiàng)目多個(gè)地方調(diào)用同一個(gè)@FeignClien("XX")某項(xiàng)目時(shí),在多個(gè)包中創(chuàng)建接口,并無(wú)影響。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring Security 將用戶(hù)數(shù)據(jù)存入數(shù)據(jù)庫(kù)

    Spring Security 將用戶(hù)數(shù)據(jù)存入數(shù)據(jù)庫(kù)

    這篇文章主要介紹了Spring Security 如何將用戶(hù)數(shù)據(jù)存入數(shù)據(jù)庫(kù),幫助大家更好的理解和學(xué)習(xí)Spring Security,感興趣的朋友可以了解下
    2020-09-09
  • java分頁(yè)工具類(lèi)的使用方法

    java分頁(yè)工具類(lèi)的使用方法

    這篇文章主要為大家詳細(xì)介紹了java分頁(yè)工具類(lèi)的使用方法,穩(wěn)定的分頁(yè)效果,包括導(dǎo)航頁(yè)碼功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • 小白教程! Linux服務(wù)器上JDK安裝配置方法

    小白教程! Linux服務(wù)器上JDK安裝配置方法

    這篇文章主要為大家詳細(xì)介紹了Linux服務(wù)器上JDK安裝配置方法,小白教程!具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • springboot+vue2+elementui實(shí)現(xiàn)時(shí)間段查詢(xún)方法

    springboot+vue2+elementui實(shí)現(xiàn)時(shí)間段查詢(xún)方法

    這篇文章主要介紹了springboot+vue2+elementui實(shí)現(xiàn)時(shí)間段查詢(xún)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • Mybatis-Plus使用saveOrUpdate及問(wèn)題解決方法

    Mybatis-Plus使用saveOrUpdate及問(wèn)題解決方法

    本文主要介紹了Mybatis-Plus使用saveOrUpdate及問(wèn)題解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • java實(shí)現(xiàn)兩張圖片2D翻轉(zhuǎn)動(dòng)畫(huà)效果

    java實(shí)現(xiàn)兩張圖片2D翻轉(zhuǎn)動(dòng)畫(huà)效果

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)兩張圖片2D翻轉(zhuǎn)動(dòng)畫(huà)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • MyBatis-plus如何執(zhí)行自定義SQL

    MyBatis-plus如何執(zhí)行自定義SQL

    這篇文章主要介紹了MyBatis-plus如何執(zhí)行自定義SQL問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • 如何使用CountDownLatch同步j(luò)ava多線程

    如何使用CountDownLatch同步j(luò)ava多線程

    這篇文章主要介紹了如何使用CountDownLatch同步j(luò)ava多線程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • SpringBoot整合Mybatis與thymleft實(shí)現(xiàn)增刪改查功能詳解

    SpringBoot整合Mybatis與thymleft實(shí)現(xiàn)增刪改查功能詳解

    MybatisPlus是國(guó)產(chǎn)的第三方插件,?它封裝了許多常用的CURDapi,免去了我們寫(xiě)mapper.xml的重復(fù)勞動(dòng)。本文將整合MybatisPlus實(shí)現(xiàn)增刪改查功能,感興趣的可以了解一下
    2022-12-12
  • SpringCloud Gateway的基本入門(mén)和注意點(diǎn)詳解

    SpringCloud Gateway的基本入門(mén)和注意點(diǎn)詳解

    這篇文章主要介紹了SpringCloud Gateway的基本入門(mén)和注意點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評(píng)論

锡林郭勒盟| 毕节市| 尚义县| 修武县| 苍梧县| 湖北省| 长子县| 肥东县| 淮南市| 万源市| 买车| 罗源县| 常德市| 连南| 昌宁县| 榆林市| 岳阳市| 黄浦区| 措勤县| 普安县| 大关县| 罗源县| 鸡泽县| 鄂尔多斯市| 永仁县| 即墨市| 遵化市| 陇西县| 雷山县| 凭祥市| 抚州市| 张家口市| 五峰| 南和县| 汉寿县| 台北县| 肇州县| 信宜市| 武清区| 酒泉市| 寿阳县|