Feign 简介
Feign 是对 Hystrix 和 Ribbon 的封装,简便了代码的编写,代价是对服务的性能有一定的影响,在并发量非常高的场景下不建议使用。(用 Hystrix 的场景并发量能不高么?所以基本上就是不建议使用)
配置与使用
服务发现方
依赖配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
启动类配置
//开启feign支持,clients指定哪个类开启feign
@EnableFeignClients(clients = {StudentService.class})
配置文件配置
#feign开启熔断器
feign.hystrix.enabled=true
#开启feign的压缩功能
feign.compression.request.enabled=true
#feign.compression.request.mime-types=text/xml,application/xml,application/json
#feign.compression.request.min-request-size=2048
feign.compression.response.enabled=true
#另外还可以配置关于hystrix的配置,如
#hystrix.command.default.execution.isolation.strategy=SEMAPHORE
接口配置
/*
* fallback = StudentServiceFallback.class
* 不能获取具体异常
*
* */
@FeignClient(name = "MICRO-ORDER",path = "/feign"
/*fallback = StudentServiceFallback.class,*/
,fallbackFactory = StudentServiceFallbackFactory.class)
public interface StudentService {
//相当于每一个方法上都加上了@Hystrix
@GetMapping("/student/getAllStudent")
String getAllStudent();
//入参为pojo,@RequestBody 不能丢
@PostMapping("/student/saveStudent")
String saveStudent(@RequestBody Student student);
//入参为基本数据类型/String,@RequestParam 不能丢
@GetMapping("/student/getStudentById")
String getStudentById(@RequestParam("id") Integer id);
}
服务降级
@Slf4j
@Component
public class StudentServiceFallbackFactory implements FallbackFactory<StudentService> {
@Override
public StudentService create(Throwable throwable) {
if(throwable == null) {
return null;
}
final String msg = throwable.getMessage();
log.info("exception:" + msg);
return new StudentService() {
@Override
public String getAllStudent() {
log.info("exception=====getAllStudent==========" + msg);
return msg;
}
@Override
public String saveStudent(Student student) {
log.info("exception=====saveStudent==========" + msg);
return msg;
}
@Override
public String getStudentById(Integer id) {
log.info("exception=====getStudentById==========" + msg);
return msg;
}
};
}
}
异常信息过滤器
@Configuration
public class FeignErrMessageFilter {
@Bean
public ErrorDecoder errorDecoder() {
return new FeignErrorDecoder();
}
/*
* 当调用服务时,如果服务返回的状态码不是200,就会进入到Feign的ErrorDecoder中
* 只有这种方式才能获取所有的被feign包装过的异常信息
*
* 这里如果创建的Exception是HystrixBadRequestException
* 则不会走熔断逻辑,不记入熔断统计
* */
class FeignErrorDecoder implements ErrorDecoder {
private Logger logger = LoggerFactory.getLogger(FeignErrorDecoder.class);
@Override
public Exception decode(String s, Response response) {
RuntimeException runtimeException = null;
try {
String retMsg = Util.toString(response.body().asReader());
logger.info(retMsg);
runtimeException = new RuntimeException(retMsg);
} catch (IOException e) {
e.printStackTrace();
}
return runtimeException;
}
}
}
服务提供方
服务接口与实现类
-
接口:有返回值的方法只能返回String类型。
远程调用会传输序列化的Json格式的字符串。 -
实现类:注意返回值转换成Json格式的字符串。
可以考虑用 alibaba 的 fastjson,用 JSONObject.toJSONString() 包裹 pojo。
Controller
@RestController
public class StudentController implements StudentService {
@Autowired
private StudentService studentService;
@RequestMapping("/feign/student/getAllStudent")
@Override
public String getAllStudent() {
return studentService.getAllStudent();
}
@RequestMapping("/feign/student/getStudentById")
@Override
public String queryStudentById(@RequestParam("id") Integer id) {
return studentService.queryStudentById(id);
}
@RequestMapping("/feign/student/saveStudent")
@Override
public String saveStudent(@RequestBody Student student) {
return studentService.saveStudent(student);
}
}