瘋狂Spring Cloud連載(21)Feign與Hystrix整合

 本文節選自《瘋狂Spring Cloud微服務架構實戰》

京東購買地址:https://item.jd.com/12256011.html

噹噹網購買地址:http://product.dangdang.com/25201393.html

Spring Cloud教學視頻http://blog.csdn.net/boxiong86/article/details/78399104

Spring Cloud電子書http://blog.csdn.net/boxiong86/article/details/78488226

 

FeignHystrix整合

FeignHystrix提供了支持,爲“服務調用者”加入以下Feign依賴:

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</artifactId>

</dependency>

application.yml中打開FeignHystrix開關,請見以下配置:

feign:

  hystrix:

    enabled: true

在應用啓動類裏面,加入開Feign的開關,本小節的“服務調用者”應啓動類,所使用的註解如下:

@SpringBootApplication

@EnableDiscoveryClient

@EnableCircuitBreaker

@ServletComponentScan

@EnableFeignClients

新建Feign接口,調用“服務提供者(spring-hystrix-provider)”的“/hello”服務,請見代碼清單6-24

代碼清單6-24

codes\06\6.4\spring-hystrix-invoker\src\main\java\org\crazyit\cloud\feign\HelloClient.java

@FeignClient(name = "spring-hystrix-provider", fallback = HelloClientFallback.class)

public interface HelloClient {

 

@RequestMapping(method = RequestMethod.GET, value = "/hello")

public String hello();

 

@Component

static class HelloClientFallback implements HelloClient {

 

public String hello() {

System.out.println("hello方法的回退");

return "error hello";

}

}

}

與普通的Feign客戶端無異,僅僅設置了處理回退的類,回退類實現客戶端接口。爲了能測試效果,修改服務器端的“/hello”服務,讓其有800毫秒的延時。根據前面章節可知,默認情況下,Hystrix的超時時間爲1秒,因此,還需要修改配置超時配置。代碼清單6-25,在application.yml中修改命令配置。

代碼清單6-25codes\06\6.4\spring-hystrix-invoker\src\main\resources\application.yml

hystrix:

  command:

    HelloClient#hello():

      execution:

        isolation:

          thread:

            timeoutInMilliseconds: 500

      circuitBreaker:

        requestVolumeThreshold: 3

注意,如果是針對全局配置,則使用與下面類似的配置片斷:

hystrix.command.default.circuitBreaker.requestVolumeThreshold // 默認時間段內發生的請求數

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds // 超時時間

如果針對某個客戶端,如使用下面的配置片斷:

hystrix.command.CommandKey.circuitBreaker.requestVolumeThreshold

FeignHystrix整合使用時,會自動幫我們生成CommandKey,格式爲:“Feign客戶端接口名#方法名()”。例如本例中的客戶端爲HelloClient,方法爲hello,生成的CommandKey爲“HelloClient#hello()”。而默認情況下,生成的GroupKey@FeignClient註解的name屬性。

以上的配置中,我們針對了hello方法,設置了超時時間爲500毫秒,而“/hello”服務超時時間爲800毫秒,換言之,hello方法總會超時。另外,如果請求超過3並且失敗率超過50%,斷路器將被打開。編寫控制器,調用hello服務,並查看斷路器的情況,請見代碼清單6-26

代碼清單6-26

codes\06\6.4\spring-hystrix-invoker\src\main\java\org\crazyit\cloud\feign\HelloController.java

@RestController

public class HelloController {

 

@Autowired

HelloClient helloClient;

 

@RequestMapping(value = "/feign/hello", method = RequestMethod.GET)

public String feignHello() {

// hello方法會超時

String helloResult = helloClient.hello();

//獲取斷路器

HystrixCircuitBreaker breaker = HystrixCircuitBreaker.Factory

.getInstance(HystrixCommandKey.Factory

.asKey("HelloClient#hello()"));

System.out.println("斷路器狀態:" + breaker.isOpen());

return helloResult;

}

}

控制器的方法中,獲取了hello方法的斷路器,並輸出其狀態。接下來,編寫一個測試客戶端,多線程訪問:http://localhost:9000/feign/hello/{index},也就是控制器的feignHello方法,客戶端請見代碼清單6-27

代碼清單6-27

06\6.4\spring-hystrix-invoker\src\main\java\org\crazyit\cloud\feign\TestFeignClient.java

public class TestFeignClient {

public static void main(String[] args) throws Exception {

//創建默認的HttpClient

final CloseableHttpClient httpclient = HttpClients.createDefault();

//調用多次服務並輸出結果

for(int i = 0; i < 6; i++) {

//建立線程訪問接口

Thread t = new Thread() {

public void run() {

try {

String url = "http://localhost:9000/feign/hello";

//調用GET方法請求服務

HttpGet httpget = new HttpGet(url);

//獲取響應

HttpResponse response = httpclient.execute(httpget);

//根據 響應解析出字符串

System.out.println(EntityUtils.toString(response.getEntity()));

} catch (Exception e) {

e.printStackTrace();

}

}

};

t.start();

}

//等待完成

Thread.sleep(15000);

}

}

完成後,依次啓動Eureka服務器、服務提供者、服務調用者,運行代碼清單6-27,可看到“服務計用者”的控制檯輸出如下:

斷路器狀態:false

斷路器狀態:false

斷路器狀態:false

斷路器狀態:false

斷路器狀態:true

斷路器狀態:true

根據輸出可知,斷路器已經被打開。

 

 

 

 

 本文節選自《瘋狂Spring Cloud微服務架構實戰》

Spring Cloud教學視頻http://blog.csdn.net/boxiong86/article/details/78399104

Spring Cloud電子書http://blog.csdn.net/boxiong86/article/details/78488226

本書代碼共享地址:https://gitee.com/yangenxiong/SpringCloud

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章