将上一篇的feign接口抽取到公共api模块(包含feign接口示例)
文章目录
- 一、准备
- 二、主要工作
- 三、建立dto类
- 四、添加多个feign接口
- 五、测试
- 六、目录结构
- 6.1 父工程`service-demo`
- 6.2 `order-service`模块
- 6.3 `product-service`模块
- 6.4 `sd-api`模块
一、准备
将上一篇的目录结构改造一下:
- 修改包名使根路径分别为
com.hdl.order
和com.hdl.product
- 修改项目名为
service-demo
,(先后在项目结构、pom文件、pc文件管理器中修改) - 修改将
@EnableFeignClients
注解移动到order-service
,谁用谁就要开启
如下
到这里,feign服务能成功访问。
二、主要工作
- 建立新模块
sd-api
- 建立根路径包名
com.hdl.api
- 在
com.hdl.api
下建立client.order
包和client.product
,用来存放order和product模块的feign接口;建立config
包,用来存放配置类,建立dto
包,用来存放dto对象 - 将其它包下的feign接口移动到
com.hdl.api.client
下的对应包 - 引入
spring-cloud-starter-openfeign
和spring-cloud-loadbalancer
依赖,同时删掉其它模块下的这两个依赖 - 在
config
包下建立FeignConfig
类,主要目的是开启@EnableFeignClients
- 在
resource
下建立META-INF/spring.factories
,加载FeignConfig
到ioc容器中
- 建立根路径包名
- 修改需要用到feign接口的
order-service
模块- 移除对
product-service
模块的依赖, 引入sd-api
依赖 - 删掉
@EnableFeignClients
- 删掉
spring-cloud-starter-openfeign
和spring-cloud-loadbalancer
依赖
- 移除对
到这里,抽取就完成了,目录结构如下
三、建立dto类
建立dto类,测试feign接口为对象、返回值为对象时的场景
- 在
sd-api
模块下com.hdl.api.dto
包,建立ProductDTO
类,需要有getter
和setter
,否则feign接口收到的对象全为null值 - 在
product-service
模块下com.hdl.product.pojos.dto
包,建立ProductDTO
类,需要有getter
和setter
,否则controller接口会报406
@Data
public class ProductDTO{
Integer id;
String name;
Integer price;
}
四、添加多个feign接口
- 修改
product-service
的controller方法 - 修改
sd-api
的feign接口 - 修改
order-service
的调用feign接口的方法
五、测试
3个feign接口都正常
六、目录结构
6.1 父工程service-demo
实际上order-service
和product-service
高度重合,还可以抽取出一个模块作为它们的父模块,打包方式为pom
pom.xml
<?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>org.example</groupId>
<artifactId>service-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>order-service</module>
<module>product-service</module>
<module>sd-api</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-boot.version>2.7.18</spring-boot.version>
<spring-cloud.version>2021.0.8</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
<spring-framework.version>5.3.33</spring-framework.version>
</properties>
<!-- 依赖声明-->
<dependencyManagement>
<dependencies>
<!-- SpringFramework的依赖配置-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring-framework.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringCloud 微服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringCloud Alibaba 微服务 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringBoot 依赖配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- bootstrap 启动器 SpringBoot2.4之后不会默认加载bootstrap.yaml 所以要引入这个依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>public</id>
<name>aliyun nexus</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>aliyun nexus</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
6.2 order-service
模块
忽略掉sentinel的配置,那是后面的内容
pom.xml
<?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>
<parent>
<groupId>org.example</groupId>
<artifactId>service-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>order-service</artifactId>
<description>order-service服务</description>
<dependencies>
<!-- feign远程调用模块-->
<dependency>
<groupId>org.example</groupId>
<artifactId>sd-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos服务注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos 配置中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
</project>
6.3 product-service
模块
pom.xml
<?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>
<parent>
<groupId>org.example</groupId>
<artifactId>service-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>product-service</artifactId>
<description>product-service服务</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos服务注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos 配置中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
6.4 sd-api
模块
pom.xml
<?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>
<parent>
<groupId>org.example</groupId>
<artifactId>service-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>sd-api</artifactId>
<description>feign远程调用模块</description>
<dependencies>
<!-- feign接口需要的包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- feign接口需要-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>