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

@FeignClient用于Nacos微服务间的接口调用

依赖:
<!-- spring-boot启动依赖 -->
<!-- 提供者 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- openFeign -->
<!-- 消费者-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

yml配置

feign:compression:response:enabled: truerequest:enabled: truemime-types: text/xml,application/xml,application/jsonmin-request-size: 2048circuitbreaker:enabled: trueclient:config:default:connectTimeout: 5000readTimeout: 5000loggerLevel: basic

提供者创建RESTful接口,controller接口 @RestController @GetMapping("/url")

消费者创建feign目录,创建Interface ManagementClient

//name 填写
//spring:
//application:
//  name: management
//springboot的服务名
//fallback填写实现类,用于接口回调,接口异常时返回保底数据
@FeignClient(name = "management", fallback = ManagementClientFallback.class)
public interface ManagementClient {@PostMapping("/url")OperaResponse selectList(@RequestBody IdRequest request);
}

/feign/impl,创建ManagementClientFallback类

/*** fallback是在远程服务调用失败时,向调用方返回一个备用(回退)响应的机制*/
@Component
public class ManagementClientFallback implements ManagementClient {@Overridepublic OperaResponse selectList(IdRequest request) {return OperaResponse.error(ErrStatus.FEIGN_ERROR);}
}

创建ClientUtil用于调用Client方法,service层依赖注入Client,将Client对象和参数传给ClientUtil方法

    @Autowiredprivate ChannelManagementClient channelManagementClient;
public class ClientUtil {private ClientUtil(){}public static List<Response> selectList(ManagementClient client, Integer Id){IdRequest request = new IdRequest();request.setId(id);OperaResponse operaResponse = client.selectList(request);if(operaResponse.getData() == null){return new ArrayList<>();}List<Response> list = JSONObject.parseArray(JSONObject.toJSONString(operaResponse.getData()), Response.class);return list == null ? new ArrayList<>() : list;}
}

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

相关文章:

  • 认识javascript中的模块化
  • 容器设计模式:Sidecar
  • ensp 静态路由配置
  • MATLAB图卷积神经网络GCN处理分子数据集节点分类研究
  • Android-Glide详解
  • 2.Nuxt学习 组件使用和路由跳转相关
  • 代码开发相关操作
  • 动态导出word文件支持转pdf
  • 登陆harbor发现证书是错误的, 那么如何更新harbor的证书呢
  • 【Leetcode Top 100】199. 二叉树的右视图
  • React自学:如何使用localStorage,以及如何实现删除笔记操作
  • go语言使用websocket发送一条消息A,持续接收返回的消息
  • 如何对小型固定翼无人机进行最优的路径跟随控制?
  • C++常见面试题-初级2
  • Spring Security 6 系列之二 - 基于数据库的用户认证和认证原理
  • mfc140.dll是什么东西?mfc140.dll缺失的几种具体解决方法
  • 【STM32 Modbus编程】-作为主设备写入多个线圈和寄存器
  • Windows安全中心(病毒和威胁防护)的注册
  • 微积分复习笔记 Calculus Volume 2 - 4.2 Direction Fields and Numerical Methods
  • 深入理解旋转位置编码(RoPE)及其在大型语言模型中的应用
  • 内网穿透的应用-在OpenWrt上轻松搭建SFTP服务,安全传输文件不再难!
  • 【图像处理lec3、4】空间域的图像增强
  • 【算法day13】二叉树:递归与回溯
  • 上海亚商投顾:创业板指缩量下跌 多只高位股午后跌停
  • 单步调试Android Framework——App冷启动
  • 统计一个目录下的文件及目录数量-linux010
  • spring RestTemplate使用说明
  • thinkphp:try-catch捕获异常
  • shardingsphere分库分表跨库访问 添加分片规则
  • c++:std::map下标运算符的不合理使用