Example of Feign calling a third-party interface, FeignClient parameter dynamic configuration url

Feign is a Java Http client inspired by Retrofit, JAXRS-2.0, and WebSocket. Feign's main goal is to reduce the complexity of using Http API for everyone."

In fact, the bottom layer of Feign relies on Java's dynamic proxy mechanism, which encapsulates native Java Socket or Apache HttpClient, and implements remote procedure calls based on the Http protocol. Of course, Feign also implements mechanisms such as load balancing and fusing on this basis.

Annotation @FeignClient

For dynamic url, use url = ${}. It should be noted that the configuration file must be in the resource directory where the startup class is located!

// 注意:这里的url属性值不能为空字符串,但是可以设置为任意字符串值,在这里设置为“EMPTY”
@FeignClient(value = "CallbackAPI", url = "EMPTY", configuration = CallbackConfiguration.class)
public interface CallbackAPI {
    /**
     * 统一回调接口方法,请求消息体格式为JSON,响应消息体格式也为JSON
     * @param host 接口主机地址,如:http://localhost:8080
     * @param path 接口路径,如:/test/hello
     * @param queryMap 动态URL参数集合
     * @param body 请求消息体对象
     * @return
     */
    @RequestMapping(value = "{path}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    Object callback(URI host,
                    @PathVariable("path") String path,
                    @SpringQueryMap Map<String, Object> queryMap,
                    @RequestBody Object body);
    /**
     * 统一回调接口方法,请求消息体格式为JSON,响应消息体格式也为JSON
     * @param uri 完整的请求路径地址,如:http://localhost:8080/test/hello
     * @param queryMap 动态URL参数集合
     * @param body 请求消息体对象
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    Object callback(URI uri,
                    @SpringQueryMap Map<String, Object> queryMap,
                    @RequestBody Object body);
}
 
// 回调接口配置
public class CallbackConfiguration {
    @Bean
    public Encoder feignEncoder() {
        return new GsonEncoder();
    }
 
    @Bean
    public Decoder feignDecoder() {
        return new GsonDecoder();
    }
 
    @Bean
    public Retryer feignRetryer() {
        return new Retryer.Default();
    }
 
    @Bean
    public Logger feignLogger() {
        return new Slf4jLogger();
    }
 
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}
@Autowired
CallbackAPI callbackAPI;
 
String uri = "http://localhost:8080";
Map<String, Object> queryMap = new HashMap<>(0);
queryMap.put("k", "v");
Subject subject = Subject.builder().id(10).build();
// 请求主机地址与路径分开传递
this.callbackAPI.callback(URI.create(uri), "/test/simple/post/json", queryMap, subject);
// 直接将完整请求完整路径作为uri类型参数
this.callbackAPI.callback(URI.create("http://localhost:8080/test/simple/post/json"), queryMap, subject);

Annotation @RequestLine

 Steps for usage        

1. Do not write url in FeignClient, use @RequestLine modification method

2. The calling place must introduce FeignClientConfiguration, must have Decoder, Encoder

3. The calling class must be injected into the FeignClient class in the form of a Constructor

4. Pass in the URL as a parameter;

@FeignClient(name = "xxxxClient")
public interface XxxFeignClient {
 
 
    @RequestLine("POST")
    ResponseDto postMethod(URI baseUri, ApproveNotifyDto notifyDto);
 
    @RequestLine("GET")
    ResponseDto getMethod(URI baseUri, XxxDto xxxDto);
  
}
@Slf4j
@Component
@Import(FeignClientsConfiguration.class)
public class CallerService {
 
    private XxxFeignClient xxxFeignClient;
 
    @Autowired
    public CallerService(Decoder decoder, Encoder encoder) {
        xxxFeignClient = Feign.builder()
                //.client(client)
                .encoder(encoder)
                .decoder(decoder)
                .target(Target.EmptyTarget.create(XxxFeignClient.class));
    }
 
    public ResponseDto postMethod(String url, XxxxDto dto) throws URISyntaxException {
        return xxxFeignClient.postMethod(new URI(url), dto);
    }
 
 
    public String test() throws URISyntaxException {
        String url = "http://localhost:30866/";
        return xxxFeignClient.getMethod(new URI(url));
 
    }
 
 
}

Guess you like

Origin blog.csdn.net/weixin_41796956/article/details/127553386