高可用服务熔断降级代码示例
产品类型降级处理
package com.youfan.hystrix;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.youfan.model.ProductType;
import com.youfan.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2018/6/18 0018.
*/
@Service
public class ProductTypeServicehy {
@Autowired
ProductTypeService productTypeService ;
@HystrixCommand(fallbackMethod = "listproductTypeFallback")
public List<ProductType> listproductType(){
return productTypeService.listproductType();
}
public List<ProductType> listproductTypeFallback(){
ProductType productType = new ProductType();
productType.setId(1);
productType.setParentid(-1);
productType.setProducttypedescription("吃的");
productType.setProducttypename("食品18");
productType.setTypegrade("1");
List<ProductType> list = new ArrayList<ProductType>();
list.add(productType);
return list;
}
}
说明:
1)当listproductType访问失败后,会访问降级处理listproductTypeFallback;
产品首页显示产品信息
@Autowired
ProductTypeServicehy productTypeServicehy;
@RequestMapping(value = "/indexproduct",method = RequestMethod.GET)
public String sayHi(Model model , @RequestParam int productytpeid){
List<ProductType> list = productTypeServicehy.listproductType();
model.addAttribute("productypelist",list);
System.out.println(list);
if(productytpeid == -1){
productytpeid = list.get(0).getId();
}
List<Product> productlist = productService.listproduct(productytpeid);
model.addAttribute("produclist",productlist);
return "list";
}
==============================
QQ群:143522604
群里有相关资源
欢迎和大家一起学习、交流、提升!
==============================