远程调用@FeignClient注解属性使用详解

 更新时间:2022年10月20日 08:34:08   作者:austin流川枫  
这篇文章主要为大家介绍了远程调用@FeignClient注解属性使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

正文

Feign是声明性的web服务客户端。它使编写web服务客户端更加容易。通过Feign我们可以实现调用远程服务像调用本地一样便捷。本篇文章主要详细聊聊Feign下的一个核心注解@FeignClient相关属性。

通过查阅@FeignClient源码,可以看到它的注解包括以下属性:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FeignClient {
    @AliasFor("name")
    String value() default "";
    /** @deprecated */
    @Deprecated
    String serviceId() default "";
    String contextId() default "";
    @AliasFor("value")
    String name() default "";
    String qualifier() default "";
    String url() default "";
    boolean decode404() default false;
    Class<?>[] configuration() default {};
    Class<?> fallback() default void.class;
    Class<?> fallbackFactory() default void.class;
    String path() default "";
    boolean primary() default true;
}

name

定义当前客户端Client的名称,等同于value属性。

value

定义当前客户端Client的名称,等同于name属性。

serviceId

目前serviceId已经废弃了,直接使用name即可。

contextId

比如我们有个user服务,但user服务中有很多个接口,我们不想将所有的调用接口都定义在一个类中,比如UserFeignClientAUserFeignClentB,当不同的Feignname一致的时候,这时候Bean的名称就会冲突,解决方式可以通过指定不同的contextId来解决问题,举个栗子:

UserFeignClientA

@FeignClient(name = "user.service", contextId = "userServiceA", url = "${user.service.url}", configuration = UserRemoteConfig.class)
public interface UserFeignClientA {
    /**
     * 获取用户默认身份信息
     */
    @RequestMapping("/user/identity/default") 
    ResultData<UserVisaIdentity> getDefaultIdentity(@RequestParam("userId") String userId);
}

UserFeignClientB

@FeignClient(name = "user.service", contextId = "userServiceB" url = "${user.service.url}", configuration = UserRemoteConfig.class)
public interface UserFeignClientB {
    /**
     * 新增大客户信息
     */
    @RequestMapping(value = {"/user/identity/saveIdentity"}, method = RequestMethod.POST)
    ResultData<UserVisaIdentity> saveIdentity(@RequestBody UserVisaIdentity userVisaIdentity);
}

url

url用于配置指定服务的地址,相当于直接请求这个服务。

path

@FeignClient(name = "template-service", url = "${universal.service.url}", path = "template", configuration = {RemoteErrorDecoder.class})
public interface ITemplateFeignService {
    /**
     * 多条件查询
     *
     * @param templateSearchDto 多条件查询
     * @return 模板列表
     */
    @GetMapping("/search")
    ApiResult<PageResult<TemplateVo>> search(@Valid @SpringQueryMap TemplateSearchDto templateSearchDto);
}

path定义当前FeignClient访问接口时的统一前缀,比如接口地址是/user/get, 如果你定义了前缀是user, 那么具体方法上的路径就只需要写/get 即可。上面将假设universal.service.url地址为 http://universal.com ,那么调用search请求地址为:universal.com/template/se…

configuration

configuration是配置Feign配置类,在配置类中可以自定义FeignEncoderDecoderLogLevelContract等。

fallback

fallback定义容错的处理类,也就是回退逻辑,fallback的类必须实现Feign Client的接口,无法知道熔断的异常信息。比如:

@FeignClient(name = "account-service") 
 public interface AccountServiceClient { 
     @RequestMapping(method = RequestMethod.GET, value = "/accounts/{accountName}", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, fallback = AccountServiceClientFallback.class) 
     String getAccount(@PathVariable("accountName") String accountName); 
} 
@Component
public class AccountServiceClientFallback implements AccountServiceClient {
     private static final Logger LOGGER = LoggerFactory.getLogger(AccountServiceClientFallback.class); 
     @Override 
     public String getAccount(String accountName) { 
         LOGGER.error("Error during getAccount, accountName: {}", accountName); 
     } 
}

fallbackFactory

也是容错的处理,可以知道熔断的异常信息。可以自定义fallbackFactory

@Component
public class UserRemoteClientFallbackFactory implements FallbackFactory<UserRemoteClient> {
    private Logger logger = LoggerFactory.getLogger(UserRemoteClientFallbackFactory.class);
    @Override
    public UserRemoteClient create(Throwable cause) {
        return new UserRemoteClient() {
            @Override
            public User getUser(int id) {
                logger.error("UserRemoteClient.getUser异常", cause);
                return new User(0, "默认");
            }
        };
    }
}
@FeignClient(value = "user-service", fallbackFactory = UserRemoteClientFallbackFactory.class)
public interface UserRemoteClient {
	@GetMapping("/user/get")
	public User getUser(@RequestParam("id") int id);
}

primary

primary对应的是@Primary注解,默认为true,官方这样设置也是有原因的。当我们的Feign实现了fallback后,也就意味着FeignClient有多个相同的BeanSpring容器中,当我们在使用@Autowired进行注入的时候,不知道注入哪个,所以我们需要设置一个优先级高的,@Primary注解就是干这件事情的。

qualifier

qualifier对应的是@Qualifier注解,使用场景跟上面的primary关系很淡,一般场景直接@Autowired直接注入就可以了。

如果我们的Feign Clientfallback实现,默认@FeignClient注解的primary=true, 意味着我们使用@Autowired注入是没有问题的,会优先注入你的FeignClient

如果你把primary设置成false了,直接用@Autowired注入的地方就会报错,不知道要注入哪个对象。

解决方案很明显,你可以将primary设置成true即可,如果由于某些特殊原因,你必须得去掉primary=true的设置,这种情况下我们怎么进行注入,我们可以配置一个qualifier,然后使用@Qualifier注解进行注入,示列如下:

@FeignClient(name = "optimization-user", path="user", qualifier="userRemoteClient")
public interface UserRemoteClient {
	@GetMapping("/get")
	public User getUser(@RequestParam("id") int id);
}

Feign Client注入

@Autowired
@Qualifier("userRemoteClient")
private UserRemoteClient userRemoteClient;

好了,以上就是@FeignClient注解相关属性说明。

以上就是远程调用@FeignClient注解属性使用详解的详细内容,更多关于@FeignClient 远程调用的资料请关注脚本之家其它相关文章!

相关文章

  • Java中对象快速复制的几种方式详解

    Java中对象快速复制的几种方式详解

    这篇文章主要介绍了Java中对象快速复制的几种方式详解,对象的克隆是指创建一个新的对象,且新的对象的状态与原始对象的状态相同,当对克隆的新对象进行修改时,不会影响原始对象的状态,需要的朋友可以参考下
    2023-08-08
  • springboot自定义redis-starter的实现

    springboot自定义redis-starter的实现

    这篇文章主要介绍了springboot自定义redis-starter的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • mybatis-plus主键生成策略

    mybatis-plus主键生成策略

    这篇文章主要介绍了mybatis-plus主键生成策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • SpringMVC解析post请求参数详解

    SpringMVC解析post请求参数详解

    今天小编就为大家分享一篇解决SpringMVC接收不到ajaxPOST参数的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-08-08
  • SpringBoot发送各种复杂格式邮件的示例详解

    SpringBoot发送各种复杂格式邮件的示例详解

    本文主要介绍了如何使用JavaMailSender接口和MimeMessageHelper类,在SpringBoot实现发送带有附件,嵌入资源,抄送和密送的复杂邮件,需要的可以了解下
    2024-11-11
  • springboot结合mybatis-plus快速生成项目模板的方法

    springboot结合mybatis-plus快速生成项目模板的方法

    Mybatis-Plus是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生,接下来通过本文给大家分享springboot结合mybatis-plus快速生成项目模板的方法,感兴趣的朋友一起看看吧
    2021-06-06
  • java线程池使用后到底要关闭吗

    java线程池使用后到底要关闭吗

    这篇文章主要给大家介绍了关于java线程池使用后到底要不要关闭的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • Java中Lombok @Value注解导致的variable not been initialized问题

    Java中Lombok @Value注解导致的variable not been initialized问题

    本文主要介绍了Java中Lombok @Value注解导致的variable not been initialized问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • springcloud如何使用Feign后台内部传递MultipartFile

    springcloud如何使用Feign后台内部传递MultipartFile

    这篇文章主要介绍了springcloud如何使用Feign后台内部传递MultipartFile,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • JAVA中常用的设计模式:单例模式,工厂模式,观察者模式

    JAVA中常用的设计模式:单例模式,工厂模式,观察者模式

    设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的。
    2020-04-04

最新评论