转自:
http://www.java265.com/JavaFramework/SpringBoot/202205/3532.html
下文笔者讲述SpringBoot集成feign的方法分享,如下所示
实现思路: 1.引入相应的jar包 2.SpringBoot中加入EnableFeignClients注解
例:
引入jar包 <dependency> <groupId>com.netflix.feign</groupId> <artifactId>feign-httpclient</artifactId> <version>8.18.0</version> </dependency> <dependency> <groupId>com.netflix.feign</groupId> <artifactId>feign-core</artifactId> <version>8.18.0</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.4.RELEASE</version> </dependency> ---在启动类中加入默认启动feign相关 @EnableFeignClients 3.写接入的公共接口类 @FeignClient(name = "api",url = "http://域名/api") public interface CommonSwitch { //对应的服务里的接口地址,及请求方式 @RequestMapping(value = "/express_v1/index", method = RequestMethod.POST) @ResponseBody String feignString(@RequestParam(value="page") Integer page); /** * 容错处理类,当调用失败时,简单返回空字符串 */ @Component public class DefaultFallback implements CommonSwitch { @Override public String feignString(@RequestParam(value="page") Integer page){ return ""; } } } FeignClient参数说明: name:指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务的名称,用于服务发现 url: url一般用于调试,可以手动指定@FeignClient调用的地址 decode404:当发生http 404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignException configuration: Feign配置类,可以自定义Feign的Encoder、Decoder、LogLevel、Contract fallback: 定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口 fallbackFactory: 工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑,减少重复的代码 path: 定义当前FeignClient的统一前缀 4.对外的接口(也就是controller) @Api(tags = "feign集成测试") @RestController @RequestMapping("/feigntest") @Slf4j @CrossOrigin(origins = "*", maxAge = 3600) public class FeigntestController { @Autowired private CommonSwitch commonSwitch; /** * @param page * @return */ @ApiOperation(value = "json测试") @RequestMapping(value="/feignTest.json", method= RequestMethod.GET) public String feignTest(@RequestParam(name = "page") int page) { String tteString = commonSwitch.feignString(page); return tteString; } }标签:集成,FeignClient,feign,SpringBoot,value,page,public From: https://www.cnblogs.com/javalove2022/p/16821019.html