创建Consumer模块
1. 新建模块
在项目名上右键,依次点击 新建 > 新模块
在左侧选择Spring Initializr
在右侧输入模块名称consumer
,语言选择Java
,构建系统选Maven
,打包选择Jar
,然后点击“下一步”
然后按照下图所示,从左侧选择依赖:Spring Web
、Eureka Discovery Client
、OpenFeign
新建完consumer
模块后,在pom.xml
上右键,选择添加为 Maven 项目
修改pom.xml
,添加对common
模块的依赖,修改完之后重载Maven项目。完整配置如下文所示
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer</name>
<description>consumer</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 修改配置
打开application.properties
,按照下图修改
3. 修改代码
(1) 修改启动类
在ConsumerApplication
上,添加注解@EnableFeignClients
(2) 新建Feign
在com.example.consumer
包下,新建feign
包。并在com.example.consumer.feign
包下,新建StudentFeign
接口。代码如下图所示
(3) 新建Controller
在com.example.consumer
包下,新建controller
包。并在com.example.consumer.controller
包下,新建StudentController
类。代码如下图所示
4. 构建模块
先点击provider
模块下的maven clean
,完成后再点击maven compile
,构建模块