Spring Boot简单整合Open Feign

一、使用Open Feign

1、引入依赖

<dependencies>  
       <!--open feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <!--可以删除version这一行,版本交给父依赖托管-->
            <version>2.2.1.RELEASE</version>
        </dependency>
        
        <!--hystrix 豪猪哥,服务熔断-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <!--可以删除version这一行,版本交给父依赖托管-->
            <version>2.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            <!--可以删除version这一行,版本交给父依赖托管-->
            <version>2.2.1.RELEASE</version>
        </dependency>
		
		 <!-- 配置这两个httpclient,目的是后续用对象的形式调用Get请求 -->
		 <!-- 使用Apache HttpClient替换Feign原生httpclient -->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-httpclient</artifactId>
        </dependency>
		 <!-- 配置feign 发送请求使用 httpclient,而不是java原生 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
</dependencies>

2、添加Open Feign

在项目的启动类上添加Open Feign的启动注解==@EnableFeignClients和服务熔断的注解@EnableHystrix==
例如,第三行第四行

@SpringBootApplication()
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class ClassesApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClassesApplication.class, args);
    }
}

3、添加配置文件application.yml

在服务调用方配置文件如下

#feign客户端配置
feign:
  #设置feign开启hystrix
  hystrix:
    enabled: true
  #适配feign发送get请求
  httpclient:
    enabled: true
#ribbon配置
ribbon:
  #ribbon的超时时间要大于hystrix的超时时间,否则 hystrix自定义的超时时间毫无意义
  ReadTimeout: 5000
  ConnectTimeout: 5000
#hystrix配置
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            #feign整合hystrix 光设置Hystrix 超时没用的要配合ribbon超时
            timeoutInMilliseconds: 3000
      circuitBreaker:
        #默认20 ,熔断的阈值,如何user服务报错满足3次,熔断器就会打开,就算order之后请求正确的数据也不行。
        requestVolumeThreshold: 3
        #默认5S , 等5S之后熔断器会处于半开状态,然后下一次请求的正确和错误讲决定熔断器是否真的关闭和是否继续打开
        sleepWindowInMilliseconds: 8000

二、Open Feign的调用

假设当前场景有班级和学生两个对象,两个对象分别在ClassesApplication和SudentApplication两个服务中,现要求学生服务为班级服务提供查询所有学生的接口

1、模拟一个服务的提供者(假设为student)

1、学生服务的启动类,服务地址为localhost:9001,服务名称为service-student

@SpringBootApplication()
@EnableDiscoveryClient
@EnableFeignClients
public class SudentApplication{
    public static void main(String[] args) {
        SpringApplication.run(SudentApplication.class, args);
}

2、学生服务查询所有学生的接口

@RestController
@RequestMapping(value = "/api/v1/student")
public class StudentController{

	//假设为学生接口服务层
	@Autowired
	private StudentService studentService;
	
	//假设为查询所有学生的接口
	@RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Student> findAll() {
       return student.findAll();
    }
}

2、模拟一个服务的调用者(假设为classes)

1、班级服务的启动类,服务地址为localhost:9002,服务名称为service-classes

@SpringBootApplication()
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class ClassesApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClassesApplication.class, args);
    }
}

2、创建服务调用service

@FeignClient(name = "service-student", url = "localhost:9001/api/v1/student", fallback = "StudentFeignFallBack.class)
@Componet
public interface StudentFeignService{
	//复制StudentController的查询所有学生的接口
	@RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Student> findAll();
}

@FeignClient即是我们常说的Feign注释,其中,name为调用的服务的服务名称,url为调用服务的服务地址,fallback为调用StudentFeignService方法失败后用来兜底的类

3、创建服务熔断类(兜底)

@Component
public class StudentFeignFallBack implements StudentFeignService{
	 /**
     * 日志工具
     */
    private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	 public List<Student> findAll(){
		logger.error("获取学生列表失败", e);
		retun null;
	}
}

兜底类实现了调用类的方法,当Feign调用不通时就会执行兜底类的兜底方法。
自此,Spring Boot简单整合Open Feig告一段落。


参考:springboot整合hystrix和feign

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值