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);}
}