Feign 调用第三方接口示例,FeignClient参数动态配置url

Feign是一个灵感来自于Retrofit、JAXRS-2.0、WebSocket的Java Http客户端,Feign的主要目标是降低大家使用Http API的复杂性”。

其实,Feign底层依赖于Java的动态代理机制,对原生Java Socket或者Apache HttpClient进行封装,实现了基于Http协议的远程过程调用。当然,Feign还在此基础上实现了负载均衡、熔断等机制。

注解@FeignClient

动态url,则用url = ${}即可,需要注意的是,配置文件需在启动类所在的resource目录下!

// 注意:这里的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);

注解@RequestLine

 使用步骤        

1.  FeignClient 中不要写url, 使用 @RequestLine修饰方法

2. 调用地方必须引入  FeignClientConfiguration, 必须有Decoder, Encoder

3. 调用类必须以构建函数(Constructor) 的方式注入 FeignClient 类

4. 传入URL作为参数;

@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));
 
    }
 
 
}

猜你喜欢

转载自blog.csdn.net/weixin_41796956/article/details/127553386
今日推荐