Spring Cloud voice client call tool Feign

Keywords: Programming Spring Apache xml

I. The difference between Ribbon and Feign

Two kinds of client-side calling tools are supported in Spring Cloud:

1. RestTemplate in the Ribbon

The 2. is Feign.

Ribbon: a load balancer based on HTTP and TCP clients

Feign: Spring Cloud Netflix's microservices are exposed in the form of HTTP interfaces, so they can be called by Apache's HttpClient or Spring's RestTemplate.

It is an Http client that further encapsulates Http on the basis of Ribbon. It is a declarative client calling tool that implements microservice call in the form of interface + annotation.

It also integrates Spring Cloud Ribbon and Spring Cloud Hystrix.

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

**Summary: * * the way to call the RestTemplate interface of Ribbon is not used generally;

Feign declarative interface call mode, recommended.

II. Practical use

A. Jar package dependency

Spring Boot 1.x version depends on Feign

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

Spring Boot 2.x version relies on Feign

       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

**Note: * * Spring Boot1.x and 2.x refer to Feign with different names

B. Feign interface statement

@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);

Be careful:

**1. The declared interface is * *, and * * is not a class.

2. @ feignclient (name = "tigbs report"), @ RequestMapping. These two annotations are required.

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

C. the called entry of Feign interface

@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 form 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 identification", 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. the startup class plus the startup Feign client annotation @ 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 call is over, that is, so easy ~ ~, continue to refuel~~

Posted by r3n on Thu, 31 Oct 2019 04:15:09 -0700