Feign がサードパーティ インターフェイス、FeignClient パラメーターの動的構成 URL を呼び出す例

Feign は、Retrofit、JAXRS-2.0、および WebSocket からインスピレーションを得た Java Http クライアントです。Feign の主な目標は、すべての人が Http API を使用する際の複雑さを軽減することです。」

実際、Feign の最下層は、ネイティブ Java ソケットまたは Apache HttpClient をカプセル化し、Http プロトコルに基づいてリモート プロシージャ コールを実装する Java の動的プロキシ メカニズムに依存しています。もちろん、Feign はこれに基づいて負荷分散や融合などのメカニズムも実装します。

注釈 @FeignClient

動的 URL の場合は、url = ${} を使用します。構成ファイルは、スタートアップ クラスが配置されているリソース ディレクトリに存在する必要があることに注意してください。

// 注意:这里的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. 呼び出しクラスは、コンストラクターの形式で 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