Spring Cloud Gateway 2021.x版本,无法使用Feign调用其他服务接口。
问题原因:
在官网的 issue 里面找到了相关的问题。
Spring Cloud Gateway 2021.x版本 基于WebFlux实现,使用webclient 替换 feign。
使用案例:
- import com.alibaba.fastjson.JSONObject;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.MediaType;
- import org.springframework.stereotype.Component;
- import org.springframework.web.reactive.function.client.WebClient;
- import reactor.core.publisher.Mono;
-
- /**
- * @Author: meng
- * @Description: 权限工具类
- * @Date: 2023/8/3 15:01
- * @Version: 1.0
- */
- @Component
- public class AuthUtils {
-
- private static Logger logger = LoggerFactory.getLogger(AuthUtils.class);
-
- public final static String LB = "lb://";
-
- @Autowired
- private WebClient.Builder webBuilder;
-
- public String getAesKeyByAppId(String appId) {
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("appId", appId);
- try {
- Mono
result = webBuilder.baseUrl(LB + "服务名称") - .build()
- .post()
- .uri("uri")
- .contentType(MediaType.APPLICATION_JSON)
- .header(HttpHeaders.AUTHORIZATION, "token")
- .bodyValue(jsonObject)
- .retrieve()
- .bodyToMono(String.class);
- String body = result.toFuture().get();
- logger.info("body:{}", body);
- // 处理逻辑
- ...
- }
- catch (Exception e) {
- logger.error("Exception:{}", e);
- }
- return null;
- }
-
- }