【日常记录-Java】SpringBoot使用Feign请求

Author:赵志乾
Date:2024-08-22
Declaration:All Right Reserved!!!

1. 简介

        在SpringBoot中,使用Feign客户端进行服务间调用是一种非常流行的做法,特别是在微服务架构中。Feign是一个声明式的Web服务客户端,其使得编写Web服务客户端变得非常容易。

2. 步骤

   2.1 添加依赖

        在pom.xml中添加如下依赖:

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
   <version>3.1.5</version>
</dependency>
    2.2 启用Feign客户端

        在Spring主类或配置类上添加@EnableFeignClients注解,该注解会告诉SpringBoot去扫描 @FeignClient注解的类,并将它们注册为Spring应用程序上下文中的Bean。默认情况下SpringBoot扫描@EnableFeignClients注解标注类所在的包及其子包,当你的Feign客户端接口不在这些包内时,需要通过basePackages属性明确指定扫描的包路径。

@SpringBootApplication  
@EnableFeignClients(basePackages = "com.yourcompany.feignclients")  
public class YourApplication {  
    public static void main(String[] args) {  
        SpringApplication.run(YourApplication.class, args);  
    }  
}
   2.3 定义Feign客户端

        通过@FeignClient注解来定义Feign客户端,该注解可以指定服务名称(若使用了服务发现组件时,可用于服务发现),还可以配置其他与Feign相关的选项,如请求拦截、错误解码等。当需要向远端服务传递参数时,可以使用与SpringMVC相同的注解来定义参数传递方式,这些注解包括@RequestParam、@PathVariable、@RequestBody、@Header等。

@FeignClient(name = "your-service-name",url="${client.url}")  
public interface YourServiceClient {  
 
    @GetMapping("/some-endpoint")  
    String someMethod(@RequestParam("taskId")String taskId);  
}
  2.4 定义Feign客户端地址

        在application.yml中定义Feign客户端url属性的引用值。

client:
  url: http://localhost:8080/
  2.5 使用Feign客户端

        在SpringBoot应用中的任何地方注入并使用Feign客户端接口。

@Service  
public class MyService {  
  
    @Resource  
    private YourServiceClient yourServiceClient;  
  
    public String someMethod(String taskId)  {  
        return yourServiceClient.someMethod(taskId);  
    }  
}
  2.6 测试

        运行SpringBoot应用,并调用MyService中的方法,查看是否能够正确地从远程服务接收到响应。

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用Feign调用微服务时,如果想取消响应数据映射,直接以String类型返回,可以通过在Feign的配置类上加上`Decoder`和`Encoder`的Bean实现。具体操作如下: 1. 创建一个配置类,例如`FeignConfig`,并在该类上加上注解`@Configuration`。 2. 在配置类中定义一个`Decoder`类型的Bean,用于取消响应数据映射,直接以String类型返回。具体代码如下: ```java @Bean public Decoder feignDecoder() { return new Decoder() { @Override public Object decode(Response response, Type type) throws IOException, FeignException { if (type.equals(String.class)) { return Util.toString(response.body().asReader()); } else { return new JacksonDecoder().decode(response, type); } } }; } ``` 3. 在配置类中定义一个`Encoder`类型的Bean,用于将请求数据转换为String类型。具体代码如下: ```java @Bean public Encoder feignEncoder() { return new Encoder() { @Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { if (bodyType.equals(String.class)) { template.body(object.toString(), Charset.defaultCharset()); } else { new JacksonEncoder().encode(object, bodyType, template); } } }; } ``` 4. 最后,在Feign客户端接口上加上注解`@FeignClient`,并指定配置类`FeignConfig`,即可实现取消响应数据映射,直接以String类型返回。例如: ```java @FeignClient(name = "microservice", configuration = FeignConfig.class) public interface MicroserviceClient { @GetMapping("/api/data") String getData(); } ``` 以上就是使用Feign调用微服务,取消响应数据映射,直接以String类型返回的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我叫白小猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值