This is the 13th day of my participation in Gwen Challenge

One, foreword

Spring Cloud encapsulates Feign to support SpringMVC standard annotations and HttpMessageConverters.

Feign can be used in combination with Eureka and Ribbon to support load balancing, and with Hystrix to support circuit breaker fallback.

Cloud. Spring. IO/spring – clou…


use

Use divided into:

  1. Basic simple use
  2. Upload a file
  3. Modifying configuration filesURL


(1) Basic use

  1. Add the dependent
	<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
      <version>2.2.2. RELEASE</version>
    </dependency>
Copy the code
  1. Add in the startup class@EnableFeignClientsAnnotation, indicating that it is enabledfeignThe client
@EnableFeignClients
@SpringBootApplication
public class Application {

    public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code
  1. Add annotation @feignClient (name=”product-service”) to the interface to indicate that this is a FeIGN client-class interface

    Product-service is the service name corresponding to the registries (nacOS, Eureka).

@FeignClient("product-service")
public interface ProductClientService {
	
	@GetMapping("/api/v1/product/queryProductByIds")
	public List<Product> queryProductByIds(@RequestParam List<Integer> ids);
	
}
Copy the code
  1. Setting timeout

The default readTimeout is 60 seconds, but the default Hystrix is 1 second, so more than 1 second will also timeout.

# 2 properties must be set at the same time, otherwise it will not take effect, set the read timeout time to 11 seconds
feign.client.config.default.connectTimeout=5000
feign.client.config.default.readTimeout=11000
Copy the code


(2) PassFeignUpload a file

  1. Add the configuration
    <dependency>
      <groupId>io.github.openfeign.form</groupId>
      <artifactId>feign-form</artifactId>
      <version>3.8.0</version>
    </dependency>
    <dependency>
      <groupId>io.github.openfeign.form</groupId>
      <artifactId>feign-form-spring</artifactId>
      <version>3.8.0</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
    </dependency>
Copy the code
  1. defineFeignconfiguration
@Configuration
public class MultipartSupportConfig {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    public Encoder feignFormEncoder(a) {
        return new SpringFormEncoder(newSpringEncoder(messageConverters)); }}Copy the code
  1. use
import cn.percent.dolphin.oceanfile.common.Response;
import cn.percent.dolphin.oceanfile.config.MultipartSupportConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

@FeignClient(value = "oss", configuration = MultipartSupportConfig.class)
public interface OssClient {

    /** * Upload file **@paramThe file file *@returnThe path * /
    @PostMapping(value = "/api/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Response<String> uploadFile(@RequestPart(value = "file") MultipartFile file);

    /** * upload file **@paramThe path path *@returnByte stream * /
    @GetMapping(value = "/api/download")
    feign.Response downloadFile(@RequestParam(value = "path") String path);
}
Copy the code
  1. On the steps ofResponse
public class Response<T> implements Serializable {
    /** * Status code */
    private Integer code;
    /** * message */
    private String msg;
    /** * data */
    privateT data; . }Copy the code


(3) Modify the parameters based on the configuration fileURL

  1. Try to change according to the configuration fileURL
@FeignClient(name = "test", url="http://xxxx")
Copy the code
  1. Modify the
@FeignClient(url = "${feign.client.url.TestUrl}")
Copy the code

Application. Yml as follows:

feign:
  client:
    url:
      TestUrl: http://dev:dev
Copy the code




Second, addHystrix

  1. Rely on
    <! -- Already with hystrix -->
	<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
Copy the code
  1. implementation
@FeignClient("product-service",fallback = ProductFeignFallback.class)
public interface ProductClientService {
	
	@GetMapping("/api/v1/product/queryProductByIds")
	public List<Product> queryProductByIds(@RequestParam List<Integer> ids);
	
}
Copy the code

Service degradation processing:

import com.springcloud.product.feign.PriceFeign;
import lombok.extern.slf4j.Slf4j;

/** ** Price service downgrade processing */
@Slf4j
public class ProductFeignFallback implements ProductClientService {
    @Override
    public List<Product> queryProductByIds( List<Integer> ids) {
        log.info("Execution of downgrade logic begins...");
        returnCollections.emptyList(); }}Copy the code