Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Spring Cloud declarative client invocation tool Feign

Shulou Source: shulou.com Published: 2022-06-02 02:32:49 10月30日 Update

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!

Tags: Interface customer client report version tool comment service two content actual combat form method more details questions integration query balance Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information OPPO Reno Shulou Tech Info Huawei