
步骤 1新建一个空的父工程feign-demo-parent父工程 pom.xml 核心?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion groupIdcom.demo/groupId artifactIdfeign-demo-parent/artifactId version0.0.1/version packagingpom/packaging namefeign-demo-parent/name modules modulefeign-consumer/module /modules properties maven.compiler.source8/maven.compiler.source maven.compiler.target8/maven.compiler.target project.build.sourceEncodingUTF-8/project.build.sourceEncoding spring.boot.version2.7.15/spring.boot.version spring.cloud.version2021.0.8/spring.cloud.version /properties dependencyManagement dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-dependencies/artifactId version${spring.boot.version}/version typepom/type scopeimport/scope /dependency dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-dependencies/artifactId version${spring.cloud.version}/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement /project步骤 2新建子模块 1feign-provider提供者端口为8081子模块1 pom.xml 核心?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd parent groupIdcom.demo/groupId artifactIdfeign-demo-parent/artifactId version0.0.1/version /parent modelVersion4.0.0/modelVersion artifactIdfeign-provider/artifactId namefeign-provider/name dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency /dependencies /project写好配置文件与启动类然后写好提供的接口ProviderController即可package com.demo.feignprovider.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; /** * author 86176 * date 2026/10/7 21:34 */ RestController Slf4j public class ProviderController { GetMapping(/hello/{name}) public String hello(PathVariable String name){ log.info(【来自provider8081】 hello name); return 【来自provider8081】 hello name; } }步骤 3新建子模块 2feign-consumer消费者端口为8082OpenFeign核心pom文件为?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdcom.demo/groupId artifactIdfeign-demo-parent/artifactId version0.0.1/version /parent artifactIdfeign-consumer/artifactId namefeign-consumer/name properties maven.compiler.source8/maven.compiler.source maven.compiler.target8/maven.compiler.target project.build.sourceEncodingUTF-8/project.build.sourceEncoding /properties dependencies dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency !-- openfeign -- dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-openfeign/artifactId /dependency !-- 负载均衡SpringCloud2021移除Ribbon必须加 -- dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-loadbalancer/artifactId /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency /dependencies /project随后写好配置文件和启动类即可启动类上要加入EnableFeignClients注解启动类代码如下SpringBootApplication EnableFeignClients //开启Feign public class FeignConsumerApplication { public static void main(String[] args) { SpringApplication.run(FeignConsumerApplication.class, args); } }随后写Feign接口ProviderFeignClientFeignClient(name provider, url http://127.0.0.1:8081) public interface ProviderFeignClient { GetMapping(/hello/{name}) String hello(PathVariable(name) String name); }并写一个controller类ConsumerController接收请求从而调用feign接口RestController Slf4j public class ConsumerController { Autowired private ProviderFeignClient providerFeignClient; GetMapping(/call/{name}) public String callProvider(PathVariable String name){ String result providerFeignClient.hello(name); log.info(consumer收到远程返回 result); return consumer收到远程返回 result; } }步骤4启动顺序 测试浏览器访问http://localhost:8082/call/李四即可测试ConsumerController是否成功接收请求从而是否成功调用feign接口git仓库https://gitee.com/gsgs73/feign-demo-parent.git