唯能極於情,故能極於劍
有問題或錯誤請及時聯系小編或關注小編公眾號 "CodeCow",小編一定及時回復和改正,期待和大家一起學習交流
此文由四部分組成(OpenFeign簡介、@FeignClient 使用、實操、總結),別着急,慢慢來
一、OpenFeign
1.1、OpenFeign 啥玩意 ?:
- 官網:Feign 是一個聲明式的Web服務客戶端,讓編寫Web服務客戶端變得非常容易,只需 創建一個接口並在接口上添加注解 即可
1.2、OpenFeign 能干嘛 ? :
小編就不多 BB 先來張圖 壓壓驚
這圖不難理解:客服端調用服務端有兩種方式 ribbon + restTemplate 或 Openfeign;
但是:在我們實際開發當中,往往 一個接口會被多處調用,所以通常都會針對每個微服務自行封裝一些客服端類來包裝這些依賴服務的調用 ;所以,OpenFeign在此基礎上做了進一步封裝,由他來幫助我們實現依賴服務接口的定義。
因此:需要我們做的非常簡單,我們只需要創建一個接口並使用注解的方式來配置他,即可完成對服務提供方的接口綁定。
來兩個 好男人 必備網站, 哈哈
官網:cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/#spring-cloud-openfeign( 賊 慢。。。。)
GitHub:https://github.com/spring-cloud/spring-cloud-openfeign (還好,一般般)
二、OpenFeign 實操
了解OpenFeign 了,不來點 硬核 咋行呢,下面小編就結合實際開發和大家聊聊
注意:有服務端和客戶端兩個模塊/項目
2.1、 :服務端 操作
①、首先導包
<!--監控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--服務注冊與發現 consul--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
②、改 YML 配置文件
server: port: 8006 //端口號 spring: application: name: cloud-payment-service //服務名 cloud: consul: host: localhost //consul的IP port: 8500 //consul啟動端口默認8500 discovery: service-name: ${spring.application.name} prefer-ip-address: true //不寫這個配置,在docker下的consul里面健康檢查會失敗 healthCheckInterval: 5s //健康檢查頻率 port: ${server.port} //注冊服務所在端口
③、添加主啟動類
@SpringBootApplication // springboot 注解 @EnableDiscoveryClient //作用:能夠讓注冊中心發現,並掃描到該服務 public class ProvideMain8006 { public static void main(String[] args) { SpringApplication.run(ProvideMain8006.class, args); } }
④、業務邏輯Controller
@RestController @Slf4j public class PaymentController { @Resource private PaymentService paymentService; @GetMapping("/payment/get/{id}") public RespResult<Payment> getPaymentById(@PathVariable("id") Integer id) { Payment payment = paymentService.getById(id); //getById 是serviceImpl中根據id 獲取 payment 實體的方法, 小編在這就不贅述了 log.info("查詢結果為:" + payment); if (payment != null) { return RespResult.success(payment); // RespResult 是小編自己封裝的返回結果,不懂可以問小編 } else { return new RespResult<>(444, "查詢為空"); } } } 服務端就完事了: 自測: url : http://localhost:8006/payment/get/1 結果 :{"code":200,"message":"成功","data":{"id":1,"desc":"98K"}} 解釋 :Payment 實體就兩個字段 id、desc, 通過結果可以知道 RespResult 是啥了吧 經過 小編一頓 SAO 操作, 服務端 還可以吧 ^ _ ^
2.1、 :客服端 / 消費端 操作
注意:消費端大體和客服端一樣,注意細節 O ^ _ ^
①、首先導包
<!--監控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--服務注冊與發現 consul--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!--服務調用 openFeign--> // 朋友:包其實就比服務端多一個 openFeign <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
②、改 YML 配置文件
server: port: 80 //端口號 瀏覽網頁服務默認的端口號都是80,因此只需輸入網址即可,不用輸入“: 80”了 spring: application: name: cloud-order-service //服務名 cloud: consul: host: localhost //consul的IP port: 8500 //consul啟動端口默認8500 discovery: service-name: ${spring.application.name} prefer-ip-address: true //不寫這個配置,在docker下的consul里面健康檢查會失敗 healthCheckInterval: 5s //健康檢查頻率 port: ${server.port} //注冊服務所在端口 ribbon: // 此配置和 OpenFeign 超時控制 有關, 小編在這就不多贅述了,有想了解,直接給小編留言 ReadTimeout: 5000 //請求處理的超時時間 5秒 ConnectTimeout: 3000 //請求連接的超時時間 3秒 logging: // 此配置和 OpenFeign 打印日志級別有關 level: top.msxdlb.springcloud.service.OrderService: debug //日志打印級別
③、添加主啟動類
@SpringBootApplication // springboot 注解 @EnableDiscoveryClient //作用:能夠讓注冊中心發現,並掃描到該服務 @EnableFeignClients //服務調用 注解 public class ConsumerMain80 { public static void main(String[] args) { SpringApplication.run(ConsumerMain80.class); } }
④、業務邏輯 Service
/** * 此類作用: 不懂看 最上面 OpenFeign 是啥玩意? * OpenFeign 能干嘛? */ @Component @FeignClient("cloud-payment-service") //里面是服務端的 服務名(yml 文件中) public interface PaymentService{ @GetMapping("/payment/get/{id}") public RespResult<Payment> getPaymentById(@PathVariable("id") Integer id);
④、業務邏輯 Controller
@RestController @Slf4j public class PaymentController { /** * 使用 Feign 方式調用 */ @Resource private PaymentService paymentService; // 注入 service 中 通過 @FeignClient 定義好的接口 @GetMapping("/feign/payment/get/{id}") public RespResult<Payment> getPayment(@PathVariable("id") Integer id) { log.info("<<<<<<<<<<< 我是通過feign >>>>>>>>>>>>"); return paymentService.getPaymentById(id); } } 消費端也完事了: 總測試步驟: 1、啟動服務端 2、啟動消費端 3、地址欄輸入url: http://localhost/feign/payment/get/1 // 為什么不加端口號(回去看 消費端 Yml 配置 有說明) 4、結果: 如果結果為:{"code":200,"message":"成功","data":{"id":1,"desc":"98K"}} 就是和服務端自測結果一樣,恭喜你 成功了 如果你 有幸看到這里: 咋青山不改,綠水長流,不妨看看小編其他作品,很香喲, 呵呵
第二步OpenFeign 實操總結: 其實就兩點:
①、在消費端啟動類加:@EnableFeignClients 注解
②、在業務邏輯 Service 接口加: @FeignClient 注解 即可
三、總結
這是 SpringCloud 的 OpenFeign 篇,后續小編會從 “Hystrix、Gateway …” 等堅持以博客的方式來分享自己對SpringCloud 的理解,並從不同角度和大家分享工作心得,並且含有相關Demo,最終小編會發布到GitHub上,供大家下載、分享、交流、指正,下面是源碼地址:
GitHub:https://github.com/msxdlb/Spring-Cloud-2020
有問題或錯誤請及時聯系小編或關注小編公眾號 “CodeCow”,小編一定及時回復和改正 啦
《 心有多大,舞台就有多大 》 人得有——理想
2020/04/16 午后