Source Code Analysis of Java B2C Springcloud Electronic Commerce Platform-Feign

Posted by pedroz on Fri, 17 May 2019 04:22:11 +0200

What is Feign

Feign, influenced by Retrofit, JAXRS-2.0 and WebSocket, is an open source project for Java to http client binding. Feign's main goal is to make the Java Http client simple.

Write a Feign

Now let's simply implement a FeignClient. First, through @FeignClient, the client, where value is the name of calling other services, and FeignConfig.class is the configuration file of FeignClient. The code is as follows:

@FeignClient(value = "service-hi",configuration = FeignConfig.class)
public interface SchedualServiceHi {
    @GetMapping(value = "/hi")
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

Its custom configuration file is as follows, of course, it can also be written without the configuration file, the default can be used:

@Configuration
public class FeignConfig {

    @Bean
    public Retryer feignRetryer() {
        return new Retryer.Default(100, SECONDS.toMillis(1), 5);
    }
    
}

Look at the source code for the FeignClient annotation, which is as follows:


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FeignClient {

@AliasFor("name")
String value() default "";
    
@AliasFor("value")
String name() default "";
    
@AliasFor("value")
String name() default "";
String url() default "";
boolean decode404() default false;

Class<?>[] configuration() default {};
Class<?> fallback() default void.class;

Class<?> fallbackFactory() default void.class;
}

String path() default "";

boolean primary() default true;

The FeignClient annotation is modified by @Target(ElementType.TYPE) to indicate that the purpose of the FeignClient annotation is on the interface.

@ Retention(RetentionPolicy.RUNTIME), annotations exist in the class bytecode file and can be retrieved by reflection at run time; @Documented indicates that the annotation will be included in javadoc.

The annotation used by feign to declare the interface of a REST client with that interface should be created (for example, to automatically connect to another component). If the functional area is available, it will be
Used for load balancing back-end requests and can configure load balancers
Use the same name (value) @RibbonClient as the camouflage client.

Where value() is the name of the service being invoked, just like name().

url(), directly fill in the hard-coded url, decode 404 (), that is, whether 404 is decoded or thrown an exception; configuration(), indicating the configuration class of FeignClient, the default configuration class is the FeignClients Configuration class, which can cover information such as Decoder, Encoder and Contentract, for custom configuration. fallback(), filling in the fuse information class.

Configuration of FeignClient

The default configuration class is FeignClients Configuration. Opening this class under the jar package of spring-cloud-netflix-core, you can see that it is a configuration class that injects many beans with related configuration, including feignRetryer, FeignLoggerFactory, Formatting Conversion Service, etc., including Decoder, Encoder, Contract, if these beans are not annotated. In case of incoming, default configuration is automatically injected.

Decoder feignDecoder: Response Entity Decoder (this is the encapsulation of Spring Decoder)

Encoder feignEncoder: SpringEncoder

Logger feignLogger: Slf4jLogger

Contract feignContract: SpringMvcContract

Feign.Builder feignBuilder: HystrixFeign.Builder

The code is as follows:

@Configuration
public class FeignClientsConfiguration {

...//Ellipsis code

@Bean
    @ConditionalOnMissingBean
    public Decoder feignDecoder() {
        return new ResponseEntityDecoder(new SpringDecoder(this.messageConverters));
    }

    @Bean
    @ConditionalOnMissingBean
    public Encoder feignEncoder() {
        return new SpringEncoder(this.messageConverters);
    }

    @Bean
    @ConditionalOnMissingBean
    public Contract feignContract(ConversionService feignConversionService) {
        return new SpringMvcContract(this.parameterProcessors, feignConversionService);
    }

...//Ellipsis code
}

Rewrite configuration:

You can rewrite the beans in FeignClients Configuration to achieve the purpose of custom configuration. For example, the default number of retries for FeignClients Configuration is Retryer.NEVER_RETRY, i.e. no retries. You want to rewrite, write a configuration file, and inject the beans of feignRetryer with the following code:

@Configuration
public class FeignConfig {

   @Bean
   public Retryer feignRetryer() {
       return new Retryer.Default(100, SECONDS.toMillis(1), 5);
   }

}

In the above code, the number of retries of the FeignClient is changed, the retry interval is 100 ms, the maximum retry time is 1 s, and the number of retries is 5.
java B2B2C Multi-tenant Electronic Mall System

Topics: Java Spring Retrofit REST