当前位置: 首页 > news >正文

SpringCloud-Gateway无法使用Feign服务(2021.X版本)

Spring Cloud Gateway 2021.x版本,无法使用Feign调用其他服务接口。

问题原因:

在官网的 issue 里面找到了相关的问题。

How to call another micro-service on GatewayFilterFactory ? · Issue #1090 · spring-cloud/spring-cloud-gateway · GitHubHello, Can you tell me how to call another micro-service on a GatewayFilterFactory? I want to call the authentication micro-service when a request come from user. 1st, I tried to use feign client. but I can not get the ServletRequestAttr...icon-default.png?t=N7T8https://github.com/spring-cloud/spring-cloud-gateway/issues/1090

 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://";@Autowiredprivate WebClient.Builder webBuilder;public String getAesKeyByAppId(String appId) {JSONObject jsonObject = new JSONObject();jsonObject.put("appId", appId);try {Mono<String> 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;}}

http://www.lryc.cn/news/223888.html

相关文章:

  • 基于SSM的建筑装修图纸管理平台
  • Apache Doris (五十二): Doris Join类型 - Broadcast Join
  • Docker从入门到上天系列第四篇:docker平台入门图解与平台架构图解
  • 安全防御——四、防火墙理论知识
  • 如何给PPT幻灯片解除密码保护以防止编辑
  • 在linux安装单机版hadoop-3.3.6
  • Hadoop相关
  • ArcGIS 气象风场等示例 数据制作、服务发布及前端加载
  • 【Axure高保真原型】树切换动态面板案例
  • 安装pr提示VCRUNTIME140.dll丢失的修复方法,3个有效的方法
  • Linux进程控制(2)
  • Android Glide transform旋转rotate圆图CircleCrop,Kotlin
  • 如何让群晖Audio Station公开共享的本地音频公网可访问?
  • 生态环境领域基于R语言piecewiseSEM结构方程模型
  • spring boot+netty 搭建MQTT broken
  • 从零开始搭建React+TypeScript+webpack开发环境-使用iconfont构建图标库
  • 微服务之初始微服务
  • 大口径智能水表支持最高水流量是多少?
  • 在Spring Boot中使用MyBatis访问数据库
  • 懒羊羊闲话2
  • 多路转接(上)——select
  • 基于SSM的图书管理借阅系统设计与实现
  • Python的内存优化
  • 蓝桥杯-回文日期[Java]
  • acwing算法基础之搜索与图论--树与图的遍历
  • 前端uniapp请求真是案例(带源码)
  • MySQL -- mysql connect
  • 如何用AI帮你下载安卓源码
  • 第三章:人工智能深度学习教程-基础神经网络(第三节-Tensorflow 中的多层感知器学习)
  • Python的版本如何查询?