Summary of several ways of FeignClient interface

 The @FeignClient annotation has encapsulated the remote call protocol. In the development of springboot or the development of microservices, we need to call across services or call external interfaces. We can use FeignClient.

 

1. Introduction to @FeignClient

The @FeignClient annotation is a component in Spring Cloud, which is implemented based on Netflix Feign. The @FeignClient annotation can help us define and implement RESTful interfaces between services, making calls between services more convenient and reliable. The @FeignClient annotation can be used in the client's API interface definition, which can convert an HTTP API interface into a Java interface, so that we can call remote services like calling local methods.

Annotation source code and analysis
 

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

    @AliasFor("name")
    String value() default "";

    String contextId() default "";

    @AliasFor("value")
    String name() default "";

    @Deprecated
    String qualifier() default "";

    String[] qualifiers() 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;

}

1.1 value attribute
The name of the service with an optional protocol prefix. Synonyms for names. A name must be specified for all clients whether or not a url is provided. Can be specified as a property key, for example: ${propertyKey}.

1.2 contextId attribute
This will be used as the bean name, not the name (if present), but will not be used as the service id.

1.3 The name attribute
has the same effect as the value

1.4 qualifier property
qualifier() is deprecated. If both qualifier() and qualifiers() exist, we will use the latter, unless the array returned by qualifiers() is empty or contains only null or blank values, in which case we will return qualifier() first, if also If it does not exist, the default value =contextId+"FeignClient" will be returned.

1.5 qualifiers attribute
As above, it has been explained

1.6 url attribute
Absolute URL or resolvable hostname (protocol is optional)

1.7 whether the decode404 attribute
404 should be decoded instead of throwing a false exception

1.8 configuration attribute
Custom configuration class for external clients. Override @Bean definitions that can contain parts that make up the client, such as feign.codec.Decoder, feign.codec.Encoder, feign.Contract.

1.9 fallback attribute
Specifies the fallback class of the external client interface. The fallback class must implement the interface annotated by this annotation and be a valid spring bean.

1.10 fallbackFactory attribute
Defines a fallback factory for the specified external client interface. The fallback factory must generate instances of fallback classes that implement the interface annotated by FeignClient. The fallback factory must be a valid spring bean.

1.11 path attribute
The path prefix used by all method-level mappings.

1.12 primary attribute
Whether to mark the external agent as the main bean. The default is true.

2. Summary of several ways of common FeignClient interface

@FeignClient(name = "myhuaweicloud", url = "https://xxx.com/")
public interface HuaweicloudClient {
	
		/**
		 * 获取access-token接口
		 */
	    @PostMapping("v1/xxx/enterprises/access-token")
		public Map<String,Object>  getAccessToken(@RequestBody Map<String, Object> params);

	    /**
		 * 获取实况地址统一接口
		 */
	    @PostMapping("/v2/xxx/devices/channels/media/live-connections")
		public Map<String, Object> liveConnections(@RequestHeader(name = "Access-Token",required = true) String Token,@RequestBody Map<String, Object> params);

		/**
		 * 获取通道列表
		 * @param map
		 * @return
		 */
	    @GetMapping("/v1/xxx/channels")
		public Map<String, Object> channels( @RequestHeader(name = "Access-Token",required = true) String Token,@RequestParam("device_id")String deviceId,
				
				@RequestParam("offset") Integer offset,@RequestParam("limit")Integer limit );
	    
		/**
		 * 获取设备列表
		 * @param map
		 * @return
		 */
	    @GetMapping("/v1/xxx/devices")
		public Map<String, Object> devices( @RequestHeader(name = "Access-Token",required = true) String Token,
				@RequestParam("device_name") String deviceName,
				@RequestParam("offset") Integer offset,
				@RequestParam("limit")Integer limit);

}

1. We use @FeignClient(name = "myhuaweicloud", url = "https:/xxx.com/")

Annotate the interface.

The method encapsulated in the interface has the same pass

@PostMapping

@GetMapping for annotation.

These two request methods correspond. Use the @PostMapping annotation to indicate that the interface needs to be requested through post, and use @GetMapping to indicate that the interface needs to be requested through get.

2. Look at the common parameter passing methods

@RequestBody Map<String, Object> params

Pass the json parameter in the form of body.

@RequestHeader(name = "Access-Token",required = true) String Token

Pass parameters in the request header

@RequestParam("device_name") String deviceName

Splicing parameters and values ​​​​in a key-value manner for transmission

Guess you like

Origin blog.csdn.net/dongjing991/article/details/132086839