feignClient注解怎么用?

一、FeignClient注解

  FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的作用目标在接口上

1

2

3

4

5

@FeignClient(name = "github-client", url = "https://api.github.com", configuration = GitHubExampleConfig.class)

public interface GitHubClient {

    @RequestMapping(value = "/search/repositories", method = RequestMethod.GET)

    String searchRepo(@RequestParam("q") String queryStr);

}

 声明接口之后,在代码中通过@Resource注入之后即可使用。@FeignClient标签的常用属性如下:

  • name:指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务的名称,用于服务发现
  • url: url一般用于调试,可以手动指定@FeignClient调用的地址
  • decode404:当发生http 404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignException
  • configuration: Feign配置类,可以自定义Feign的Encoder、Decoder、LogLevel、Contract
  • fallback: 定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口
  • fallbackFactory: 工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑,减少重复的代码
  • path: 定义当前FeignClient的统一前缀

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

@FeignClient(name = "github-client",

        url = "https://api.github.com",

        configuration = GitHubExampleConfig.class,

        fallback = GitHubClient.DefaultFallback.class)

public interface GitHubClient {

    @RequestMapping(value = "/search/repositories", method = RequestMethod.GET)

    String searchRepo(@RequestParam("q") String queryStr);

 

    /**

     * 容错处理类,当调用失败时,简单返回空字符串

     */

    @Component

    public class DefaultFallback implements GitHubClient {

        @Override

        public String searchRepo(@RequestParam("q") String queryStr) {

            return "";

        }

    }

}

 

 在使用fallback属性时,需要使用@Component注解,保证fallback类被Spring容器扫描到,GitHubExampleConfig内容如下:

1

2

3

4

5

6

7

@Configuration

public class GitHubExampleConfig {

    @Bean

    Logger.Level feignLoggerLevel() {

        return Logger.Level.FULL;

    }

}

  在使用FeignClient时,Spring会按name创建不同的ApplicationContext,通过不同的Context来隔离FeignClient的配置信息,在使用配置类时,不能把配置类放到Spring App Component scan的路径下,否则,配置类会对所有FeignClient生效.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@FeignClient是Spring Cloud中的一个注解,用于声明一个Feign客户端。Feign是一个声明式的Web服务客户端,它使得编写Web服务客户端变得更加简单。通过使用@FeignClient注解,我们可以轻松地定义和使用Feign客户端来调用其他微服务。 下面是使用@FeignClient的步骤: 1. 添加依赖:首先,需要在项目的pom.xml文件中添加Feign的依赖。例如,对于Maven项目,可以添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 启用Feign客户端:在Spring Boot应用程序的启动类上添加@EnableFeignClients注解,以启用Feign客户端。例如: ```java @SpringBootApplication @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 定义Feign客户端接口:创建一个接口,并使用@FeignClient注解来声明该接口是一个Feign客户端。指定要调用的微服务的名称,例如: ```java @FeignClient(name = "service-name") public interface MyFeignClient { // 定义要调用的接口方法 } ``` 4. 使用Feign客户端:在需要调用其他微服务的地方,通过注入Feign客户端接口来使用它。例如: ```java @Autowired private MyFeignClient myFeignClient; public void doSomething() { // 调用Feign客户端的方法 myFeignClient.someMethod(); } ``` 通过以上步骤,我们就可以使用@FeignClient注解来定义和使用Feign客户端来调用其他微服务了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值