高可用服务熔断降级代码示例

 

高可用服务熔断降级代码示例

 

 

产品类型降级处理

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
群里有相关资源
欢迎和大家一起学习、交流、提升!
==============================

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、Spring Cloud Hystrix 1. 什么是 Hystrix Hystrix 是一个延迟和容错库,用于隔离依赖服务的访问点,以防止这些依赖服务的故障导致雪崩效应。它提供了熔断、限流和降级等机制,保障了对依赖服务的访问。 2. Hystrix的核心概念 熔断:在一段时间内,如果服务的错误比例超过了设定的阈值,那么这个服务就会被熔断,示例代码: ``` @HystrixCommand(fallbackMethod = "fallbackMethod") public String method() { int i = new Random().nextInt(10); if (i % 2 == 0) { throw new RuntimeException("error"); } return "success"; } public String fallbackMethod(){ return "fallback"; } ``` 限流:在一段时间内,如果服务的请求数量超过了设定的阈值,那么这个服务就会被限流,示例代码: ``` @HystrixCommand(fallbackMethod = "fallbackMethod", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500"), @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"), @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "30000")}) public String method() { return "success"; } public String fallbackMethod(){ return "fallback"; } ``` 降级:在一段时间内,如果依赖服务不可用,那么就会调用预先备选的服务逻辑或返回预先设定的响应,示例代码: ``` @HystrixCommand(fallbackMethod = "fallbackMethod") public String method() { return restTemplate.getForObject("http://service-provider/method", String.class); } public String fallbackMethod(){ return "fallback"; } ``` 3. Hystrix的集成 添加依赖 ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> ``` 开启熔断 ``` @SpringBootApplication @EnableCircuitBreaker public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 添加注解 ``` @Service public class Service { @HystrixCommand(fallbackMethod = "fallbackMethod") public String method() { return "success"; } public String fallbackMethod() { return "fallback"; } } ``` 4. 测试 启动服务后,访问 http://localhost:8080/method,可以看到服务正常返回"success"。将calledOnce设置为false并再次访问该URL,可以看到服务返回"fallback"。 二、Spring Cloud Gateway 1. 什么是 Gateway Gateway 是 Spring Cloud 提供的一种 API 网关服务,基于 Spring 5.0、Spring Boot 2.0 和 Project Reactor 等技术开发。 2. Gateway 的三种机制 熔断:默认使用 Hystrix 进行熔断,示例代码: ``` spring.cloud.gateway.routes.id=route1 spring.cloud.gateway.routes.uri=http://httpbin.org:80 spring.cloud.gateway.routes.predicate[0]=Host=**.somehost.org spring.cloud.gateway.routes.predicate[1]=Path=/get spring.cloud.gateway.routes.filters[0]=Hystrix=hystrixCommandName ``` 限流:使用 RequestRateLimiter Gateway Filter 进行限流,示例代码: ``` spring.cloud.gateway.routes[0].id=count_route spring.cloud.gateway.routes[0].uri=http://httpbin.org:80 spring.cloud.gateway.routes[0].predicates[0]=Path=/get spring.cloud.gateway.routes[0].filters[0]=RequestRateLimiter=my-limiter-key,2,10,PT1S ``` 降级:可以使用 Hystrix 进行降级示例代码: ``` spring.cloud.gateway.routes[0].id=test_route spring.cloud.gateway.routes[0].uri=http://localhost:9090 spring.cloud.gateway.routes[0].predicates[0]=Path=/test spring.cloud.gateway.routes[0].filters[0]=Hystrix=mycommand ``` 3. Gateway 的集成 添加依赖 ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> ``` 4. 测试 启动服务后,访问 http://localhost:8080/get,可以看到服务正常返回。将请求频率配置为 0.1s,再次访问该 URL,可以看到服务返回 429 Too Many Requests。 参考资料: 1. Spring Cloud Hystrix 官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.2.1.RELEASE/reference/html/#spring-cloud-hystrix 2. Spring Cloud Gateway 官方文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值