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

SpringCloud OpenFeign超時(shí)控制示例詳解

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

前言:

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

1、編寫代碼模擬連接超時(shí)

(1)編寫providder-payment8001項(xiàng)目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ù)庫(kù)成功,提供服務(wù)的端口號(hào)為" + serverPort,result);
        }else {
            return new CommonResult(500,"插入數(shù)據(jù)庫(kù)失敗",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ù)的端口號(hào)為" + serverPort,payment);
        }else {
            return new CommonResult(500,"沒(méi)有對(duì)應(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下的實(shí)例
        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ù)的端口號(hào)
        return serverPort;
    }
    @GetMapping("/payment/feign/timeout")
    public String paymentFeigntimeout() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //返回當(dāng)前服務(wù)的端口號(hào)
        return serverPort;
    }
}

(2)編寫cloud-consumer-feign-order80項(xiàng)目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èn)的微服務(wù)的名稱,表示通過(guò)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項(xiàng)目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() {
        //客戶端一般默認(rèn)等待1秒鐘
        return paymentFeignService.paymentFeigntimeout();
    }
}

2、測(cè)試payment接口是否正常工作

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

3、測(cè)試通過(guò)consumer服務(wù)遠(yuǎn)程調(diào)用payment服務(wù)

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

效果圖:

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

修改cloud-consumer-feign-order80項(xiàng)目的application.yml文件(因?yàn)镺penFeign集成了Ribbon,所以O(shè)penFeign的超時(shí)控制也由最底層的Ribbon來(lái)進(jìn)行限制,所以這里是對(duì)Ribbon進(jìn)行配置)

集成示意圖:

application.yml文件

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

5、重新測(cè)試通過(guò)consumer服務(wù)遠(yuǎn)程調(diào)用payment服務(wù)

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

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

相關(guān)文章

  • eclipse配置javap命令的方法

    eclipse配置javap命令的方法

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

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

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

    JAVA中常見(jiàn)異常類

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

    Java 手動(dòng)解析不帶引號(hào)的JSON字符串的操作

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

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

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

    SpringBoot接口惡意刷新和暴力請(qǐng)求的解決方法

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

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

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

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

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

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

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

    Spring Cloud CLI簡(jiǎn)單介紹

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

最新評(píng)論

肥城市| 宜黄县| 德兴市| 冀州市| 英山县| 万州区| 安龙县| 永济市| 化隆| 丹凤县| 贵南县| 甘肃省| 宜丰县| 广河县| 巫溪县| 临高县| 贺州市| 静宁县| 张家川| 遂川县| 阜康市| 长春市| 察隅县| 阜平县| 吉林市| 芦溪县| 平乐县| 海阳市| 佛坪县| 当雄县| 美姑县| 昭通市| 聂荣县| 蕉岭县| 利辛县| 浑源县| 云阳县| 靖宇县| 康马县| 林州市| 林甸县|