In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to integrate OpenFeign remote calls. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
1.Feign outlines the HTTP client for Feign declarative clients, making remote calls easier. By providing a template for HTTP requests, writing simple interfaces and inserting annotations, you can define the parameters, format, address and other information of HTTP requests and integrate Ribbon (load balancing component) and Hystix (service fuse component). There is no need to show that Spring Cloud Feign extends the support for SpringMVC annotations based on Netflix Feign. Remote invocation example
Example: query the user's learning time
The method of user micro-service passjava-member calling learning micro-service passjava-study
1. Introduce openfeign dependency
The pom files of passjava-member and passjava-study projects introduce openfeign dependencies
Org.springframework.cloud
Spring-cloud-starter-openfeign
2.StudyTimeController defines a remote invocation test method
Returns the total time for a user to learn a question
@ RequestMapping ("/ member/list/test")
Public R memberStudyTimeTest () {
StudyTimeEntity studyTimeEntity = new StudyTimeEntity ()
StudyTimeEntity.setTotalTime (100min); / / Learning time: 100min
StudyTimeEntity.setQuesTypeId (1L); / / question type: 1 (javaBasic)
Return R.ok () .put ("studyTime", Arrays.asList (studyTimeEntity))
}
Create a feign service under the 3.member directory
Create package: com.jackson0714.passjava.member.feign
Create a StudyTimeFeignService interface
Add the annotation @ FeignClient. The display declares that this interface is used to invoke the study service remotely.
@ FeignClient ("passjava-study")
Publicinterface StudyTimeFeignService {}
Add remote invocation method
Public R memberStudyTime ()
Add the path study/studytime/member/list/test of the method to be called remotely to the method
@ RequestMapping ("study/studytime/member/list/test")
Public R getMemberStudyTimeListTest ()
Add the annotation @ EnableFeignClients to start the remote invocation service.
Add an annotation @ EnableFeignClients to the class PassjavaStudyApplication.java.
BasePackages represents all interfaces with @ FeignClient annotations under the specified path.
@ EnableFeignClients (basePackages = "com.jackson0714.passjava.member.feign")
@ EnableDiscoveryClient
@ MapperScan ("com.jackson0714.passjava.member.dao")
@ SpringBootApplication
Publicclass PassjavaMemberApplication {
Public static void main (String [] args) {
SpringApplication.run (PassjavaMemberApplication.class, args)
}
}
Test interface
Start the passjava-member and passjava-study services
Enter the request address using the postman tool or browser
Http://localhost:10000/member/member/studytime/list/test
The returned result is as follows
Both studytime and member have data.
Study time: 100 minutes, nickname: Wukong chat structure
Interface test results 4. Test OpenFeign parameter transfer
Example: user id is passed between services as a parameter
MemberController
@ RequestMapping ("/ studytime/list/test/ {id}")
Public R getMemberStudyTimeListTest (@ PathVariable ("id") Long id) {
/ / member information found in mock database
MemberEntity memberEntity = new MemberEntity ()
MemberEntity.setId (id); / / Learning time: 100min
MemberEntity.setNickname ("Goku chat Architecture")
/ / remote call to get the user's learning time (the learning time is mock data)
R memberStudyTimeList = studyTimeFeignService.getMemberStudyTimeListTest (id)
Return R.ok () .put ("member", memberEntity) .put ("studytime", memberStudyTimeList.get ("studytime"))
}
StudyTimeFeignService
@ FeignClient ("passjava-study")
Publicinterface StudyTimeFeignService {
@ RequestMapping ("study/studytime/member/list/test/ {id}")
Public R getMemberStudyTimeListTest (@ PathVariable ("id") Long id)
}
StudyTimeController
@ RequestMapping ("/ member/list/test/ {id}")
Public R memberStudyTimeTest (@ PathVariable ("id") Long id) {
StudyTimeEntity studyTimeEntity = new StudyTimeEntity ()
StudyTimeEntity.setTotalTime (100min); / / Learning time: 100min
StudyTimeEntity.setQuesTypeId (1L); / / question type: 1 (javaBasic)
Return R.ok () .put ("studytime", Arrays.asList (studyTimeEntity))
}
3. Summarize that FeignClient uses methods to introduce OpenFeign dependencies to define FeignClient interface classes (note @ FeignClient), declare that this interface class is used to remotely call interface methods defined in the interface classes of other services, specify the path of remote service methods to call interface methods in Controller class to enable remote calls (Note @ EnableFeignClients) this is the end of how to integrate OpenFeign remote calls. I hope the above can be helpful to you. You can learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.