Spring Cloud OpenFeign:基于Ribbon和Hystrix的声明式服务调用
Spring Cloud OpenFeign是一个声明式的服务调用框架,基于Feign并整合了Ribbon和Hystrix;目标是简化分布式系统中编写服务间调用的代码,并提供一种更加优雅和便捷的方式来进行服务之间的通信
创建feign-service模块
- 依赖导入
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix -eureka-client</artifactId> </dependency> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency>
- application.yml配置
server:port: 8701 spring:application:name: feign-service eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://localhost:8001/eureka/
- 启动类上添加@EnableFeignClients注解来启用Feign的客户端功能
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class FeignServiceApplication {public static void main(String[] args) {SpringApplication.run(FeignServiceApplication.class, args);} }
- 添加UserService接口完成对user-service服务的接口绑定
@FeignClient(value = "user-service") public interface UserService {@PostMapping("/user/create")CommonResult create(@RequestBody User user);@GetMapping("/user/{id}")CommonResult<User> getUser(@PathVariable Long id); }
- 添加UserFeignController调用UserService实现服务调用
@RestController @RequestMapping("/user") public class UserFeignController {@Autowiredprivate UserService userService;@PostMapping("/create")public CommonResult create(@RequestBody User user) {return userService.create(user);}@GetMapping("/{id}")public CommonResult getUser(@PathVariable Long id) {return userService.getUser(id);} }
Feign中的服务降级
微服务架构中,服务降级是一种应对高并发或系统故障的策略,用于保证系统的可用性和稳定性;Feign是一个声明式的Web服务客户端,可以与Spring Cloud等微服务框架集成,简化服务之间的调用
- 添加服务降级实现类UserFallbackService
@Component public class UserFallbackService implements UserService {@Overridepublic CommonResult create(User user) {User defaultUser = new User(-1L, "defaultUser", "123456");return new CommonResult<>(defaultUser);}@Overridepublic CommonResult<User> getUser(Long id) {User defaultUser = new User(-1L, "defaultUser", "123456");return new CommonResult<>(defaultUser);}@Overridepublic CommonResult<User> getByUsername(String username) {User defaultUser = new User(-1L, "defaultUser", "123456");return new CommonResult<>(defaultUser);}@Overridepublic CommonResult update(User user) {return new CommonResult("调用失败,服务被降级",500);}@Overridepublic CommonResult delete(Long id) {return new CommonResult("调用失败,服务被降级",500);} }
- 修改UserService接口,设置服务降级处理类为UserFallbackService
@FeignClient(value = "user-service",fallback = UserFallbackService.class) public interface UserService { }
- 修改application.yml,开启Hystrix功能
feign:hystrix:enabled: true #在Feign中开启Hystrix
日志打印功能
Feign提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解Feign中Http请求的细节
日志级别
- NONE:默认的,不显示任何日志
- BASIC:仅记录请求方法、URL、响应状态码及执行时间
- HEADERS:除了BASIC中定义的信息之外,还有请求和响应的头信息
- FULL:除了HEADERS中定义的信息之外,还有请求和响应的正文及元数据
通过配置开启更为详细的日志
- 通过java配置来使Feign打印最详细的Http请求日志信息
@Configuration public class FeignConfig {@BeanLogger.Level feignLoggerLevel() {return Logger.Level.FULL;} }
- application.yml中配置需要开启日志的Feign客户端;配置UserService的日志级别为debug
logging:level:com.macro.cloud.service.UserService: debug
Feign的常用配置
feign:hystrix:enabled: true #在Feign中开启Hystrixcompression:request:enabled: false #是否对请求进行GZIP压缩mime-types: text/xml,application/xml,application/json #指定压缩的请求数据类型min-request-size: 2048 #超过该大小的请求会被压缩response:enabled: false #是否对响应进行GZIP压缩
logging:level: #修改日志级别com.macro.cloud.service.UserService: debug