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:
- Basic simple use
- Upload a file
- Modifying configuration files
URL
(1) Basic use
- 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
- Add in the startup class
@EnableFeignClients
Annotation, indicating that it is enabledfeign
The client
@EnableFeignClients
@SpringBootApplication
public class Application {
public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Copy the code
-
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
- 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) PassFeign
Upload a file
- 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
- define
Feign
configuration
@Configuration
public class MultipartSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder(a) {
return new SpringFormEncoder(newSpringEncoder(messageConverters)); }}Copy the code
- 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
- On the steps of
Response
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
- Try to change according to the configuration file
URL
@FeignClient(name = "test", url="http://xxxx")
Copy the code
- 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
- Rely on
<! -- Already with hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Copy the code
- 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