A simple example of using FeignClient is as follows:
1. Add maven dependency
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.0.2.RELEASE</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-core</artifactId> <version>9.7.0</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-slf4j</artifactId> <version>9.7.0</version> </dependency>
2. Write FeignClient code
@FeignClient(name = "myFeignClient", url = "http://127.0.0.1:8001") public interface MyFeignClient { @RequestMapping(method = RequestMethod.GET, value = "/participate") String getCategorys(@RequestParam Map<String, Object> params); }
3. Use FeignClient directly
@Autowired MyFeignClient myFeignClient;
Now, FeignClient can use the normal Http interface~
4. If you want to use the file upload interface or the x-www-form-urlencoded interface of post, you need to do the following configuration
Add dependency package
<dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency>
Add bean annotation configuration
@Bean @Primary @Scope("prototype") public Encoder multipartFormEncoder(ObjectFactory<HttpMessageConverters> messageConverters) { return new SpringFormEncoder(new SpringEncoder(messageConverters)); }
Define file upload interface
@RequestMapping(value = {"/demo/v1/upload"}, method = {RequestMethod.POST}, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) ReturnResult<ImageVO> uploadFile( @RequestPart(value = "file") MultipartFile file, @RequestParam(value = "bucketName", required = false) String bucketName);
5. If you want to use the connection pool of Apache's httpclient, you can do the following configuration
Add dependency
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> <version>9.7.0</version> </dependency>
Add property configuration
feign: okhttp: enabled: false httpclient: enabled: true maxConnections: 20480 maxConnectionsPerRoute: 512 timeToLive: 60 connectionTimeout: 10000 userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0'
Introducing FeignAutoConfiguration configuration
@Import(FeignAutoConfiguration.class) @Configuration public class FeignConfig { ... }
After these steps, you can enable Apache's httpclient to replace its embedded httpclient.
6. If you want to enable hystrix fuse degradation, you can make the following configuration
Add dependency
<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-hystrix</artifactId> <version>9.7.0</version> </dependency>
Add property configuration
feign: hystrix: enabled: true hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 15000 threadpool: default: coreSize: 40 maximumSize: 100 maxQueueSize: 100
Add demotion policy
public class MyFeignClientFallback implements MyFeignClient { @Override public ReturnResult<ImageVO> uploadFile(MultipartFile file, String bucketName) { return new ReturnResult<>(5001); } }
Add bean configuration
@Bean @Scope("prototype") public Feign.Builder feignBuilder() { return HystrixFeign.builder(); } @Bean public MyFeignClientFallback fb() { return new MyFeignClientFallback(); }
Update @ FeignClient code
@FeignClient( name = "myFeignClient", url = "http://127.0.0.1:8001", fallback = MyFeignClientFallback.class, configuration = {FeignConfig.class})
7. If you want to deal with the specific causes of fusing, you can update as follows
Update fuse policy code to implement FallbackFactory interface
public class MyFeignClientFallback implements FallbackFactory<MyFeignClient> { @Override public MyFeignClient create(final Throwable cause) { return new MyFeignClient() { @Override public ReturnResult<ImageVO> uploadFile(MultipartFile file, String bucketName) { // Processing cause return new ReturnResult<>(5001); } }; } }
Update bean configuration
@Bean public MyFeignClientFallback fbf() { return new MyFeignClientFallback(); }
Update @ FeignClient code
@FeignClient( name = "myFeignClient", url = "http://127.0.0.1:8001", fallbackFactory = MyFeignClientFallback.class, configuration = {FeignConfig.class})
-End-