Using Spring Cloud OpenFeign for Remote Calls in a Microservice Project
This article demonstrates how to integrate Spring Cloud OpenFeign into a Java microservice project, covering dependency setup, Feign client definition, annotation usage, enabling remote calls, parameter passing, and testing with Postman, providing a complete step‑by‑step guide for distributed service communication.
Spring Cloud OpenFeign simplifies remote HTTP calls in microservice architectures by allowing declarative client interfaces with annotations.
1. Add OpenFeign dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>2. Define a Feign client interface
@FeignClient("passjava-study")
public interface StudyTimeFeignService {
@RequestMapping("study/studytime/member/list/test")
R memberStudyTimeTest();
}Place the interface in a package such as com.jackson0714.passjava.member.feign and add the
@EnableFeignClients(basePackages = "com.jackson0714.passjava.member.feign")annotation to the Spring Boot application class to enable scanning.
3. Implement a controller method that calls the remote service
@RequestMapping("/member/list/test")
public R memberStudyTimeTest() {
StudyTimeEntity studyTimeEntity = new StudyTimeEntity();
studyTimeEntity.setTotalTime(100);
studyTimeEntity.setQuesTypeId(1L);
return R.ok().put("studytime", Arrays.asList(studyTimeEntity));
}Another controller can invoke the Feign client:
@RequestMapping("/studytime/list/test/{id}")
public R getMemberStudyTimeListTest(@PathVariable("id") Long id) {
MemberEntity memberEntity = new MemberEntity();
memberEntity.setId(id);
memberEntity.setNickname("悟空聊架构");
R memberStudyTimeList = studyTimeFeignService.getMemberStudyTimeListTest(id);
return R.ok().put("member", memberEntity).put("studytime", memberStudyTimeList.get("studytime"));
}4. Test the remote call
Start both passjava-member and passjava-study services, then send a request with Postman or a browser to http://localhost:10000/member/member/studytime/list/test. The response includes the mocked study time (100 minutes) and the member nickname.
5. Summary of Feign usage steps
Include OpenFeign starter dependency.
Create an interface annotated with @FeignClient.
Define remote method signatures with the appropriate request mapping.
Inject and call the Feign client from your controllers.
Enable Feign scanning with @EnableFeignClients.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
