SpringCloud之@FeignClient()注解的使用方式

作者:Zero . 时间:2022-05-16 04:22:40 

@FeignClient()注解的使用

由于SpringCloud采用分布式微服务架构,难免在各个子模块下存在模块方法互相调用的情况。比如service-admin服务要调用service-card 服务的方法。

  • @FeignClient()注解就是为了解决这个问题的。

  • @FeignClient()注解的源码要求它必须在Interface接口上使用。( FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的作用目标在接口上)

@RequestLine与其它请求不同,只需要简单写请求方式和路径就能达到请求其它服务的目的。


@FeignClient(value = "feign-server",configuration = FeignConfig.class)  //需要一个配置文件
public interface TestService {
   @RequestLine("POST /feign/test")    //对应请求方式和路径
   String feign(@RequestBody UserDO userDO);
}

@EnableFeignClients
@SpringBootConfiguration
public class FeignConfig {
   @Bean
   public Contract contract(){
       return new feign.Contract.Default();
   }
}

@FeignClient标签的常用属性如下

  • value: 服务名

  • 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的统一前缀

此外还要求服务的启动类要有@EnableFeignClients 注解才能使Fegin生效。

SpringCloud 服务间互相调用 @FeignClient注解

SpringCloud搭建各种微服务之后,服务间通常存在相互调用的需求,SpringCloud提供了@FeignClient 注解非常优雅的解决了这个问题

首先,保证几个服务都在一个Eureka中注册成功形成服务场。

如下,我一共有三个服务注册在服务场中。COMPUTE-SERVICE ; FEIGN-CONSUMER ; TEST-DEMO;

SpringCloud之@FeignClient()注解的使用方式

我在FEIGN-CONSUMER

服务中调用其他两个服务的两个接口

分别为get带参和post不带参两个接口如下这个是COMPUTE-SERVICE中的get带参方法


@RequestMapping(value = "/add" ,method = RequestMethod.GET)
public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
   ServiceInstance instance = client.getLocalServiceInstance();
   Integer r = a + b;
   logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
   return r;
}

如果要在FEIGN-CONSUMER 服务中调用这个方法的话,需要在 FEIGN-CONSUMER 中新建一个接口类专门调用某一工程中的系列接口


@FeignClient("compute-service")
public interface ComputeClient {
    @RequestMapping(method = RequestMethod.GET, value = "/add")
    Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}

其中,@FeignClient注解中标识出准备调用的是当前服务场中的哪个服务,这个服务名在目标服务中的配置中取


spring.application.name

接下来,在@RequestMapping中设置目标接口的接口类型、接口地址等属性。然后在下面定义接口参数以及返回参数

在FEIGN-CONSUMER

Controller层调用方法的时候

将上面接口注入进来,就可以直接用了


@Autowired
ComputeClient computeClient;

@RequestMapping(value = "/add", method = RequestMethod.GET)
public Integer add() {
    return computeClient.add(10, 20);
}

当然,post方法同理:

这是目标接口:


@RestController
@RequestMapping("/demo")
@EnableAutoConfiguration
public class HelloController {
   @RequestMapping(value = "/test",method = RequestMethod.POST)
   String test1(){
      return "hello,test1()";
   }
}

这是在本项目定义的接口文件:


@FeignClient("test-Demo")
public interface TestDemo {
    @RequestMapping(method = RequestMethod.POST, value = "/demo/test")
    String test();
}

这是项目中的Controller层


@RestController
public class ConsumerController {
    @Autowired
    TestDemo testDemo;

@Autowired
    ComputeClient computeClient;

@RequestMapping(value = "/add", method = RequestMethod.GET)
    public Integer add() {
        return computeClient.add(10, 20);
    }

@RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test() {
        return testDemo.test();
    }
}

最终调用结果如下:

SpringCloud之@FeignClient()注解的使用方式

OK 服务间接口调用就是这样了!

来源:https://blog.csdn.net/admin123404/article/details/103678156

标签:SpringCloud,@FeignClient,注解
0
投稿

猜你喜欢

  • java实现顺时针打印矩阵

    2023-06-26 19:17:22
  • 解决java.lang.ClassCastException的java类型转换异常的问题

    2023-11-29 04:34:10
  • C#二维码图片识别代码

    2022-01-15 13:03:51
  • JFinal使用ajaxfileupload实现图片上传及预览

    2023-08-05 08:30:48
  • java中i = i++和i =++i的深入讲解

    2021-10-04 17:25:48
  • SpringBoot+WebSocket实现多人在线聊天案例实例

    2022-08-22 11:53:08
  • Springboot整合mqtt服务的示例代码

    2022-07-20 02:58:01
  • java 中模式匹配算法-KMP算法实例详解

    2022-01-30 09:08:08
  • Spring Boot2如何构建可部署的war包

    2023-11-29 06:40:59
  • 详解JAVA 线程-线程的状态有哪些?它是如何工作的?

    2023-11-27 03:33:09
  • IntelliJ IDEA 好用插件之analyze inspect code详解

    2021-09-26 22:16:36
  • Springboot 在普通类型注入Service或mapper

    2023-11-29 15:26:21
  • MyEclipse2018中安装Mybatis generator插件的实现步骤

    2022-02-17 03:47:37
  • 详解在Spring Boot中使用JPA

    2022-08-03 15:44:13
  • java实现钉钉机器人消息推送的示例代码

    2023-05-18 13:53:25
  • Spring整合WebSocket应用示例(上)

    2023-05-05 10:09:21
  • spring boot 集成 shiro 自定义密码验证 自定义freemarker标签根据权限渲染不同页面(推荐

    2023-07-28 17:39:16
  • Nacos 动态服务发现、配置和服务管理平台初体验

    2022-09-10 23:56:53
  • 聊聊Java的switch为什么不支持long

    2023-08-24 17:35:14
  • SpringBoot整合Shiro的方法详解

    2022-04-13 15:05:56
  • asp之家 软件编程 m.aspxhome.com