sentinel

image

image

image

imageimage

下载地址:https://github.com/alibaba/Sentinel/releases
image

下载启动的地址:

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -jar sentinel-dashboard-1.8.2.jar


image

代码示例:

package com.yxkj.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yxkj.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @USER: 
 * @DATE: 2021-06-14
 * @description:功能描述
 */
@RestController
@RequestMapping(value = "/order")

/*
* @DefaultProperties:指定次接口中公共的熔断设置
* 如果在@DefaultProperties指定了公共的降级方法
* 在@HystrixCommand不需要单独指定
* */
@DefaultProperties(defaultFallback = "defaultFallback")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;
 /*
 * @SentinelResource
 * blockHandler :申明熔断的调用方法
 * fallback:执行抛出异常的降级方法
 * */
    @SentinelResource(value = "orderById",blockHandler ="orderBlockHandler",fallback = "orderFallback")
    @RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
    public Product findById(@PathVariable Long id){
        return restTemplate.getForObject("http://service-product/product/1",Product.class);

    }
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public String findOrder(@PathVariable Long id){
        return "根据id查询订单";

    }
    /*
    定义降级逻辑
    hystrix和sentinel
      熔断执行的降级方法
      抛出异常执行的降级方法

     */
    public Product orderBlockHandler(Long id){
        Product product = new Product();
        product.setProductName("触发熔断降级的方法");
        return  product;
    }

    public Product orderFallback(Long id){
        Product product = new Product();
        product.setProductName("触发异常执行的降级方法");
        return  product;
    }


}

posted @ 2021-07-08 18:43  风飘落叶  阅读(101)  评论(0编辑  收藏  举报