SpringBoot實戰電商項目mall(20k+star)地址: https://github.com/macrozheng/mall
Spring Cloud OpenFeign 是聲明式的服務調用工具,它整合了Ribbon和Hystrix,擁有負載均衡和服務容錯功能,本文將對其用法進行詳細介紹。java
Feign是聲明式的服務調用工具,咱們只需建立一個接口並用註解的方式來配置它,就能夠實現對某個服務接口的調用,簡化了直接使用RestTemplate來調用服務接口的開發量。Feign具有可插拔的註解支持,同時支持Feign註解、JAX-RS註解及SpringMvc註解。當使用Feign時,Spring Cloud集成了Ribbon和Eureka以提供負載均衡的服務調用及基於Hystrix的服務容錯保護功能。git
這裏咱們建立一個feign-service模塊來演示feign的經常使用功能。
<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>
server: port: 8701 spring: application: name: feign-service eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8001/eureka/
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class FeignServiceApplication { public static void main(String[] args) { SpringApplication.run(FeignServiceApplication.class, args); } }
咱們經過@FeignClient註解實現了一個Feign客戶端,其中的value爲user-service表示這是對user-service服務的接口調用客戶端。咱們能夠回想下user-service中的UserController,只需將其改成接口,保留原來的SpringMvc註釋便可。
/** * Created by macro on 2019/9/5. */ @FeignClient(value = "user-service") public interface UserService { @PostMapping("/user/create") CommonResult create(@RequestBody User user); @GetMapping("/user/{id}") CommonResult<User> getUser(@PathVariable Long id); @GetMapping("/user/getByUsername") CommonResult<User> getByUsername(@RequestParam String username); @PostMapping("/user/update") CommonResult update(@RequestBody User user); @PostMapping("/user/delete/{id}") CommonResult delete(@PathVariable Long id); }
/** * Created by macro on 2019/8/29. */ @RestController @RequestMapping("/user") public class UserFeignController { @Autowired private UserService userService; @GetMapping("/{id}") public CommonResult getUser(@PathVariable Long id) { return userService.getUser(id); } @GetMapping("/getByUsername") public CommonResult getByUsername(@RequestParam String username) { return userService.getByUsername(username); } @PostMapping("/create") public CommonResult create(@RequestBody User user) { return userService.create(user); } @PostMapping("/update") public CommonResult update(@RequestBody User user) { return userService.update(user); } @PostMapping("/delete/{id}") public CommonResult delete(@PathVariable Long id) { return userService.delete(id); } }
2019-10-04 15:15:34.829 INFO 9236 --- [nio-8201-exec-5] c.macro.cloud.controller.UserController : 根據id獲取用戶信息,用戶名稱爲:macro 2019-10-04 15:15:35.492 INFO 9236 --- [io-8201-exec-10] c.macro.cloud.controller.UserController : 根據id獲取用戶信息,用戶名稱爲:macro 2019-10-04 15:15:35.825 INFO 9236 --- [nio-8201-exec-9] c.macro.cloud.controller.UserController : 根據id獲取用戶信息,用戶名稱爲:macro
Feign中的服務降級使用起來很是方便,只須要爲Feign客戶端定義的接口添加一個服務降級處理的實現類便可,下面咱們爲UserService接口添加一個服務降級實現類。
須要注意的是它實現了UserService接口,而且對接口中的每一個實現方法進行了服務降級邏輯的實現。
/** * Created by macro on 2019/9/5. */ @Component public class UserFallbackService implements UserService { @Override public CommonResult create(User user) { User defaultUser = new User(-1L, "defaultUser", "123456"); return new CommonResult<>(defaultUser); } @Override public CommonResult<User> getUser(Long id) { User defaultUser = new User(-1L, "defaultUser", "123456"); return new CommonResult<>(defaultUser); } @Override public CommonResult<User> getByUsername(String username) { User defaultUser = new User(-1L, "defaultUser", "123456"); return new CommonResult<>(defaultUser); } @Override public CommonResult update(User user) { return new CommonResult("調用失敗,服務被降級",500); } @Override public CommonResult delete(Long id) { return new CommonResult("調用失敗,服務被降級",500); } }
修改@FeignClient註解中的參數,設置fallback爲UserFallbackService.class便可。
@FeignClient(value = "user-service",fallback = UserFallbackService.class) public interface UserService { }
feign: hystrix: enabled: true #在Feign中開啓Hystrix
Feign提供了日誌打印功能,咱們能夠經過配置來調整日誌級別,從而瞭解Feign中Http請求的細節。
咱們經過java配置來使Feign打印最詳細的Http請求日誌信息。
/** * Created by macro on 2019/9/5. */ @Configuration public class FeignConfig { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
配置UserService的日誌級別爲debug。
logging: level: com.macro.cloud.service.UserService: debug
調用 http://localhost:8701/user/1進行測試,能夠看到如下日誌。
2019-10-04 15:44:03.248 DEBUG 5204 --- [-user-service-2] com.macro.cloud.service.UserService : [UserService#getUser] ---> GET http://user-service/user/1 HTTP/1.1 2019-10-04 15:44:03.248 DEBUG 5204 --- [-user-service-2] com.macro.cloud.service.UserService : [UserService#getUser] ---> END HTTP (0-byte body) 2019-10-04 15:44:03.257 DEBUG 5204 --- [-user-service-2] com.macro.cloud.service.UserService : [UserService#getUser] <--- HTTP/1.1 200 (9ms) 2019-10-04 15:44:03.257 DEBUG 5204 --- [-user-service-2] com.macro.cloud.service.UserService : [UserService#getUser] content-type: application/json;charset=UTF-8 2019-10-04 15:44:03.258 DEBUG 5204 --- [-user-service-2] com.macro.cloud.service.UserService : [UserService#getUser] date: Fri, 04 Oct 2019 07:44:03 GMT 2019-10-04 15:44:03.258 DEBUG 5204 --- [-user-service-2] com.macro.cloud.service.UserService : [UserService#getUser] transfer-encoding: chunked 2019-10-04 15:44:03.258 DEBUG 5204 --- [-user-service-2] com.macro.cloud.service.UserService : [UserService#getUser] 2019-10-04 15:44:03.258 DEBUG 5204 --- [-user-service-2] com.macro.cloud.service.UserService : [UserService#getUser] {"data":{"id":1,"username":"macro","password":"123456"},"message":"操做成功","code":200} 2019-10-04 15:44:03.258 DEBUG 5204 --- [-user-service-2] com.macro.cloud.service.UserService : [UserService#getUser] <--- END HTTP (92-byte body)
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
在Feign中配置Ribbon能夠直接使用Ribbon的配置,具體能夠參考Spring Cloud Ribbon:負載均衡的服務調用。github
在Feign中配置Hystrix能夠直接使用Hystrix的配置,具體能夠參考Spring Cloud Hystrix:服務容錯保護。web
springcloud-learning ├── eureka-server -- eureka註冊中心 ├── user-service -- 提供User對象CRUD接口的服務 └── feign-service -- feign服務調用測試服務
https://github.com/macrozheng/springcloud-learningspring
mall項目全套學習教程連載中,關注公衆號第一時間獲取。json