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

spring openfeign

pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>dr.cqr</groupId><artifactId>demor</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><java.version>1.8</java.version></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.13.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.2.2.RELEASE</version></dependency></dependencies><dependencyManagement></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
APPApplication
package dr.cqr;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RestController;@SpringBootApplication
@RestController
// 它告诉 Spring 容器需要启用 Feign 客户端的功能,
// 并且会扫描项目中的 Feign 客户端接口,将其注册为 Spring Bean,
// 以便后续在项目中可以自动装配和使用这些 Feign 客户端。
// Feign默认情况下是启用重试机制的,会自动重新发送请求,最多尝试5次‌
@EnableFeignClients
public class APPApplication {public static void main(String[] args) {SpringApplication.run(APPApplication.class, args);System.out.println("################### 启动成功 ##################");}
}
feign
package dr.cqr.feign;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;@FeignClient(name="test-name", url="http://192.168.1.2:8090", configuration = TestErrorDecoder.class)
public interface TestClient {@GetMapping("/api/qing")Object getQing(@RequestParam(required = false, name="name") String name);
}
Controller
package dr.cqr.controller;import dr.cqr.feign.TestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.Optional;@RestController
@RequestMapping("/test")
public class Test {@Autowiredprivate TestClient testClient;@GetMapping("/t")public String tt () {Object o = testClient.getQing(null);System.out.println(o);return "ok";}
}
package dr.cqr.feign;import feign.FeignException;
import feign.Response;
import feign.codec.ErrorDecoder;public class TestErrorDecoder implements ErrorDecoder {@Overridepublic Exception decode(String methodKey, Response response) {System.out.println("methodKey " + methodKey);// 根据响应状态码和响应内容判断异常类型,并返回对应的自定义异常if (response.status() == 404) {System.out.println("11");} else if (response.status() == 500) {System.out.println("22");}// 返回默认异常return FeignException.errorStatus(methodKey, response);}
}

http://www.xdnf.cn/news/735193.html

相关文章:

  • AWS云创建安全审计用户组
  • TIDB创建索引失败 mkdir /tmp/tidb/tmp_ddl-4000/1370: no such file or directory.
  • NHANES指标推荐:CQI
  • ESP32对接巴法云实现配网
  • 线路板厂家遇到的PCB元件放置的常见问题有哪些?
  • 内核进程基础
  • 界面控件DevExpress WinForms中文教程:Banded Grid View - 如何固定Bands?
  • 《 PyTorch 2.3革新:torch.compile自动生成CUDA优化内核全解》
  • 鸿蒙OSUniApp页面切换动效实战:打造流畅精致的转场体验#三方框架 #Uniapp
  • Go语言结构体:数据组织的艺术
  • 网络犯罪分子利用虚假ChatGPT安装程序实施攻击
  • 【Go语言】Fyne GUI 库使用指南 (面向有经验开发者)
  • XUANYING炫影-移动版-智能轻云盒SY900Pro和SY910_RK3528芯片_免拆机通刷固件包
  • PHP中文网文章内容提取免费API接口教程
  • JavaScript中的命名导出(暴露)
  • yolov8添加注意力机制
  • 避免空值判断
  • Fluence (FLT) 2026愿景:RWA代币化加速布局AI算力市场
  • 一、Python 常用内置工具(函数、模块、特性)的汇总介绍和完整示例
  • Go 中 `json.NewEncoder/Decoder` 与 `json.Marshal/Unmarshal` 的区别与实践
  • C++学习-入门到精通【10】面向对象编程:多态性
  • LangChain表达式 (LCEL)
  • C语言实现对哈希表的操作:插入新键值对与删除哈希表中键值对
  • 哪些岗位最易被AI替代?
  • Docker设置代理
  • ros2工程在普通用户下正常编译但root下编译无法成功也不会自动停止
  • RAG混合检索:倒数秩融合RRF算法
  • 零硬件成本玩转嵌入式通信!嵌入式仿真实验教学平台解锁STM8S串口黑科技
  • 对COM组件的调用返回错误 HRESULT E_FAIL
  • Linux操作系统之进程(四):命令行参数与环境变量