一 实际问题
数据查询失败怎么办?
二 传统解决方式
1 添加超时机制
2 人肉运维
三 容错框架Hystrix
Netflix提供了一个容错框架
四 改造集群
五 编写会员模块
1 编写控制器
package org.crazyit.cloud;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@RequestMapping(value = "/normalHello", method = RequestMethod.GET)
public String normalHello() {
return "Hello World";
}
@RequestMapping(value = "/errorHello", method = RequestMethod.GET)
public String errorHello() throws Exception {
Thread.sleep(10000);
return "Error Hello World";
}
}
2 启动会员模块
六 编写销售模块
1 添加依赖
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.crazyit.cloud</groupId>
<artifactId>first-hy-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<version>1.7.25</version>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
</project>
2 编写测试类
package org.crazyit.cloud;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClientMain {
public static void main(String[] args) throws Exception {
String url = "http://localhost:8080/normalHello";
HttpGet httpget = new HttpGet(url);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpResponse response = httpclient.execute(httpget);
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
3 测试结果
Hello World
4 添加HelloCommand类
package org.crazyit.cloud;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
public class HelloCommand extends HystrixCommand<String> {
public HelloCommand() {
super(HystrixCommandGroupKey.Factory.asKey("TestGroup"));
}
protected String run() throws Exception {
String url = "http://localhost:8080/normalHello";
HttpGet httpget = new HttpGet(url);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpResponse response = httpclient.execute(httpget);
return EntityUtils.toString(response.getEntity());
}
}
5 添加测试类
package org.crazyit.cloud;
public class NormalMain {
public static void main(String[] args) {
HelloCommand command = new HelloCommand();
String result = command.execute();
System.out.println(result);
}
}
6 测试输出
Hello World
三 添加回退逻辑
1 新增ErrorCommand类
package org.crazyit.cloud.error;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
public class ErrorCommand extends HystrixCommand<String> {
public ErrorCommand() {
super(HystrixCommandGroupKey.Factory.asKey("TestGroup"));
}
protected String run() throws Exception {
String url = "http://localhost:8080/errorHello";
HttpGet httpget = new HttpGet(url);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpResponse response = httpclient.execute(httpget);
return EntityUtils.toString(response.getEntity());
}
protected String getFallback() {
System.out.println("fall back method");
return "fall back hello";
}
}
2 测试类
package org.crazyit.cloud.error;
public class ErrorMain {
public static void main(String[] args) {
ErrorCommand command = new ErrorCommand();
String result = command.execute();
System.out.println(result);
}
}
3 测试结果
fall back method
fall back hello