标签:ppi 需要 cti new get bat factory gre ogg
1.第一步,新建两个简单的springboot项目并创建rest接口
demo系统的rest接口
plus系统的调用接口
2.在项目pom文件里导入feign和hystrix的pom依赖包
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<!-- 支持使用feign客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 支持使用hystrix断路器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3.创建远程调用client及熔断回调类
package com.mybatis.plus.web.client; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient(name="demo",url="${conf.address.demo}",fallbackFactory=DemoFeignClientFB.class) public interface DemoFeignClient { @RequestMapping("cmpOclOrder/demoFeign") public String testFeign(); }
package com.mybatis.plus.web.client; import feign.hystrix.FallbackFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Component; @Component public class DemoFeignClientFB implements FallbackFactory<DemoFeignClient> { private static Log logger = LogFactory.getLog(DemoFeignClientFB.class); @Override public DemoFeignClient create(Throwable throwable) { return new DemoFeignClient() { @Override public String testFeign() { return "demo系统接口调用失败"; } }; } }
这里的url在配置文件里面有定义(就是需要调用系统的IP及端口)
如果要支持熔断(如果调用远程接口报错,远程接口就返回一个固定的值)需要在配置文件里启用hystrix
4.在启动类上加上支持feign的注解就ok了(这里basePackages值是定义client类的包路径)
5.测试效果展示
调用plus系统的plusTest接口
demo系统接口时报错(修改代码-重启)
返回固定的值
以上就是简单的feign+hystrix的应用
springBoot使用feign实现远程接口调用和错误熔断
标签:ppi 需要 cti new get bat factory gre ogg
原文地址:https://www.cnblogs.com/guanxiaohe/p/11989223.html