最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringCloud OpenFeign超時控制示例詳解

 更新時間:2024年05月27日 09:35:48   作者:Ken_1115  
在Spring Cloud中使用OpenFeign時,可以通過配置來控制請求的超時時間,這篇文章主要介紹了SpringCloud OpenFeign超時控制,需要的朋友可以參考下

前言:

在上一章節(jié)中我們簡單的介紹了如何使用OprnFeign去調(diào)用微服務(wù),因為消費側(cè)和服務(wù)側(cè)是兩個不同的微服務(wù),這樣可能會出現(xiàn)超時的現(xiàn)象,例如服務(wù)側(cè)需要3秒處理任何才能返回結(jié)果,但消費側(cè)可能2秒就斷開連接了,這時就會因為時間差而出現(xiàn)連接超時的問題,而本節(jié)內(nèi)容則是關(guān)于如果去對OpenFeign進行超時控制。

1、編寫代碼模擬連接超時

(1)編寫providder-payment8001項目PaymentController類的代碼

package com.ken.springcloud.controller;
import com.ken.springcloud.entities.CommonResult;
import com.ken.springcloud.entities.Payment;
import com.ken.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;
    @Resource
    private DiscoveryClient discoveryClient;
    @PostMapping("/payment/insert")
    public CommonResult insert(@RequestBody Payment payment) {
        int result = paymentService.insert(payment);
        log.info("插入結(jié)果{}",result);
        if(result > 0) {
            return new CommonResult(200,"插入數(shù)據(jù)庫成功,提供服務(wù)的端口號為" + serverPort,result);
        }else {
            return new CommonResult(500,"插入數(shù)據(jù)庫失敗",result);
        }
    }
    @GetMapping("/payment/get/{id}")
    public CommonResult insert(@PathVariable("id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("查詢結(jié)果{}",payment);
        if(payment != null) {
            return new CommonResult(200,"查詢成功,提供服務(wù)的端口號為" + serverPort,payment);
        }else {
            return new CommonResult(500,"沒有對應(yīng)的數(shù)據(jù),查詢失敗,查詢id" + id,payment);
        }
    }
    @GetMapping("/payment/discovery")
    public Object discovery() {
        //獲取eureka內(nèi)的服務(wù)
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("***service:" + service);
        }
        //獲取服務(wù)名為CLOUD-PAYMENT-SERVICE下的實例
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return this.discoveryClient;
    }
    @GetMapping("/payment/lb")
    public String getPaymentLB() {
        //返回當(dāng)前服務(wù)的端口號
        return serverPort;
    }
    @GetMapping("/payment/feign/timeout")
    public String paymentFeigntimeout() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //返回當(dāng)前服務(wù)的端口號
        return serverPort;
    }
}

(2)編寫cloud-consumer-feign-order80項目PaymentFeignService類的代碼

package com.ken.springcloud.service;
import com.ken.springcloud.entities.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里寫的是指定要訪問的微服務(wù)的名稱,表示通過FeignClient去Eureka上面找名稱為CLOUD-PAYMENT-SERVICE的微服務(wù)的接口
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
    //指明要調(diào)用的CLOUD-PAYMENT-SERVICE的微服務(wù)的接口,這里調(diào)用的是PaymentController類里的/payment/get/{id}接口
    @GetMapping("/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id);
    @GetMapping("/payment/feign/timeout")
    public String paymentFeigntimeout();
}

(3)編寫cloud-consumer-feign-order80項目OrderFeignController的代碼

package com.ken.springcloud.controller;
import com.ken.springcloud.entities.CommonResult;
import com.ken.springcloud.entities.Payment;
import com.ken.springcloud.service.PaymentFeignService;
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;
@Slf4j
@RestController
public class OrderFeignController {
    @Resource
    private PaymentFeignService paymentFeignService;
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        return paymentFeignService.getPaymentById(id);
    }
    @GetMapping("/payment/feign/timeout")
    public String paymentFeigntimeout() {
        //客戶端一般默認等待1秒鐘
        return paymentFeignService.paymentFeigntimeout();
    }
}

2、測試payment接口是否正常工作

分別啟動eureka-server7001、eureka-server7002,然后再啟動provider-payment8001,最后再啟動cloud-consumer-feign-order80,全部啟動完畢后在瀏覽器的地址欄里輸入http://localhost:8001/payment/feign/timeout 并且回車調(diào)用接口,最后可以看到接口調(diào)用成功并返回8001,這證明provider-payment8001服務(wù)工作正常

3、測試通過consumer服務(wù)遠程調(diào)用payment服務(wù)

在瀏覽器地址欄里輸入http://localhost/consumer/payment/feign/timeout 并且回車調(diào)用接口,這時會顯示Read timed out executing GET http://CLOUD-PAYMENT-SERVICE/payment/feign/timeout的錯誤信息,這是因為Feign客戶端默認只等待一秒鐘,但是服務(wù)端處理需要超過1秒鐘,導(dǎo)致Feign客戶端不想等待了,直接返回報錯,為了避免這樣的情況,有時候我們需要設(shè)置Feign客戶端的超時控制。

效果圖:

4、設(shè)置Feign客戶端的超時時間

修改cloud-consumer-feign-order80項目的application.yml文件(因為OpenFeign集成了Ribbon,所以O(shè)penFeign的超時控制也由最底層的Ribbon來進行限制,所以這里是對Ribbon進行配置)

集成示意圖:

application.yml文件

server:
  port: 80
eureka:
  client:
    #表示是否將自己注冊進Eureka Server里,默認為true
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
#設(shè)置feign客戶端超時時間(OpenFeign默認支持ribbon)
ribbon:
  #指的是建立連接所用的時間,適用于網(wǎng)絡(luò)狀況正常的情況下,兩端連接所用的時間
  ReadTimeout: 5000
  #指的是建立連接后從服務(wù)器讀取到可用資源所用的時間
  ConnectTimeout: 5000

5、重新測試通過consumer服務(wù)遠程調(diào)用payment服務(wù)

重新啟動consumer服務(wù),然后重新用瀏覽器調(diào)用http://localhost/consumer/payment/feign/timeout 接口,發(fā)現(xiàn)現(xiàn)在并不會再次發(fā)生微服務(wù)間調(diào)用出現(xiàn)連接超時的情況

到此這篇關(guān)于SpringCloud OpenFeign超時控制的文章就介紹到這了,更多相關(guān)SpringCloud OpenFeign超時控制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • eclipse配置javap命令的方法

    eclipse配置javap命令的方法

    本篇文章主要介紹了如何為eclipse配置javap命令,在配置過程中會出現(xiàn)的小問題的解決方法,非常實用,需要的朋友可以參考下
    2015-07-07
  • 史上最全的java隨機數(shù)生成算法分享

    史上最全的java隨機數(shù)生成算法分享

    這篇文章主要介紹了史上最全的java隨機數(shù)生成算法,我分享一個最全的隨機數(shù)的生成算法,最代碼的找回密碼的隨機數(shù)就是用的這個方法
    2014-01-01
  • JAVA中常見異常類

    JAVA中常見異常類

    本文主要介紹了JAVA中的常見異常類。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • Java 手動解析不帶引號的JSON字符串的操作

    Java 手動解析不帶引號的JSON字符串的操作

    這篇文章主要介紹了Java 手動解析不帶引號的JSON字符串的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • web.xml中servlet, bean, filter, listenr 加載順序_動力節(jié)點Java學(xué)院整理

    web.xml中servlet, bean, filter, listenr 加載順序_動力節(jié)點Java學(xué)院整理

    這篇文章主要介紹了web.xml中servlet, bean, filter, listenr 加載順序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • SpringBoot接口惡意刷新和暴力請求的解決方法

    SpringBoot接口惡意刷新和暴力請求的解決方法

    在實際項目使用中,必須要考慮服務(wù)的安全性,當(dāng)服務(wù)部署到互聯(lián)網(wǎng)以后,就要考慮服務(wù)被惡意請求和暴力攻擊的情況,所以本文給大家介紹了SpringBoot接口惡意刷新和暴力請求的解決方法,需要的朋友可以參考下
    2024-11-11
  • Springboot允許logger.debug輸出日志方式

    Springboot允許logger.debug輸出日志方式

    這篇文章主要介紹了Springboot允許logger.debug輸出日志方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • SpringBoot 使用 Ehcache 作為緩存的操作方法

    SpringBoot 使用 Ehcache 作為緩存的操作方法

    這篇文章主要介紹了SpringBoot 如何使用 Ehcache 作為緩存,我們通過添加 Ehcache 依賴、創(chuàng)建 Ehcache 配置文件并在 Spring Boot 應(yīng)用程序的配置文件中啟用 Ehcache 緩存,來配置 Ehcache 緩存,需要的朋友可以參考下
    2023-06-06
  • java基于servlet使用組件smartUpload實現(xiàn)文件上傳

    java基于servlet使用組件smartUpload實現(xiàn)文件上傳

    這篇文章主要介紹了java基于servlet使用組件smartUpload實現(xiàn)文件上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Spring Cloud CLI簡單介紹

    Spring Cloud CLI簡單介紹

    本文我們將介紹Spring Boot Cloud CLI(或簡稱Cloud CLI)。該工具為Spring Boot CLI提供了一組命令行增強功能,有助于進一步抽象和簡化Spring Cloud部署。感興趣的小伙伴們可以參考一下
    2018-12-12

最新評論

南江县| 静宁县| 略阳县| 黎城县| 全椒县| 阳泉市| 淮阳县| 尼玛县| 库伦旗| 金乡县| 卫辉市| 九龙城区| 渑池县| 遵义市| 锡林浩特市| 彭州市| 天水市| 林甸县| 即墨市| 营山县| 神农架林区| 民勤县| 绥德县| 博兴县| 大姚县| 商丘市| 盖州市| 白河县| 仪陇县| 临清市| 辽源市| 明水县| 周至县| 和龙市| 开原市| 昭通市| 青州市| 佳木斯市| 泰安市| 洮南市| 裕民县|