使用FeignClient,消费方使用方法,和一些坑

本文深入探讨了微服务架构下服务接口暴露、消费者调用、依赖引入及配置注册等关键步骤,详细介绍了如何通过Maven管理项目依赖、引入Spring Cloud组件进行服务发现、使用Feign实现远程调用,以及配置Eureka服务注册中心的过程。同时,文章还涉及了异常回调机制和熔断支持的实现,确保服务间的稳定交互。

1.首先我们将生产者的接口暴露出来,一般是作为独立的对外接口,写在一个项目里。将对外的接口打包。

当然还有我们的pom文件依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qdsg.ylt</groupId>
<artifactId>ylt_api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<description>所有对外api接口由该项目负责</description>
<modules>
<module>vedio_api</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>



<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
  //注意该依赖一定要放在spring-cloud-starter-openfeign依赖的上边,不然消费方可能会出现一些问题
   <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

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




</dependencies>

</project>

 

2.还有就是消费者,如何调用接口,

我们将打好的jar包引入到消费者的项目里边去,通过pom引入,(接口打包的时候会有相应的坐标)。

消费者的包结构

这个是消费者的启动类
@SpringBootApplication(scanBasePackages = {"com.qdsg","com.example"})
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.qdsg"})
public class FeignClientConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(FeignClientConsumerApplication.class, args);
}
}
看到启动类上扫面了两个包,一个是该项目的扫描,一个是引入jar的包扫描,你也可以简单来写(@SpringBootApplication(scanBasePackages = {"com"}))
还有就是@FeignClient注解的扫描,扫描的是对外接口所在的包。如下:

消费者调用提供的jar包(使用注入的方式)

 

这样就可以调用我们对外提供的接口了,但是要保证你的项目启动并且注册到eureka上。

如何注册呢,配置如下

eureka:
client:
service-url:
defaultZone: #这里是服务注册地址
register-with-eureka: true
fetch-registry: false
instance:
instance-id: 192.168.1.88
prefer-ip-address: true
lease-renewal-interval-in-seconds: 1
lease-expiration-duration-in-seconds: 5

 

下面就是对外提供的接口

对外的接口

 

 

异常回调

当然还有用到的实体类都要暴露出来

 消费者配置

##配置项目名字
eureka:
client:
service-url:
defaultZone: http://119.3.18.49:8071/eurekaservers/eureka/
fetch-registry: true
register-with-eureka: false
# #feign 对hytrix的支持
feign:
hystrix:
enabled: true
ribbon:
ReadTimeout: 15000
ConnectTimeout: 60000

hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 20000

 加上最后一个配置熔断才会有作用

转载于:https://www.cnblogs.com/guagua-join-1/p/9638767.html

### Spring Cloud FeignClient 暴露方法并实现内部调用 在微服务架构中,`@FeignClient` 是一种声明式的 HTTP 客户端工具,主要用于简化微服务间的通信。如果需要通过 `@FeignClient` 暴露方法并在内部进行调用,则可以通过以下式实现。 #### 1. 引入必要的依赖 为了使用 Feign 功能,需确保项目已引入相关依赖。以下是 Maven 配置中的必要部分: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 此配置会自动导入 Feign 所需的基础库以及与 Spring 的集成支持[^4]。 --- #### 2. 启动类启用 Feign 支持 在项目的主启动类上添加 `@EnableFeignClients` 注解以激活 Feign 客户端功能: ```java @SpringBootApplication @EnableFeignClients(basePackages = "com.example.feignclients") // 指定扫描路径 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 上述代码中指定了 Feign 接口所在的包路径,便于框架自动发现这些接口。 --- #### 3. 编写 Feign 客户端接口 创建一个 Feign 客户端接口来定义对外暴露的服务方法。例如: ```java @FeignClient(name = "example-service", url = "http://localhost:8080") public interface ExampleServiceClient { @GetMapping("/api/example/{id}") String getExampleById(@PathVariable("id") Long id); @PostMapping("/api/example/save") Boolean saveExampleData(@RequestBody ExampleDto exampleDto); } ``` 在此示例中: - `name` 属性表示目标服务的注册名称。 - 如果未使用 Eureka 或 Consul 等服务注册中心,可以显式设置 `url` 来指向具体的服务地址。 - 方法签名应匹配实际的目标 API 设计。 --- #### 4. 内部调用 Feign 客户端 为了让其他模块能够便地调用该 Feign 客户端,可以在业务逻辑层注入它,并封装成更通用的方法供外部调用。例如: ```java @Service public class InternalCallerService { private final ExampleServiceClient exampleServiceClient; public InternalCallerService(ExampleServiceClient exampleServiceClient) { this.exampleServiceClient = exampleServiceClient; } public String fetchExampleData(Long id) { try { return exampleServiceClient.getExampleById(id); // 调用 Feign 客户端方法 } catch (Exception e) { throw new RuntimeException("Failed to retrieve data from external service.", e); } } public boolean persistExampleData(ExampleDto dto) { return exampleServiceClient.saveExampleData(dto); // 调用 Feign 客户端保存数据 } } ``` 此处展示了如何将 Feign 客户端作为依赖注入到服务类中,并提供更高层次的功能抽象。 --- #### 5. 处理常见问题 当遇到远程调用失败时,请注意以下几点: - **参数绑定错误**:对于 GET 请求中的查询参数,建议始终为 `@RequestParam` 明确指定参数名[^3]。 - **负载均衡问题**:如果使用了 Ribbon 或者其他负载均衡器,URL 应替换为目标服务 ID(而非具体的 IP 端口号)。例如: ```java String response = restTemplate.getForObject("http://service-id/resource", String.class); ``` 这表明 URL 地址可以直接引用服务实例的名字而不是硬编码主机信息[^2]。 --- #### 总结 以上描述了一种利用 `@FeignClient` 构建微服务间交互的式,并说明了其基本原理实践技巧。通过这种式不仅可以减少手动管理 HTTP 请求的工作量,还能提高开发效率可维护性。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值