Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Spring Cloud declarative client invocation tool Feign

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Spring Cloud declarative client call tool Feign how to use, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

First, brief introduction feign integrates rabbion and hytrix, is perfectly compatible with spring mvc, and makes service invocation simple. II. The difference between Ribbon and Feign

Two client invocation tools are supported in Spring Cloud:

1. It's RestTemplate in Ribbon.

two。 It's Feign.

Ribbon: a load balancer based on HTTP and TCP clients

Feign: Spring Cloud Netflix's micro-services are exposed in the form of HTTP interfaces, so you can use Apache's HttpClient or Spring's RestTemplate to call

It is a Http client encapsulated by Http on the basis of Ribbon. It is a declarative client invocation tool that implements micro-service invocation in the form of interface + annotations.

It also integrates Spring Cloud Ribbon and Spring Cloud Hystrix.

In particular, you can view the jar package hierarchy of spring-cloud-netflix-core, and clearly see the relationship among Netflix, Feign and Ribbon.

* * Summary: * * the RestTemplate API of Ribbon is called, which is generally not used.

Feign declarative API call method is recommended.

II. Practical use

A, Jar package dependency

Spring Boot version 1.x depends on Feign

Org.springframework.cloud spring-cloud-starter-feign

Spring Boot version 2.x depends on Feign

Org.springframework.cloud spring-cloud-starter-openfeign

* * Note: * * Spring Boot1.x and 2.x have different names for referencing Feign.

B, Feign interface declaration

@ FeignClient (name = "tigbss-report") public interface ReportFeignService {@ RequestMapping (value = "/ openApi/manage/queryReportDetailByReportAndVersionId", method = RequestMethod.GET) ResponseObject queryRptDtail (@ RequestParam (value = "reportId") String reportId, @ RequestParam (value = "versionId") String versionId, @ RequestParam (value = "systemLabel") Integer systemLabel

Note:

* * 1. Declare * interface, * * is not a class

2. @ FeignClient (name = "tigbs-report"), @ RequestMapping, these two annotations are required

3. @ RestController cannot be added to the interface class, because the interface is an abstract class and cannot be instantiated

C, Feign interface is called to the entrance

@ RestController@RequestMapping ("api/operate/") public class ReportManageController {@ Autowired private ReportFeignService reportFeignService / * @ param reportId report id * @ param versionId report version id * @ return ResponseObject * / @ ApiOperation (value = "query report details", notes = "query report details") @ ApiImplicitParams ({@ ApiImplicitParam (paramType = "query", name = "reportId", value = "report ID", required = true, dataType = "String"), @ ApiImplicitParam (paramType = "query") Name = "versionId", value = "report version ID", required = true, dataType = "String"), @ ApiImplicitParam (paramType = "query", name = "systemLabel", value = "system identity", required = true, dataType = "int")}) @ RequestMapping (value = "reportDetail", method = RequestMethod.GET) public ResponseObject reportDetail (@ RequestParam (value = "reportId") String reportId @ RequestParam (value = "versionId") String versionId) {return reportFeignService.queryRptDtail (reportId, versionId, 3) }

D, startup class plus startup Feign client note @ EnableFeignClients

/ enable automatic configuration @ ComponentScan (value = {"com.drpp", "com.framework.workflow"}) @ EnableFeignClients (basePackages = {"com.drpp"}) @ SpringBootApplication@EnableTransactionManagement@ImportResource (locations = {"classpath:spring-res.xml"}) @ EnableAutoConfiguration@EnableDiscoveryClient@EnableHystrix@EnableEurekaClient@ServletComponentScan ({"com.drpp"}) public class CmsApplication extends SpringBootServletInitializer {public static void main (String [] args) {SpringApplication.run (CmsApplication.class, args) @ Override protected SpringApplicationBuilder configure (SpringApplicationBuilder builder) {return builder.sources (CmsApplication.class);} @ Bean public PlatformTransactionManager transactionManager (DataSource dataSource) {return new DataSourceTransactionManager (dataSource);}}

At this point, the actual use of Feign client calls is over, so so easy~~, continues to refuel.

After reading the above, have you mastered how to use the Spring Cloud declarative client call tool Feign? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report