压力分析
cloud-consumer-feign-hystrix-order8084是消费模块,cloud-provider-hystrix-payment8008是支付模块。目前我们使用了Jmeter对支付模块进行压力测试同时还有消费模块通过PRC的方式进行服务代用。这时我们的支付模块已经压力很大了,会产生报错或等待响应的问题。我们不希望直接产生报错或超时等待的问题,希望能立即返回正确的消息或稍后提示。
服务降级
在支付模块的服务降级
更改业务类
package com.gcl.springcloud.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class HystrixService {
public String paymentInfo_ok(Integer id){
return "线程池: "+ Thread.currentThread().getName() + " paymentInfo_ok " + id;
}
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler"/*指定善后方法名*/,commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000")
})
public String paymentInfo_timout(Integer id){
try{
TimeUnit.SECONDS.sleep( 5 );
}catch (Exception e){
e.printStackTrace();
}
return "线程池: "+ Thread.currentThread().getName() + " paymentInfo_timout " + id;
}
public String paymentInfo_TimeOutHandler(Integer id){
return "业务繁忙,请稍后再试";
}
}
更改启动类
package com.gcl.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentMain8008 {
public static void main(String[] args) {
SpringApplication.run( PaymentMain8008.class , args );
}
}
运行截图
在消费模块的服务降级
更改yml
server:
port: 8084
eureka:
client:
register-with-eureka: false
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:7001/eureka/
feign:
hystrix:
enabled: true
更改主启动类
package com.gcl.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
public class OrderMain8084 {
public static void main(String[] args) {
SpringApplication.run( OrderMain8084.class , args );
}
}
业务类
package com.gcl.springcloud.controller;
import com.gcl.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@Slf4j
public class OrderHystirxController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id)
{
String result = paymentHystrixService.paymentInfo_OK(id);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
})
public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfo_TimeOut(id);
return result;
}
//善后方法
public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
return "我是消费者8084,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
}
}