springcloud中feign聲明式服務和hystrix請求熔斷和服務降級


一、配置

pom文件引入依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

啟動類配置

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class StartCloudFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartCloudFeignApplication.class,args);
    }
}

feign常用配置

feign:
  hystrix:
    enabled: true #在Feign中開啟Hystrix
  compression:
    request:
      enabled: false #是否對請求進行GZIP壓縮
      mime-types: text/xml,application/xml,application/json #指定壓縮的請求數據類型
      min-request-size: 2048 #超過該大小的請求會被壓縮
    response:
      enabled: false #是否對響應進行GZIP壓縮

hystrix:
  command:
default: #default全局有效,service id指定應用有效
execution:
timeout:
enabled: false
isolation:
thread:
timeoutInMilliseconds: 3000 #斷路器超時時間,默認1000ms

 二、服務降級示例

@FeignClient(name = "cloud-web1",fallback = CloudWebHystrixImpl.class)
public interface ICloudWebFeign {
    @GetMapping("/test1")
    public String test1();
}

 

@Component
public class CloudWebHystrixImpl implements ICloudWebFeign {
    @Override
    public String test1() {
        return "調用失敗,服務降級";
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM