本文節選自《瘋狂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
Feign與Hystrix整合
Feign對Hystrix提供了支持,爲“服務調用者”加入以下Feign依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
在application.yml中打開Feign的Hystrix開關,請見以下配置:
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-25:codes\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
Feign與Hystrix整合使用時,會自動幫我們生成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