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

SpringCloud之Feign代理,聲明式服務(wù)調(diào)用方式

 更新時(shí)間:2022年03月09日 09:07:08   作者:擇業(yè)  
這篇文章主要介紹了SpringCloud之Feign代理,聲明式服務(wù)調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

將其他微服務(wù)中的服務(wù)接口,用feign在本項(xiàng)目中進(jìn)行調(diào)用。

Spring Cloud Feign是一套基于Netflix Feign實(shí)現(xiàn)的聲明式服務(wù)調(diào)用客戶端。它使得編寫Web服務(wù)客戶端變得更加簡(jiǎn)單。我們只需要通過創(chuàng)建接口并用注解來配置它既可完成對(duì)Web服務(wù)接口的綁定。它具備可插拔的注解支持,包括Feign注解、JAX-RS注解。它也支持可插拔的編碼器和解碼器。Spring Cloud Feign還擴(kuò)展了對(duì)Spring MVC注解的支持,同時(shí)還整合了Ribbon和Hystrix來提供均衡負(fù)載的HTTP客戶端實(shí)現(xiàn)。

對(duì)于Feign來講,其實(shí)就是一個(gè)WEB接口而已,它內(nèi)部自集成了Spring Ribbon 和Spring Hystrix 斷路器功能,也就是說可以支持自動(dòng)降級(jí)和負(fù)載均衡功能??梢哉f,在內(nèi)部服務(wù)與服務(wù)之間的相互數(shù)據(jù)通信橋梁就是通過Feign來實(shí)現(xiàn)的。也就是說,我們可以像使用web service OR dubbo 一樣對(duì)其進(jìn)行聲明式的配置,這非常棒~ 在我之前的工作中,就大量的使用了Feign代理,可以說使用spring cloud 就必須要學(xué)會(huì)如何深入使用Feign代理,當(dāng)然它也非常的簡(jiǎn)單。

引入相關(guān)依賴然后再主入口啟用注解

@EnableFeignClients    //啟用代理服務(wù)
@EnableCircuitBreaker  //啟用斷路器

引入相關(guān)依賴然后再主入口啟用注解:@Enabl

注意:我們feign在第四個(gè)版本后需要手工的開啟斷路器功能才可以生效。

了解完Feign的基礎(chǔ)配置之后,我們當(dāng)然要開始代碼實(shí)現(xiàn)了。首先我們需要編寫一個(gè)interface,并且這個(gè)interface一定是已知的服務(wù)(也就是注冊(cè)到了Eureka上的接口服務(wù),我們?cè)谶@里需要使用interface的方式進(jìn)行聲明)

@FeignClient注解就是Feign的注解聲明了,里面name屬性表示了當(dāng)前代理的服務(wù)APP NAME; fallback屬性當(dāng)然就是我們調(diào)用服務(wù)失敗的降級(jí)策略了,也就是當(dāng)網(wǎng)絡(luò)閃段、異常等調(diào)用代理服務(wù)失敗時(shí),會(huì)根據(jù)斷路器的超時(shí)時(shí)間降級(jí)到指定的fallback所賦予的HelloServiceHystrix類中,我們可以進(jìn)行降級(jí)處理。

我們的Feign底層默認(rèn)提供了重試機(jī)制,也就是底層使用Retryer類對(duì)調(diào)用服務(wù)進(jìn)行重試調(diào)用操作,通過底層代碼我們知道默認(rèn)是每100ms去進(jìn)行調(diào)用,調(diào)用次數(shù)是5次。既然Feign集成了Ribbon 與 Hystrix ,那么必然會(huì)使用兩個(gè)超時(shí)機(jī)制,一個(gè)是Ribbon的超時(shí)時(shí)間,一個(gè)是Hystrix的超時(shí)時(shí)間.這兩個(gè)超時(shí)時(shí)間的含義截然不同,千萬要注意配置。

經(jīng)驗(yàn)小結(jié): 我們可以配置Hystrix的超時(shí)時(shí)間大于Ribbon的超時(shí)時(shí)間。并且如果想進(jìn)行重試最好是Hystrix的超時(shí)時(shí)間設(shè)置為Ribbon的超時(shí)時(shí)間的倍數(shù)。

這樣我們可以進(jìn)行重試策略,如果Hystrix的超時(shí)時(shí)間小于Ribbon的超時(shí)時(shí)間,則不會(huì)重試,直接被斷路器組件對(duì)調(diào)用請(qǐng)求執(zhí)行請(qǐng)求段熔機(jī)制,服務(wù)降級(jí)。

Feign配合Ribbon、Hystrix的超時(shí)策略配置如下

1.pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
? ? ? ? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ? ?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
? ? <parent>
? ? ? ? <artifactId>com.zx.dt2b.erp</artifactId>
? ? ? ? <groupId>com.zx.dt2b.erp</groupId>
? ? ? ? <version>1.0-SNAPSHOT</version>
? ? </parent>
? ? <modelVersion>4.0.0</modelVersion>
?
? ? <artifactId>feign</artifactId>
? ? <dependencies>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-actuator</artifactId>
? ? ? ? </dependency>
? ? ? ? <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-netflix-hystrix</artifactId>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-starter-feign</artifactId>
? ? ? ? </dependency>
? ? </dependencies>
?
? ? <dependencyManagement>
? ? ? ? <dependencies>
? ? ? ? ? ? <dependency>
? ? ? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? ? ? <artifactId>spring-cloud-dependencies</artifactId>
? ? ? ? ? ? ? ? <!-- <version>Dalston.SR5</version> ?-->
? ? ? ? ? ? ? ? <version>Edgware.SR4</version>
? ? ? ? ? ? ? ? <!-- <version>Finchley.SR1</version> ?-->
? ? ? ? ? ? ? ? <type>pom</type>
? ? ? ? ? ? ? ? <scope>import</scope>
? ? ? ? ? ? </dependency>
? ? ? ? </dependencies>
? ? </dependencyManagement>?
?
? ? <build>
? ? ? ? <finalName>feign</finalName>
? ? ? ? <plugins>
? ? ? ? ? ? <plugin>
? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ? ? <artifactId>spring-boot-maven-plugin</artifactId>
? ? ? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? ? ? ? <mainClass>com.zx.dt2b.FeignApplication</mainClass>
? ? ? ? ? ? ? ? </configuration>
? ? ? ? ? ? </plugin>
? ? ? ? </plugins>
? ? </build>
?
</project>

2.主入口

package com.zx.dt2b;?
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
?
@EnableFeignClients?? ??? ?//啟用代理服務(wù)
@EnableCircuitBreaker?? ?//啟用斷路器
@EnableDiscoveryClient?? ?//標(biāo)識(shí)具體的一個(gè)服務(wù),需要向注冊(cè)中心注冊(cè)
@SpringBootApplication?? ?//SpringBoot 核心配置
public class FeignApplication {
?
?? ?public static void main(String[] args) {?
?? ??? ?SpringApplication.run(FeignApplication.class, args);
?? ?}?
}

3.配置文件

feign:
? hystrix:
? ? enabled: true ?#開啟降級(jí)
? compression:
? ? request:
? ? ? min-request-size: 2048
? ? ? mime-types:
? ? ? ? - text/html, application/xml, application/json
spring:
? application:
? ? name: feign-consumer
? cloud:
? ? loadbalancer:
? ? ? retry:
? ? ? ? enabled: true
?
server:
? context-path: /
? port: 7005
?
eureka:
? client:
? ? service-url:
? ? ? defaultZone: http://eureka1:8001/eureka
?
feign:
? hystrix:
? ? enabled: true
? compression:
? ? request:
? ? ? min-request-size: 2048
? ? ? mime-types:
? ? ? ? - text/html, application/xml, application/json
?
##設(shè)置斷路器的超時(shí)時(shí)間
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 10000
?
##微服務(wù)的請(qǐng)求配置
customer-service:
? ConnectTimeout: 10000
? ReadTimeout: 3000
? ribbon:
? ? OkToRetryOnAllOperations: true ##對(duì)所有的請(qǐng)求都進(jìn)行重試
? ? MaxAutoRetriesNextServer: 1 ##切換實(shí)例的次數(shù)
? ? MaxAutoRetries: 2 ? ?##對(duì)當(dāng)前實(shí)例重試的次數(shù)

4.業(yè)務(wù)代碼與實(shí)現(xiàn)

@FeignClient(name="provider-service", fallback= IndexFeignFailback.class)

name表示微服務(wù)名稱:前端直接從本項(xiàng)目訪問其他項(xiàng)目服務(wù)接口

失敗后IndexFeignFailback中對(duì)應(yīng)的接口,進(jìn)行降級(jí)。

代理服務(wù)中異常等要保持一致

package com.zx.dt2b.feign;?
import com.zx.dt2b.feign.hystrix.IndexFeignFailback;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
?
@FeignClient(name="customer-service", fallback= IndexFeignFailback.class)
public interface IndexFeignClient {
?
?? ?@RequestMapping(value="/customerservice/index", method = {RequestMethod.GET})
?? ?public String hello() throws Exception;
?
?? ?@RequestMapping(value="/customerservice/hi", method = {RequestMethod.GET})
?? ?public String hi() throws InterruptedException;
}
package com.zx.dt2b.feign.hystrix;?
import com.zx.dt2b.feign.IndexFeignClient;
import org.springframework.stereotype.Component;
?
@Component
public class IndexFeignFailback implements IndexFeignClient {
?
?? ?@Override
?? ?public String hello() throws Exception {
?? ??? ?return "-----hello接口的降級(jí)方法!--------";
?? ?}
?
?? ?@Override
?? ?public String hi() throws InterruptedException {
?? ??? ?return "-----hi接口的降級(jí)方法!--------";
?? ?}?
}

5.controller測(cè)試

package com.zx.dt2b.api;?
import com.zx.dt2b.feign.IndexFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
?
@RestController
public class ConsumerController {
?
?? ?@Autowired
?? ?private IndexFeignClient indexFeignClient;
?
?? ?@RequestMapping(value="/feign-hello")
?? ?public String hello() throws Exception {
?? ??? ?return indexFeignClient.hello();
?? ?}
?
?? ?@RequestMapping(value="/feign-hi")
?? ?public String hi() throws InterruptedException {
?? ??? ?return indexFeignClient.hi();
?? ?}
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring Boot REST國(guó)際化的實(shí)現(xiàn)代碼

    Spring Boot REST國(guó)際化的實(shí)現(xiàn)代碼

    本文我們將討論如何在現(xiàn)有的Spring Boot項(xiàng)目中添加國(guó)際化。只需幾個(gè)簡(jiǎn)單的步驟即可實(shí)現(xiàn)Spring Boot應(yīng)用的國(guó)際化,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Windows安裝兩個(gè)或多個(gè)JDK并實(shí)現(xiàn)自由切換的方法

    Windows安裝兩個(gè)或多個(gè)JDK并實(shí)現(xiàn)自由切換的方法

    最近新接手一個(gè)項(xiàng)目,啟動(dòng)的時(shí)候,發(fā)現(xiàn)有些jar和現(xiàn)在正在使用的JDK版本不一致,一直啟動(dòng)有問題,想著就多裝一個(gè)JDK,由于為了保證java的運(yùn)行環(huán)境和編譯環(huán)境保持一致,就需要我們?cè)O(shè)置jdk的環(huán)境變量,所以本文給大家介紹了Windows安裝兩個(gè)或多個(gè)JDK并實(shí)現(xiàn)自由切換的方法
    2025-03-03
  • Servlet連接數(shù)據(jù)庫實(shí)現(xiàn)用戶登錄的實(shí)現(xiàn)示例

    Servlet連接數(shù)據(jù)庫實(shí)現(xiàn)用戶登錄的實(shí)現(xiàn)示例

    本文主要介紹了Servlet連接數(shù)據(jù)庫實(shí)現(xiàn)用戶登錄的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Struts2學(xué)習(xí)教程之輸入校驗(yàn)示例詳解

    Struts2學(xué)習(xí)教程之輸入校驗(yàn)示例詳解

    這篇文章主要給大家介紹了關(guān)于Struts2學(xué)習(xí)教程之輸入校驗(yàn)的相關(guān)資料,文中通過示例介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用struts2具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • MybatisPlus+Postgresql整合的幾個(gè)坑及解決

    MybatisPlus+Postgresql整合的幾個(gè)坑及解決

    這篇文章主要介紹了MybatisPlus+Postgresql整合的幾個(gè)坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Java從內(nèi)存角度帶你理解數(shù)組名實(shí)質(zhì)是個(gè)地址的論述

    Java從內(nèi)存角度帶你理解數(shù)組名實(shí)質(zhì)是個(gè)地址的論述

    這篇文章主要介紹了Java如何從內(nèi)存解析的角度理解“數(shù)組名實(shí)質(zhì)是一個(gè)地址”,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-09-09
  • spring中BeanPostProcessor的作用和使用注意事項(xiàng)

    spring中BeanPostProcessor的作用和使用注意事項(xiàng)

    在Spring框架中,BeanPostProcessor?是一個(gè)核心擴(kuò)展接口,允許你在Bean實(shí)例化的過程中插入自定義邏輯,本文給大家介紹spring中BeanPostProcessor的作用,感興趣的朋友一起看看吧
    2025-04-04
  • Java中public關(guān)鍵字用法詳細(xì)講解

    Java中public關(guān)鍵字用法詳細(xì)講解

    這篇文章主要給大家介紹了關(guān)于Java中public關(guān)鍵字用法的相關(guān)資料,public關(guān)鍵字是和訪問權(quán)限相關(guān)的,它所修飾的方法對(duì)所有類都是可以訪問的,需要的朋友可以參考下
    2023-09-09
  • 關(guān)于springboot響應(yīng)式編程整合webFlux的問題

    關(guān)于springboot響應(yīng)式編程整合webFlux的問題

    在springboot2.x版本中提供了webFlux依賴模塊,該模塊有兩種模型實(shí)現(xiàn):一種是基于功能性端點(diǎn)的方式,另一種是基于SpringMVC注解方式,今天通過本文給大家介紹springboot響應(yīng)式編程整合webFlux的問題,感興趣的朋友一起看看吧
    2022-01-01
  • java巧用@Convert實(shí)現(xiàn)表字段自動(dòng)轉(zhuǎn)entity

    java巧用@Convert實(shí)現(xiàn)表字段自動(dòng)轉(zhuǎn)entity

    本文主要介紹了java巧用@Convert實(shí)現(xiàn)表字段自動(dòng)轉(zhuǎn)entity,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07

最新評(píng)論

眉山市| 达日县| 万年县| 宜宾市| 临漳县| 安新县| 辽宁省| 仙桃市| 乐亭县| 潞西市| 永安市| 静宁县| 西昌市| 公主岭市| 宁强县| 台前县| 横山县| 中江县| 三都| 舟曲县| 锦州市| 济南市| 中江县| 武安市| 怀化市| 兴安县| 长垣县| 兴和县| 绍兴县| 甘孜| 凤阳县| 承德县| 台江县| 海口市| 九台市| 察哈| 珠海市| 营口市| 巴彦淖尔市| 无极县| 莱州市|