10 Sentinel简单入门

 Hystrix的替换方案

 10.1 替换方案介绍

  10 Sentinel简单入门_第1张图片

10.2 Sentinel概述

10.2.1 Sentinel简介

10 Sentinel简单入门_第2张图片

10 Sentinel简单入门_第3张图片

10 Sentinel简单入门_第4张图片

10.2.2 服务熔断区别

10 Sentinel简单入门_第5张图片

10.2.3 迁移方案

10 Sentinel简单入门_第6张图片

 10.2.4 名词解释

10 Sentinel简单入门_第7张图片

 

10.3 Sentinel中的管理控制台

 10.3.1 下载启动控制台

 1 下载地址

https://github.com/alibaba/Sentinel/releases/download/1.6.3/sentinel-dashboard-1.6.3.jar

2 启动

java -Dserver.port=8080 -DCSP.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.6.3.jar

其中-Dserver.port=8080用于指定Sentinel控制台端口8080。

从Sentinel1.6.0起,Sentinel控制台引入基本的登录功能,默认用户密码都是sentinel。

启动Sentinel需要JDK1.8及以上版本。

10 Sentinel简单入门_第8张图片

10.3.2 将服务交给控制台管理

客户端接入Sentinel管理控制平台

1 在客户端引入依赖

在父项目中引入

 
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Greenwich.RELEASE
                pom
                import
            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.1.0.RELEASE
                pom
                import
            
        
    

10 Sentinel简单入门_第9张图片

在子项目中引入

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        

 

2 在客户端application.properties配置启动参数

#配置sentinel 接入sentinel平台
spring.cloud.sentinel.transport.dashboard=localhost:8080

 3 重新启动程序

 10 Sentinel简单入门_第10张图片

 

10.3.3 查看机器列表以及健康情况

10 Sentinel简单入门_第11张图片

10.4 通用资源保护

 

10.4.1 熔断流程

10 Sentinel简单入门_第12张图片

10.4.2 熔断注入

 

package xx.study.sc.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;


import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    //注入
    @Autowired
    private RestTemplate restTemplate;

    /**
     * @SentinelResource
     *   blockHandler:声明熔断降级方法
     *   fallback:声明异常执行的降级方法
     *  value :自定义的资源名称 不设置的话默认当前类名.方法名
     * 调用远程服务
     */
     @SentinelResource(blockHandler = "orderFallBack",fallback = "orderErrorFallBack")
    @RequestMapping(value = "/buyBanana",method = RequestMethod.GET)
    public String  buyBanana(@RequestParam String name){
        name=restTemplate.getForObject("http://service-product/product/buy?name= "+name,String.class);
        String returnVal="从注册中心收到"+name+"!!!";
        return returnVal;
    }

    /**
     * 定义降级逻辑
     *    熔断执行的降级方法
     *    抛出异常的执行方法
     *   和需要收到的方法返回值一致 方法参数一致
     * @return
     */
    public String orderErrorFallBack(String name){

       return name+"报错了,降级了";
    }
    public String orderFallBack(String name){

        return name+"熔断了,降级了";
    }
    /**
     * 指定同一降级方法
     */
    public String defaultFallBack(){

        return "降级了";
    }



}

10 Sentinel简单入门_第13张图片 

10 Sentinel简单入门_第14张图片

 

 10.4.3 Sentinel控制台配置规则

10 Sentinel简单入门_第15张图片

10.5 本地配置

由于服务放在内存中的重启后会丢失之前的配置,可以在本地配置解决此问题。

10 Sentinel简单入门_第16张图片

10 Sentinel简单入门_第17张图片

在此类中可找到Json变量定义

10 Sentinel简单入门_第18张图片 

10.6 对RestTemplate的支持

 1 在创建RestTemplate对象时构造

package xx.study.sc;

import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import xx.study.sc.exception.ExceptionUtils;

@SpringBootApplication

public class RestOrderApplication {
    /**
     * sentinel支持对resttemplate的服务调用使用sentinel方法。在构造RestTemplate对象的
     * 时候,只需要加载@SentinelRestTemplate即可
     * 资源名   httpmethod:sechema://host:port/path:协议、主机、端口和路径
     * @SentinelRestTemplate
     *   异常降级:
     *      fallback :降级方法
     *      fallbackClass :降级配置类
     *   限流熔断
     *      blockHandler
     *      blockHandlerClass
     *
     *
     */
    @LoadBalanced
    @Bean
    @SentinelRestTemplate(fallback = "handleFallback",fallbackClass = ExceptionUtils.class,
            blockHandler = "handleBlock",blockHandlerClass = ExceptionUtils.class)
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }


    public static void main(String[] args) {
        SpringApplication.run(RestOrderApplication.class);
    }
}

 2 写降级配置类

package xx.study.sc.exception;

import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;

public class ExceptionUtils {
    /**
     * 限流熔断业务逻辑
     * @param request
     * @param body
     * @param execution
     * @param exception
     * @return
     */
    public static SentinelClientHttpResponse handleBlock(HttpRequest request, byte[] body, ClientHttpRequestExecution execution
    , BlockException exception ){
        return new SentinelClientHttpResponse("限流熔断降级");

    }

    /**
     * 异常降级业务逻辑
     * @param request
     * @param body
     * @param execution
     * @param exception
     * @return
     */
    public static SentinelClientHttpResponse handleFallback(HttpRequest request, byte[] body, ClientHttpRequestExecution execution
            , BlockException exception ){
        return new SentinelClientHttpResponse("限流熔断降级");

    }
}

 

10.7 对Feign的支持

1 引入依赖

      
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        

2 开启sentinel支持

application.properties 开启

#开启对sentinel的支持
feign.sentinel.enabled=true

3 配置FeignClient

跟Hystrix相同

package xx.study.sc.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**声明需要调用的微服务名称
 * @FeignClient
 *    name:服务提供者的名称
 *    fallback
 *
 */
@FeignClient(name="service-product",fallback = ProductFeignClientCallBack.class)
public interface ProductFeignClient {
    /**
     * 配置需要调用的微服务接口
     * 访问路径要写全  类+方法
     */
    @RequestMapping(value = "/product/buy",method = RequestMethod.GET)
    public String buy(@RequestParam String name);

}
package xx.study.sc.feign;

import org.springframework.stereotype.Component;

@Component
public class ProductFeignClientCallBack implements ProductFeignClient {
    @Override
    public String buy(String name) {
        return "feign调用,触发降级了";
    }
}
package xx.study.sc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xx.study.sc.feign.ProductFeignClient;


import java.util.List;

@RestController
@RequestMapping("/order")

public class OrderController {


    //idea提示红色 不用管
    @Autowired
    public ProductFeignClient productFeignClient;

    @RequestMapping(value = "/buyBanana",method = RequestMethod.GET)
    public String  buyBanana(@RequestParam String name){
        name=productFeignClient.buy(name);
        String returnVal="从feign收到"+name+"!!!";
        return returnVal;
    }



}

 

你可能感兴趣的:(SpringCloud)