1. Feign custom configuration and root containers are effectively isolated.
- Annotated with @configuration
- cannot be detached from its package name within the scope of the primary @componentscan (or @SpringBootApplication)
- Note that the best way to avoid packet scanning overlap is to explicitly specify the package name
2. Spring Cloud Netflix provides the default bean type:
- Decoder Feigndecoder:responseentitydecoder (which wraps a springdecoder)
- Encoder Feignencoder:springencoder
- Logger Feignlogger:slf4jlogger
- Contract Feigncontract:springmvccontract
- Feign.builder FeignBuilder:HystrixFeign.Builder
3. Spring Cloud Netflix does not provide default values, but can still be created in the feign context configuration:
- Logger.level
- Retryer
- Errordecoder
- Request.options
- Collection
4. Custom Feign message codecs:
Do not getobject the new object inside the method in the following code, the GetObject method is called frequently by the outside.
1 2 3 4 5 6 |
objectfactorynew objectfactory< Httpmessageconverters> () { @Override Public httpmessageconverters getObject() throws Beansexception { return httpmessageconverters; } }; |
5. Note the test environment and production environment, and pay attention to using the feign log level correctly. 6. Correct configuration of apachehttpclient or other client:
- Apachehttpclient The custom configuration is placed in spring root context, do not feigncontext, otherwise it will not work.
- Apachehttpclient Connection Pool configuration reasonable connection and other parameters
7. Feign configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#Hystrix支持, if the True,hystrix library must be in Classpath Feign.hystrix.enabled=false
#请求和响应GZIP压缩支持 Feign.compression.request.enabled=true Feign.compression.response.enabled=true #支持压缩的mime types Feign.compression.request.enabled=true Feign.compression.request.mime-types=text/xml,application/xml,application/json feign.compression.request.min-request-size=2048 # Log Support Logging.level.project.user.UserClient:DEBUG |
8. Logger.level Support
Each feign client configuration must be configured to tell feign how to output the log, optionally:
- NONE, No logging (DEFAULT).
- BASIC, Log only the request method and URL and the response status code and execution time.
- HEADERS, Log the basic information along with request and response HEADERS.
- Full, Log the headers, body, and metadata for both requests and responses.
9. Feignclient.fallback the correct use method
The configured fallback class must also be instantiated in the feignclient configuration, otherwise it will be reported
java.lang.IllegalStateException: No fallback instance of type class
Abnormal.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 |
@FeignClient (name = public Interface hystrixclient { @RequestMapping (method = Requestmethod.get, Value = hello ifailsometimes (); } public class Hystrixclientfallback implements hystrixclient { span class= "meta" > @Override public hello ifailsometimes () { return new hello ( "fallback"); } } @Configuration Public class Fooconfiguration { @Bean @Scope ("prototype") Public feign. Builder feignbuilder() { return Feign.builder (); }
@Bean Public Hystrixclientfallback FB() { return new Hystrixclientfallback (); }
} |
10. Precautions when using feign Client and @requestmapping
When the current project has the same endpoint as the feign client, @requestmapping annotations cannot be used on feign client's class. Otherwise, the current project endpoint HTTP request and use Accpet will be reported 404.
The following example:
There's a Controller.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@RestController @RequestMapping ("/v1/card") Public class Indexapi { @PostMapping ("balance") @ResponseBody Public Info Index() { Info.builder Builder = new Info.builder (); Builder.withdetail ("x", 2); Builder.withdetail ("Y", 2); return Builder.build (); } } |
There is a feign Client
1 2 3 4 5 6 7 8 9 All |
@FeignClient ( name = URL = fallback = Cardfeignclientfallback.class, Configuration = Feignclientconfiguration.class ) @RequestMapping (Value = "/v1/card") public interface cardfeignclient { @RequestMapping (value = info info (); } |
If @RequestMapping is used on class, when Invoke Http/v1/card/balance, like this:
If the @RequestMapping annotation is used on the Feignclient class, be aware of the accept header when requesting/v1/card/balance like the following code:
1 2 3 4 |
Content-type: Application/json Accept: Application/json POST http://localhost:7913/v1/card/balance |
Then it will return 404.
OK if the request is not included in the Accept header:
1 2 |
Content-type:application/json POST http://localhost:7913/v1/card/balance |
Or if the @requestmapping annotation is not used on the feign client as below, the request is OK, whether or not the accept is included:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@FeignClient ( Name = "card", url = "http://localhost:7913", fallback = Cardfeignclientfallback.class, Configuration = Feignclientconfiguration.class ) Public Interface cardfeignclient { @RequestMapping (value = "/v1/card/balance", method = Requestmethod.post, produces = Mediatype.application_json_ VALUE) Info Info(); } |
Spring Cloud feign usage and performance optimization