1.线程隔离,服务降级(服务的消费方做降级处理)
当服务繁忙时,如果服务出现异常,不是粗暴的直接报错,而是返回一个友好的提示,虽然拒绝了用户的访问,但是会返回一个结果。
这就好比去买鱼,平常超市买鱼会额外赠送杀鱼的服务。等到逢年过节,超时繁忙时,可能就不提供杀鱼服务了,这就是服务的降级。
系统特别繁忙时,一些次要服务暂时中断,优先保证主要服务的畅通,一切资源优先让给主要服务来使用,在双十一、618时,京东天猫都会采用这样的策略。
pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
UserConsumerApplication.java
package cn.itcast.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
//@EnableDiscoveryClient
//@SpringBootApplication
//@EnableCircuitBreaker //服务的熔断
@SpringCloudApplication //SpringCloudApplication可以替代上面三个
public class UserConsumerDemoApplication {
@Bean
@LoadBalanced //负载均衡
public RestTemplate restTemplate() {
// 这次我们使用了OkHttp客户端,只需要注入工厂即可
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
public static void main(String[] args) {
SpringApplication.run(UserConsumerDemoApplication.class, args);
}
}
3.UserHController.java
package cn.itcast.user.controller; import cn.itcast.user.pojo.User; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("consumerH") public class UserHController { @Autowired private RestTemplate restTemplate; @GetMapping("{id}") @HystrixCommand(fallbackMethod = "queryUserByIdFallback") //单个方法的超时返回 public String queryUserById(@PathVariable("id") Long id){ String url = "http://user-service/user/" + id; //User user = restTemplate.getForObject(url, User.class); //return user; String user = restTemplate.getForObject(url, String.class); return user; } public String queryUserByIdFallback(@PathVariable("id") Long id){ return "用户信息查询出现异常!"; // User user = new User(); // user.setId(id); // user.setNickName("用户信息查询出现异常!"); // return user; } }
==========================
配置统一超时信息
package cn.itcast.user.controller;
import cn.itcast.user.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("consumerH")
@DefaultProperties(defaultFallback = "queryUserByIdFallback")
public class UserHController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("{id}")
//@HystrixCommand(fallbackMethod = "queryUserByIdFallback")
@HystrixCommand
public String queryUserById(@PathVariable("id") Long id){
String url = "http://user-service/user/" + id;
//User user = restTemplate.getForObject(url, User.class);
//return user;
String user = restTemplate.getForObject(url, String.class);
return user;
}
//public String queryUserByIdFallback(@PathVariable("id") Long id){
//return "用户信息查询出现异常!";
// User user = new User();
// user.setId(id);
// user.setNickName("用户信息查询出现异常!");
// return user;
//}
public String queryUserByIdFallback(){
return "用户信息查询出现异常!";
}
}
=============================================================
package cn.itcast.user.controller;
import cn.itcast.user.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("consumerH")
@DefaultProperties(defaultFallback = "queryUserByIdFallback")
public class UserHController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("{id}")
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000"), //响应超时时间
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
})
public String queryUserById(@PathVariable("id") Long id){
if(id % 2 ==0){
throw new RuntimeException("");
}
String url = "http://user-service/user/" + id;
String user = restTemplate.getForObject(url, String.class);
return user;
}
public String queryUserByIdFallback(){
return "用户信息查询出现异常!";
}
}