Spring Cloud Alibaba - 13 OpenFeign application

Posted by nishith82 on Fri, 04 Feb 2022 08:41:29 +0100

Article catalogue

What's Feign

Feign is a declarative and templated HTTP client developed by Netflix, inspired by Retrofit and JAXRS ­ 2.0 and WebSocket.

Feign can help us call HTTP API more conveniently and gracefully

In Spring Cloud, using Feign is very simple - just create an interface and add annotations on it.

Feign supports a variety of annotations, such as feign's own annotations or JAX ­ RS annotation, etc. Spring Cloud has enhanced feign to support spring MVC annotations

In addition, Ribbon and Eureka are integrated to make Feign more convenient to use

( Ribbon+RestTemplate) Vs Feign

In the previous way, we use Ribbon+RestTemplate to invoke micro services. What's the disadvantage?

ResponseEntity<PayInfo> responseEntity = restTemplate.getForEntity(PAY_URI + orderInfo.getProductNo(), PayInfo.class);


#Ribbon call

 @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

Two disadvantages:

  1. If the constructed URL is very complex, passing parameters is a problem.
  2. At the same time, when the business is more complex, it is difficult to understand.

Feign integration

Construction of artist feign API sub module

Adopt the routine commonly used in development to define an artist feign API project, which is used to manage the interface with other services separately. Modules like DAO and COMMON

Step1 do not rely on

	<dependency>
			<groupId>org.springframework.cloudgroupId>
			<artifactId>spring-cloud-starter-openfeignartifactId>
		dependency>

Step 2 modify packaging method

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

The project is an ordinary jar, and there is no need to type an executable jar

Step 3 write declarative interface

@FeignClient(name = "artisan-product-center")
public interface ProductCenterFeignApi {
    /**
     * Declarative interface, remote call http://artisan-product-center/selectProductInfoById/{productNo}
     * @param productNo
     * @return
     */
    @RequestMapping("/selectProductInfoById/{productNo}")
    ProductInfo selectProductInfoById(@PathVariable("productNo") String productNo);
 
}

How to write this declarative interface--------- Copy the Controller interface directly.

Call the project artist cloud feign order to build

Step1 do not rely on

	 <dependency>
            <groupId>com.artisan</groupId>
            <artifactId>artisan-feign-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

The introduction of the sub module just now is equivalent to referring to feign because the dependency is passed. Here, only the sub module can be referenced.

Step 2 annotation

Open annotation and add @ EnableFeignClients

Step 3 call (call the remote service like calling the local mode)

Built by service provider artist cloud feign product

Like ordinary projects, there is no need for transformation

test

Start two services

artisan-cloud-feign-order artisan-cloud-feign-product

Access the interface provided by the Order service

Source code

https://github.com/yangshangwei/SpringCloudAlibabMaster