Feign、Hystrix整合
前面的代码,用@HystrixCommand fallbackMethod是很不好的,因为和业务代码耦合度太高,不利于维护,所以需要解耦,这我们讲下Feign Hystrix整合。
1、microservice-student-provider-hystrix项目修改
我们不用原先那套。按照正常的逻辑来写;
StudentService加新的接口方法:
/** * 测试Hystrix服务降级 * @return */ public Map<String,Object> hystrix();
StudentServiceImpl写具体实现:
@Override public Map<String, Object> hystrix() { Map<String,Object> map=new HashMap<String,Object>(); map.put("code", 200); map.put("info","工号【"+port+"】正在为您服务"); return map; }
StudentProviderController正常调用service方法:
/**
* 测试Hystrix服务降级
* @return
* @throws InterruptedException
*/
@ResponseBody
@GetMapping(value="/hystrix")
// @HystrixCommand(fallbackMethod="hystrixFallback")
public Map<String,Object> hystrix() throws InterruptedException{
Thread.sleep(100);
// Map<String,Object> map=new HashMap<String,Object>();
// map.put("code", 200);
// map.put("info","工号【"+port+"】正在为您服务");
return this.studentService.hystrix();
}
// public Map<String,Object> hystrixFallback() throws InterruptedException{
// Map<String,Object> map=new HashMap<String,Object>();
// map.put("code", 500);
// map.put("info", "系统【"+port+"】繁忙,稍后重试");
// return map;
// }
2、microservice-common项目新建FallbackFactory类,解耦服务熔断服务降级
StudentClientService接口,新增hystrix方法;
/** * 服务熔断降级 * @return */ @GetMapping(value="/student/hystrix") public Map<String,Object> hystrix();
新建 StudentClientFallbackFactory 类,实现FallbackFactory<StudentClientService>接口;
package com.javaxl.microservicecommon.service; import com.javaxl.microservicecommon.entity.Student; import feign.hystrix.FallbackFactory; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.List; import java.util.Map; @Component public class StudentClientFallbackFactory implements FallbackFactory<StudentClientService> { @Override public StudentClientService create(Throwable cause) { return new StudentClientService() { @Override public boolean save(Student student) { return false; } @Override public List<Student> list() { return null; } @Override public Map<String, Object> hystrix() { Map<String,Object> map=new HashMap<String,Object>(); map.put("code", 500); map.put("info", "系统繁忙,稍后重试"); return map; } @Override public Student get(Integer id) { return null; } @Override public boolean delete(Integer id) { return false; } @Override public String ribbon() { return null; } }; } }
StudentClientService接口的@FeignClient注解加下 fallbackFactory属性
@FeignClient(value="MICROSERVICE-STUDENT",fallbackFactory=StudentClientFallbackFactory.class)
这类我们实现了 降级处理方法实现;
3、microservice-student-consumer-feign-80修改 支持Hystrix
StudentConsumerFeignController新增方法调用
/** * Feign整合Hystrix服务熔断降级 * @return * @throws InterruptedException */ @GetMapping(value="/hystrix") public Map<String,Object> hystrix() throws InterruptedException{ return studentClientService.hystrix(); }
4、microservice-student-consumer-feign-80的application.yml加上hystrix支持
feign: hystrix: enabled: true
5、microservice-student-consumer-feign-80的启动类上添加公共模块
@ComponentScan(basePackages = {"com.javaxl.microservicecommon","com.javaxl.microservicestudentconsumerfeign80"})
注意:
u 公共子项目与当前子项目的基包都要扫描到;
u 只指定公共子模块为基包会导致本子项目的springmvc功能失效;
u 只指定本子项目为基包会导致feign与Hystrix集成失败,从而导致服务熔断功能失效
package com.javaxl.microservicestudentconsumerfeign80;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = {"com.javaxl.microservicecommon","com.javaxl.microservicestudentconsumerfeign80"})//扫描公共模块
@EnableFeignClients(value = "com.javaxl.*.*")
@EnableEurekaClient
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class MicroserviceStudentConsumerFeign80Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentConsumerFeign80Application.class, args);
}
}
测试开启三个eureka,以及带hystrix的provider,和带feign,hystrix的consummer。
测试的话,也是没问题的。0.9秒的话,返回正常信息;超过1秒的话,就返回错误提示;
超时设置
上面错误是什么原因呢,咱们明明在Hystrix中的application.yml中设置了
hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 3000
这里因为还有一个 feign 也有一个超时时间的设置,当然feign底层是 ribbon的封装,所以 直接配置ribbon,ribbon默认超时也是1秒。
所以这里都是强制要求,ribbon的超时时间要大于hystrix的超时时间,否则 hystrix自定义的超时时间毫无意义。
所以还得microservice-student-consumer-feign-80上加个 ribbon超时时间设置
ribbon: ReadTimeout: 10000 ConnectTimeout: 9000
这样就完工了。可以自行测试。
备案号:湘ICP备19000029号
Copyright © 2018-2019 javaxl晓码阁 版权所有