当前位置: 首页 > 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);}
}

相关文章:

  • Java中hashCode()与equals()的常见错误及解决方案
  • JS入门——三种输入方式
  • 超低延迟与高稳定性的行业领先直播解决方案
  • python里的Matplotlib库
  • 亚马逊商品评论爬取与情感分析:Python+BeautifulSoup实战(含防封策略)
  • 智绅科技——科技赋能健康养老,构建智慧晚年新生态
  • SpringAI系列 - 升级1.0.0
  • SpringBatch+Mysql+hanlp简版智能搜索
  • android协程异步编程常用方法
  • MySQL之数据库的内嵌函数和联合查询
  • 广东省林学会新办林业造林资质具体条件?
  • 1-1 初探Dart编程语言
  • Augment vs Cursor:当Cursor解决不了问题时的最佳补充方案
  • AI+爆款文案,提示词脚本 ——卫朋
  • Linux(9)——进程(控制篇——下)
  • 电磁器件的“折纸革命“:牛津《Sci. Reports》发布剪纸超材料
  • gitlib 常见命令
  • C++写入CSV的操作读取、写入、追加以及文件删除
  • Unsafe.putOrderedInt与Volatile
  • 软件需求文档如何做精确度度量
  • 婚纱网站设计/游戏推广公司怎么接游戏的
  • 国际新闻界期刊/谷歌搜索引擎优化seo
  • 动漫网站开发设计思想/游戏推广员
  • 滨海做网站的价格/网站维护中
  • 织梦企业 网站源码/色盲
  • 网站建设板块免费下载/企业如何开展网络营销