<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SpringCloud之OpenFeign服務(wù)接口調(diào)用

          共 5616字,需瀏覽 12分鐘

           ·

          2021-01-21 09:28

          點擊上方藍色字體,選擇“標(biāo)星公眾號”

          優(yōu)質(zhì)文章,第一時間送達

          76套java從入門到精通實戰(zhàn)課程分享

          一、OpenFeign是什么?

          1、概述
          官網(wǎng)文檔網(wǎng)址:點擊訪問
          GitHub:點擊訪問
          – Feign是一個聲明性web服務(wù)客戶機。它使編寫web服務(wù)客戶機變得更容易。
          – 它的使用方法是
          定義一個服務(wù)接口并在上面添加注解
          – Feign支持可插拔編碼器和解碼器。
          – Spring Cloud對Feign進行了封裝,使其支持SpringMVC標(biāo)準(zhǔn)注解和HttpttpMessageConverters。Feign可以與Eureka和Ribbon組合使用以支持負載均衡。

          2、Feign能干什么
          Feign旨在使編寫Java Http客戶端變得更容易。
          前面在使用Ribbon+RestTemplate時,利用RestTemplate對http請求的封裝處理,形成了一套模版化的調(diào)用方法。但在實際開發(fā)中,由于對服務(wù)依賴的調(diào)用可能不止一處,
          往往一個接口可能被多處調(diào)用,所以通常都會針對每個微服務(wù)自行封裝一些客戶端類來包裝這些依賴服務(wù)的調(diào)用。所以,F(xiàn)eign在此基礎(chǔ)上做了進一步封裝,由它來幫助我們定義和實現(xiàn)依賴服務(wù)接口的定義。在Feign的實現(xiàn)下,我們只需要創(chuàng)建一個接口并使用注解的方式來配置它(以前是Dao接口上面標(biāo)注Mapper注解,現(xiàn)在是一個微服務(wù)接口上標(biāo)注一個Feign注解即可),即可完成對服務(wù)提供方的的接口綁定,簡化了使用Spring Cloud Ribbon時,自動封裝服務(wù)調(diào)用客戶端的開發(fā)量。

          3、Feign集成了Ribbon
          利用Ribbon維護了Payment的服務(wù)列表信息,并且通過輪詢實現(xiàn)了客戶端的負載均衡。而與Ribbon不同的是,
          通過Feign只需要定義服務(wù)綁定接口且以聲明式的方法,優(yōu)雅而簡單的實現(xiàn)了服務(wù)的調(diào)用。

          4、Feign和OpenFeign的區(qū)別

          FeignOpenFeign
          Feign是SpringCloud組件中一個輕量級RESTful的HTTP服務(wù)客戶端,F(xiàn)eign內(nèi)置了Ribbon,用來做客戶端負載均衡,去調(diào)用服務(wù)注冊中心的服務(wù)。Feign的使用方式是:使用Feign的注解定義接口,調(diào)用這個接口,就可以調(diào)用服務(wù)注冊中心的服務(wù)OpenFeign 是SpringCloud在Feign的基礎(chǔ)上支持了SpringMVC的注解,如@RequestMapping等。OpenFeign 的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通過動態(tài)代理的方式產(chǎn)生實現(xiàn)類,實現(xiàn)類中做負載均衡并調(diào)用其他服務(wù)。

          ?org.springframework.cloud
          ?spring-cloud-starter-feign



          ?org.springframework.cloud
          ?spring-cloud-starter-openfeign


          5、OpenFeign服務(wù)調(diào)用
          (1)創(chuàng)建cloud-consumer-feign-order80子模塊

          A、引入依賴


          ????????
          ????????
          ????????????org.springframework.cloud
          ????????????spring-cloud-starter-openfeign
          ????????

          ????????
          ????????
          ????????????org.springframework.cloud
          ????????????spring-cloud-starter-netflix-eureka-client
          ????????

          ????????
          ????????
          ????????????com.atguigu.springcloud
          ????????????cloud-api-commons
          ????????????1.0-SNAPSHOT
          ????????

          ????????
          ????????
          ????????????org.springframework.boot
          ????????????spring-boot-starter-web
          ????????

          ????????
          ????????????org.springframework.boot
          ????????????spring-boot-starter-actuator
          ????????

          ????????
          ????????
          ????????????org.springframework.boot
          ????????????spring-boot-devtools
          ????????????runtime
          ????????????true
          ????????

          ????????
          ????????????org.projectlombok
          ????????????lombok
          ????????????true
          ????????

          ????????
          ????????????org.springframework.boot
          ????????????spring-boot-starter-test
          ????????????test
          ????????

          ????


          B、創(chuàng)建并編寫application.yml配置文件

          server:
          ??port:?80
          eureka:
          ??client:
          ????#表示將自己注冊到EurekaServer
          ????register-with-eureka:?false
          ????service-url:
          ??????defaultZone:?http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/?#集群版

          C、創(chuàng)建啟動類

          import?org.springframework.boot.SpringApplication;
          import?org.springframework.boot.autoconfigure.SpringBootApplication;
          import?org.springframework.cloud.openfeign.EnableFeignClients;

          @SpringBootApplication
          @EnableFeignClients?//使用Feign激活并開啟
          public?class?OrderFeignMain80?{

          ????public?static?void?main(String[]?args)?{
          ????????SpringApplication.run(OrderFeignMain80.class,args);
          ????}
          }

          D、編寫業(yè)務(wù)類:業(yè)務(wù)邏輯接口+@FeignClient配置調(diào)用provider服務(wù)

          import?com.atguigu.springcloud.entity.Payment;
          import?com.atguigu.springcloud.utils.CommonResult;
          import?org.springframework.cloud.openfeign.FeignClient;
          import?org.springframework.stereotype.Component;
          import?org.springframework.web.bind.annotation.GetMapping;
          import?org.springframework.web.bind.annotation.PathVariable;

          @Component
          @FeignClient(value?=?"CLOUD-PAYMENT-SERVICE")
          public?interface?PaymentFeignService?{

          ????@GetMapping(value?=?"/payment/get/{id}")
          ????CommonResult?getPaymentById(@PathVariable("id")?Long?id);
          }

          E、編寫控制類

          import?com.atguigu.springcloud.entity.Payment;
          import?com.atguigu.springcloud.service.PaymentFeignService;
          import?com.atguigu.springcloud.utils.CommonResult;
          import?lombok.extern.slf4j.Slf4j;
          import?org.springframework.web.bind.annotation.GetMapping;
          import?org.springframework.web.bind.annotation.PathVariable;
          import?org.springframework.web.bind.annotation.RestController;

          import?javax.annotation.Resource;

          @RestController
          @Slf4j
          public?class?OrderFeignController?{

          ????@Resource
          ????private?PaymentFeignService?paymentFeignService;

          ????@GetMapping(value?=?"/consumer/payment/get/{id}")
          ????public?CommonResult?getPaymentById(@PathVariable("id")?Long?id?)?{
          ????????return?paymentFeignService.getPaymentById(id);
          ????}
          }

          F、啟動主啟動類訪問http://localhost/consumer/payment/get/301
          可以看到openFeign實現(xiàn)了輪詢負載均衡來訪問服務(wù)提供者提供的服務(wù)。

          G、openFeign接口方法對應(yīng)關(guān)系

          6、OpenFeign的超時控制
          (1)超時測試,在payment8001/8002中添加延時處理方法:

          @GetMapping("feign/timeout")
          ????public?String?paymentFeignTimeout(){
          ????????try?{
          ????????????TimeUnit.SECONDS.sleep(3);
          ????????}?catch?(InterruptedException?e)?{
          ????????????e.printStackTrace();
          ????????}
          ????????return?serverPort;
          ????}

          瀏覽器訪問可以看到響應(yīng)時間3s后才會出現(xiàn)結(jié)果:
          (2)使用order80消費者端訪問這個延時功能:
          發(fā)現(xiàn)訪問失敗,報超時錯誤!原因是:OpenFeign默認響應(yīng)時間為1s,超過1s未得到微服務(wù)的提供者提供的服務(wù)就會報錯。
          為了避免這種情況,需要在application.yml/properties中設(shè)置Feign客戶端的超時控制。

          #設(shè)置feign客戶端超時時間(openfeign默認支持ribbon)
          ribbon:
          ??#指的是建立連接所用的時間,適用于網(wǎng)絡(luò)狀況正常的情況下,兩端連接所用的時間
          ??ReadTimeout:?5000
          ??#指的是建立連接后從服務(wù)器讀取到可用資源所用的時間
          ??ConnectTimeout:?5000

          設(shè)置后再次訪問,不在報錯:
          7、OpenFeign日志增強
          Feign提供了日志打印功能,我們可以通過配置來調(diào)整日志級別,從而了解Feign中Http請求的細節(jié)。說白了就是
          對Feign接口的調(diào)用情況進行檢控和輸出
          NONE:默認的,不顯示任何日志。
          BASIC:僅記錄請求方法、URL、響應(yīng)狀態(tài)嗎即執(zhí)行時間。
          HEADERS:除了BASIC中定義的信息之外,還有請求和響應(yīng)的頭信息。
          FULL:除了HEADERS中定義的信息之外,還有請求和響應(yīng)的正文及元數(shù)據(jù)。
          (1)添加配置類指明日志打印級別

          import?feign.Logger;
          import?org.springframework.context.annotation.Bean;
          import?org.springframework.context.annotation.Configuration;

          @Configuration
          public?class?FeignConfig?{
          ????@Bean
          ????Logger.Level?feignLoggerLevel(){
          ????????return?Logger.Level.FULL;
          ????}
          }

          (2)application.yml/properties配置文件開啟日志的Feign客戶端

          logging:
          ??level:
          ????#feign日志以什么級別監(jiān)控哪個接口
          ????com.atguigu.springcloud.service.PaymentFeignService:?debug

          (3)啟動order80發(fā)送一次請求查看后臺打印情況



          版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。

          本文鏈接:

          https://blog.csdn.net/weixin_42453582/article/details/112724103





          粉絲福利:Java從入門到入土學(xué)習(xí)路線圖

          ??????

          ??長按上方微信二維碼?2 秒


          感謝點贊支持下哈?

          瀏覽 58
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  婷婷丁香亚洲日韩 | 毛片性爱视屏 | 亚洲第一综合网 | 91久久婷婷国产麻豆精品电影 | 久久久久亚洲AV成人网人人小说 |