SpringCloud Aliba-Sentinel【下篇】-从入门到学废【6】

java小课堂

== 和 equals 的区别是什么?

  • 对于基本类型,== 比较的是值;
  • 对于引用类型,==比较的是地址;
  • equals不能用于基本类型的比较;
  • 如果没有重写equals, equals就相当于 ==;
  • 如果重写了 equals方法,equals比较的是对象的内容;

SpringCloud Aliba-Sentinel【下篇】-从入门到学废【6】_第1张图片

目录

1.@SentinelResource

2. Ribbon系列

3.Fegin系列 

4.规则持久化 


1.@SentinelResource

1.1按资源名称限流

在对sentinel进行流控设置时,资源名称应与@SentinelResource的value值保持一致,设置blockHander返回兜底方法,否则返回sentinel默认的

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "defaultException")
    public CommonResult byResource(){
        return new CommonResult(200,"按资源名称限流",new Payment(2023,"serial001"));
    }

    public CommonResult defaultException(BlockException exception){
        return new CommonResult(404, exception.getClass().getCanonicalName()+"服务不可用");
    }

1.2按url地址限流

只设置@SentinelRsource的value值,使用url地址进行限流,返回sentinel默认的

    @GetMapping("/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult byUrl(){
        return new CommonResult(200,"按url限流",new Payment(2023,"serial001"));
    }

SpringCloud Aliba-Sentinel【下篇】-从入门到学废【6】_第2张图片 

1.3自定义限流类 

使用@SentinelResource的blockHandlerClass属性,定义兜底类,在使用blockHandler定义兜底的方法

   @GetMapping("/payment/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "handlerException2")
    public CommonResult customerBlockHandler(){
        return new CommonResult(200,"按自定义限流",new Payment(2023,"serial003"));
    }

SpringCloud Aliba-Sentinel【下篇】-从入门到学废【6】_第3张图片 

SpringCloud Aliba-Sentinel【下篇】-从入门到学废【6】_第4张图片 

2. Ribbon系列

准备:服务提供者9003,9004;服务消费者84

2.1服务提供者

1.建工程

1.再父工程下分别创建9003,9004服务提供者

2.注意jdk和maven版本

2.改pom

  
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            org.projectlombok
            lombok
        
        
            org.example
            cloud-api-commons
            1.0-SNAPSHOT
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
    

3.改yml

server:
  port: 9003

#服务名名称
spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        #nacos地址
        server-addr: 192.168.20.50:1111


management:
  endpoints:
    web:
      exposure:
        include: '*'

4.主启动

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9004 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain9004.class);
    }
}

5.业务类

@RestController
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;

    public static HashMap hashMap = new HashMap<>();

    static {
        hashMap.put(1L, new Payment(111, "小张"));
        hashMap.put(2L, new Payment(222, "小六"));
        hashMap.put(3L, new Payment(333, "小李"));
    }

    @GetMapping("/payment/{id}")
    public CommonResult getPayment(@PathVariable("id") Long id) {
        Payment payment = hashMap.get(id);
        CommonResult result = new CommonResult(200, "服务器端口:" + serverPort, payment);
        return result;
    }
}

6.测试 

SpringCloud Aliba-Sentinel【下篇】-从入门到学废【6】_第5张图片

2.2 服务消费者

1.建工程 

1.在父工程下创建服务84

2.注意jdk和maven版本

2.改pom


        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            org.projectlombok
            lombok
        
        
            org.example
            cloud-api-commons
            1.0-SNAPSHOT
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
    

3.改yml

server:
  port: 84

spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.20.50:1111
    sentinel:
      transport:
        dashboard: localhost:8080
        prot: 8719

4.主启动

@SpringBootApplication
@EnableDiscoveryClient
public class Order84 {
    public static void main(String[] args) {
        SpringApplication.run(Order84.class);
    }
}

5.RestTemplate配置类

@Configuration
public class MyConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

6.业务类

@RestController
public class ConsumerController {

    private static final String SERVER_NAME="http://nacos-payment-provider";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/consumer/fallback/{id}")
    @SentinelResource(value = "fallback")
    public CommonResult fallback(@PathVariable("id")Long id){
        CommonResult result = restTemplate.getForObject(SERVER_NAME + "/payment/" + id, CommonResult.class, id);
        if (result.getData()==null){
            throw new NullPointerException("没有记录");
        }
        return  result;
    }
}

7.测试 

SpringCloud Aliba-Sentinel【下篇】-从入门到学废【6】_第6张图片

2.3fallback属性 

  • fallback只管业务异常
  • @SentinelResource的fallBack属性,当有异常时,返回兜底方法handlerFallBack
   @GetMapping("/consumer/fallback/{id}")
    @SentinelResource(value = "fallback", fallback = "handlerFallBack")
    public CommonResult fallback(@PathVariable("id") int id) {
        CommonResult result = restTemplate.getForObject(SERVER_NAME + "/payment/" + id, CommonResult.class, id);
        if (result.getData() == null) {
            throw new NullPointerException("没有记录");
        }
        return result;
    }

    public CommonResult handlerFallBack(@PathVariable("id") int id, Throwable e) {
        Payment payment=new Payment(id,"null");
        return new CommonResult(404, "兜底方法," + e.getMessage(),payment);
    }

2.4blockhandler属性

  • blockhandler只负责Sentinel控制台配置违规
    @GetMapping("/consumer/fallback/{id}")
    @SentinelResource(value = "fallback", blockHandler = "blockHandler")
    public CommonResult fallback(@PathVariable("id") int id) {
        CommonResult result = restTemplate.getForObject(SERVER_NAME + "/payment/" + id, CommonResult.class, id);
        if (result.getData() == null) {
            throw new NullPointerException("没有记录");
        }
        return result;
    }

    public CommonResult blockHandler(@PathVariable("id") int id, BlockException blockException) {
        Payment payment = new Payment(id, "null");
        return new CommonResult(404, "blockHandler:" + blockException.getMessage(),payment);
    }

2.5fallback和blockhandler同时配置 

blockHandler和fallback都配置了,则被限流降级而抛出BlockException时只会进入blodkHandler处理逻辑。

    @GetMapping("/consumer/fallback/{id}")
    @SentinelResource(value = "fallback", blockHandler = "blockHandler", fallback = "handlerFallBack")
    public CommonResult fallback(@PathVariable("id") int id) {
        CommonResult result = restTemplate.getForObject(SERVER_NAME + "/payment/" + id, CommonResult.class, id);
        if (result.getData() == null) {
            throw new NullPointerException("没有记录");
        }
        return result;
    }

    public CommonResult blockHandler(@PathVariable("id") int id, BlockException blockException) {
        Payment payment = new Payment(id, "null");
        return new CommonResult(404, "blockHandler:" + blockException.getMessage(), payment);
    }


    public CommonResult handlerFallBack(@PathVariable("id") int id, Throwable e) {
        Payment payment = new Payment(id, "null");
        return new CommonResult(404, "兜底方法," + e.getMessage(), payment);
    }

3.Fegin系列 

1.改pom

添加依赖

        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

2.改yml

激活feign

#激活Sentinel对Feign的支持
feign:
  sentinel:
    enabled: true

3.加Feign接口

添加feign接口,与调用方法一致

@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackFeign.class)
public interface PaymentFeign {

    @GetMapping("/payment/{id}")
    public CommonResult getPayment(@PathVariable("id") Long id);
}

兜底方法 

@Component
public class PaymentFallbackFeign implements PaymentFeign{
    @Override
    public CommonResult getPayment(Long id) {
        return new CommonResult<>(404,"服务降级返回----PaymentFallbackFeign"); 
    }
}

4.主启动

添加@EnableFeignClients

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Order84 {
    public static void main(String[] args) {
        SpringApplication.run(Order84.class);
    }
}

5.业务类

  @Autowired
    private PaymentFeign paymentFeign;

    @GetMapping("/consumer/payment/{id}")
    public CommonResult getPayment(@PathVariable("id") Long id){
     return paymentFeign.getPayment(id);
    }

 6.测试

关闭服务提供者9003,9004,消费者84不会被耗死

SpringCloud Aliba-Sentinel【下篇】-从入门到学废【6】_第7张图片

4.规则持久化 

一旦我们重启应用,sentinel规则将消失,生产环境需要将配置规则进行持久化

1.改pom

        
        
            com.alibaba.csp
            sentinel-datasource-nacos
        

2.改yml

server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service

  cloud:
    nacos:
      discovery:
        #nacos服务注册中心
        server-addr: 192.168.20.50:1111
    sentinel:
      transport:
        #sentinel dashboard地址
        dashboard: localhost:8080
        #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
        port: 8719
        #将Sentinel流控规则持久化到nacos
      datasource:
        ds1:
          nacos:
            server-addr: 192.168.20.50:1111
            dataId: ${spring.application.name}
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow

management:
  endpoints:
    web:
      exposure:
        include: '*'

3.添加nacos配置 

  • resource:资源名称;
  • limitApp: 来源应用;
  • grade:阈值类型, 0表示线程数, 1表示QPS;
  • count:单机阈值;
  • strategy:流控模式,0表示直接,1表示关联,2表示链路;
  • controlBehavior:流控效果, 0表示快速失败,1表示Warm Up, 2表示排队待;
  • clusterMode:是否集群。

SpringCloud Aliba-Sentinel【下篇】-从入门到学废【6】_第8张图片

4.测试 

启动8401后,流控规则生效

SpringCloud Aliba-Sentinel【下篇】-从入门到学废【6】_第9张图片

SpringCloud Aliba-Sentinel【下篇】-从入门到学废【6】_第10张图片SpringCloud Aliba-Sentinel【下篇】-从入门到学废【6】_第11张图片

你可能感兴趣的:(spring,cloud,sentinel,java,后端,分布式,中间件,ribbon)