天天看點

ribbon,feign選擇和與Hystrix的整合應用

ribbon,feign選擇和與Hystrix的整合應用

1.ribbon與feign的差別

  1. feign是在ribbon上封裝的
  2. ribbon請求需要自己拼接 而 Feign 是一個使用起來更加友善的 HTTP 客戶端,使用起來就像是調用自身工程的方法,而感覺不到是調用遠端方法。
    ribbon,feign選擇和與Hystrix的整合應用
  3. feign封裝了負載均衡功能
  4. 可以統一管理 友善複用
  5. 極簡整合hystrix

2.spring boot使用feign+hystrix

1.修改application類

@SpringBootApplication(scanBasePackages = "com.vanpeng.cms")
@ComponentScan("com.vanpeng.cms.*")
@EnableCaching
@EnableEurekaClient
@EnableSwagger2
@EnableFeignClients  #開啟feign
@EnableCircuitBreaker  #開啟hystrix  
//@EnableApolloConfig
public class CmsApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(CmsApplication.class, args);
    }
 
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
 
}      

2.修改application.yml

feign:
  hystrix:
    enabled: true  #hystrix預設關閉需要 開啟
ribbon:
  ConnectTimeout: 5000 # 請求連接配接的逾時時間 預設的時間為 1 秒
  ReadTimeout: 5000 # 請求處理的逾時時間
hystrix:
  command:
    default:  #default全局有效,service id指定應用有效
      execution:
        timeout:
          #如果enabled設定為false,則請求逾時交給ribbon控制,為true,則逾時作為熔斷根據
          enabled: true
        isolation:
          strategy: SEMAPHORE
          thread:
            timeoutInMilliseconds: 5000 #斷路器逾時時間,預設1000ms
      circuitBreaker:
        enabled: true
        requestVolumeThreshold: 10 #預設20 ,熔斷的門檻值,如何user服務報錯滿足3次,熔斷器就會打開,就算order之後請求正确的資料也不行。
        sleepWindowInMilliseconds: 8000 #預設5S , 等5S之後熔斷器會處于半開狀态,然後下一次請求的正确和錯誤講決定熔斷器是否真的關閉和是否繼續打開
        errorThresholdPercentage: 0.5      

3.服務間調用

@FeignClient(name = "data-service", fallback = DataFallBack.class) //表明是feign接口 name為被調用服務在erueka上的服務名
public interface DataServiceFeign {
 
    @PostMapping("/selectDataSetId") //對應着被調用服務的路徑   post表明為post請求 請求參數為@RequestBody ResultCatalog resultCatalog
    Result<Object> selectByDataSetId(@RequestBody ResultCatalog resultCatalog);
 
    @GetMapping("/resourceMenu/selectFeignService/{lecturerUserNo}") //表明為get請求 參數為 @PathVariable(value = "lecturerUserNo") Long lecturerUserNo
    Result<List<ResultCatalog>>
        selectService(@PathVariable(value = "lecturerUserNo") Long lecturerUserNo);
 
}      

4.熔斷hystrix

@Component
public class DataFallBack implements DataServiceFeign {  //需要實作feign接口 每個方法對應着每個feign調用的服務 
 
    @Override
    public Result<Object> selectByDataSetId(ResultCatalog resultCatalog) {
        Result<Object> result = new Result<Object>();
        String error = "調用系統失敗。";
        result.setCode(506);
        result.setMessage(error);
        return result;
    }
 
    @Override
    public Result<List<ResultCatalog>>
        selectServicel(Long lecturerUserNo) {
        Result<List<ResultCatalog>> result = new Result<>();
        String error = "調用系統失敗。";
        result.setCode(506);
        result.setMessage(error);
        return result;
    }
}      

使用依賴注入調用即可.

public class DataServiceController {
 
    @Autowired
    DataServiceFeign dataServiceFeign;
 
    @Autowired
    SystemPortalFeign systemPortalFeign;
 
    /**
     * ajax資料服務
     * 
     * @param resultMapResourceCatalog
     * @return
     */
    @PostMapping("/DataService")
    public Result
        DataService(@RequestBody ResultCatalog resultCatalog) {
        Result<Object> resultObj = dataServiceFeign.selectByDataSetId(resultCatalog);
       
    }
}      

繼續閱讀