2-10-Feign-最佳实践分析(11-Feign-实现Feign最佳实践)
所谓的最佳实践是针对发请求与收请求两个接口而言的
总共分两种规范:继承+抽取


由于继承会出现多次实现 且不同模块的维护人还不一样 要是出现更新 人力安排也是一个问题
抽取方式则不会出现这些问题 因为实现仅一份 而且还都是由服务维护方维护的 不存在人力安排问题
从生产者剥离
1.新建项目feign-util

2.feign-util修改POM文件-增加对应依赖
注意:需要将子模块的 groupId name url 等标签注释掉 否则有可能无法找到对应类
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.itcast.demo</groupId>
<artifactId>cloud-demo</artifactId>
<version>1.0</version>
</parent>
<!-- <groupId>org.example</groupId>-->
<artifactId>feign-util</artifactId>
<!-- <name>Archetype - feign-util</name>-->
<!-- <url>http://maven.apache.org</url>-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
</dependencies>
</project>
3.生产者项目增加依赖feign-util

4.迁移实体类
5.修改实体类引入路径

从消费者剥离
1.消费者增加依赖feign-util 并清除原来feign相关依赖[相关依赖交给单独的feign-util管理 选择哪个才是消费者要考虑的问题]

2.迁移实体类与客户端
3.EnableFeignClients注解指定客户端类

4.修改实体类与客户端引入路径


浙公网安备 33010602011771号