💗wei_shuo的个人主页
💫wei_shuo的学习社区
🌐Hello World !
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:8701spring:application:name: feign-service eureka:client:register-with-eureka:truefetch-registry:trueservice-url:defaultZone: http://localhost:8001/eureka/
- 启动类上添加@EnableFeignClients注解来启用Feign的客户端功能
@EnableFeignClients@EnableDiscoveryClient@SpringBootApplicationpublicclassFeignServiceApplication{publicstaticvoidmain(String[] args){SpringApplication.run(FeignServiceApplication.class, args);}}
- 添加UserService接口完成对user-service服务的接口绑定
@FeignClient(value ="user-service")publicinterfaceUserService{@PostMapping("/user/create")CommonResultcreate(@RequestBodyUser user);@GetMapping("/user/{id}")CommonResult<User>getUser(@PathVariableLong id);}
- 添加UserFeignController调用UserService实现服务调用
@RestController@RequestMapping("/user")publicclassUserFeignController{@AutowiredprivateUserService userService;@PostMapping("/create")publicCommonResultcreate(@RequestBodyUser user){return userService.create(user);}@GetMapping("/{id}")publicCommonResultgetUser(@PathVariableLong id){return userService.getUser(id);}}
Feign中的服务降级
微服务架构中,服务降级是一种应对高并发或系统故障的策略,用于保证系统的可用性和稳定性;Feign是一个声明式的Web服务客户端,可以与Spring Cloud等微服务框架集成,简化服务之间的调用
- 添加服务降级实现类UserFallbackService
@ComponentpublicclassUserFallbackServiceimplementsUserService{@OverridepublicCommonResultcreate(User user){User defaultUser =newUser(-1L,"defaultUser","123456");returnnewCommonResult<>(defaultUser);}@OverridepublicCommonResult<User>getUser(Long id){User defaultUser =newUser(-1L,"defaultUser","123456");returnnewCommonResult<>(defaultUser);}@OverridepublicCommonResult<User>getByUsername(String username){User defaultUser =newUser(-1L,"defaultUser","123456");returnnewCommonResult<>(defaultUser);}@OverridepublicCommonResultupdate(User user){returnnewCommonResult("调用失败,服务被降级",500);}@OverridepublicCommonResultdelete(Long id){returnnewCommonResult("调用失败,服务被降级",500);}}
- 修改UserService接口,设置服务降级处理类为UserFallbackService
@FeignClient(value ="user-service",fallback =UserFallbackService.class)publicinterfaceUserService{}
- 修改application.yml,开启Hystrix功能
feign:hystrix:enabled:true#在Feign中开启Hystrix
日志打印功能
Feign提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解Feign中Http请求的细节
日志级别
- NONE:默认的,不显示任何日志
- BASIC:仅记录请求方法、URL、响应状态码及执行时间
- HEADERS:除了BASIC中定义的信息之外,还有请求和响应的头信息
- FULL:除了HEADERS中定义的信息之外,还有请求和响应的正文及元数据
通过配置开启更为详细的日志
- 通过java配置来使Feign打印最详细的Http请求日志信息
@ConfigurationpublicclassFeignConfig{@BeanLogger.LevelfeignLoggerLevel(){returnLogger.Level.FULL;}}
- application.yml中配置需要开启日志的Feign客户端;配置UserService的日志级别为debug
logging: level:com.macro.cloud.service.UserService: debug
Feign的常用配置
feign:
hystrix:
enabled:true #在Feign中开启Hystrix
compression:
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
🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——
点赞
👍
收藏
⭐️
评论
📝
版权归原作者 wei_shuo 所有, 如有侵权,请联系我们删除。