Live2D

SpringCloud微服务框架三

Springcloud微服务框架一


Springcloud微服务框架二



39.Sentinel服务熔断Ribbon环境预说

sentinel整合ribbon+openFeign+fallback

Ribbon系列

  • 启动nacos和sentinel
  • 提供者9003/9004
  • 消费者84
提供者9003/9004
1.新建cloudalibaba-provider-payment9003/9004
POM内容
<dependencies>
    <!--SpringCloud ailibaba nacos -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- SpringBoot整合Web组件 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--日常通用jar包配置-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
yml文件内容
server:
  port: 9003

spring:
  application:
    name: nacos-provdier-Payment
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 #nacos注册中心地址
#暴露监控
management:
  endpoints:
    web:
      exposure:
        include: '*'

主启动类
package com.wl.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @Autor: wl
 * @Date: 2021-11-19     13:06
 * @Version 1.0
 * @Describe
 */
@SpringBootApplication
@EnableDiscoveryClient
public class Payment9003Main {
    public static void main(String[] args) {
            SpringApplication.run(Payment9003Main.class,args);
        }
}
实体类
package com.wl.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Autor: wl
 * @Date: 2021-10-24     16:46
 * @Version 1.0
 * @Describe
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {  //泛型:如果装的payment 返回payment,装的order 返回order

    private Integer code;
    private String message;
    private T data;

    public CommonResult(Integer code, String message){
        this(code,message,null);
    }
}
package com.wl.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @Autor: wl
 * @Date: 2021-11-19     13:16
 * @Version 1.0
 * @Describe
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Payment {
    private Long id;
    private String  flow;
}
业务类
package com.wl.springcloud.controller;
import com.wl.springcloud.entities.CommonResult;
import com.wl.springcloud.entities.Payment;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

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

    //模拟数据库
    public static HashMap<Long,Payment> hashMap = new HashMap<>();
    static
    {
        hashMap.put(1L,new Payment(1L,"28a8c1e3bc2742d8848569891fb42181"));
        hashMap.put(2L,new Payment(2L,"bba8c1e3bc2742d8848569891ac32182"));
        hashMap.put(3L,new Payment(3L,"6ua8c1e3bc2742d8848569891xt92183"));
    }

    @GetMapping(value = "/paymentSQL/{id}")
    public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id)
    {
        Payment payment = hashMap.get(id);
        CommonResult<Payment> result = new CommonResult(200,"from mysql,serverPort:  "+serverPort,payment);
        return result;
    }

}

40.消费者84

新建cloudalibaba-consumer-nacos-order84
pom内容
<dependencies>
    <!--SpringCloud openfeign -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <!--SpringCloud ailibaba nacos -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!--SpringCloud ailibaba sentinel -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    <!-- SpringBoot整合Web组件 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--日常通用jar包配置-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
yml内容
server:
  port: 84
spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 #注册进nacos
    sentinel:
      transport:
        dashboard: 127.0.0.1:8080
        clientIp: 127.0.0.1
        port: 8719
#消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
service-url:
  nacos-user-service: nacos-provdier-Payment

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

主启动
package com.wl.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @Autor: wl
 * @Date: 2021-11-19     13:28
 * @Version 1.0
 * @Describe
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //开启Feign
public class Consumer84Main {
    public static void main(String[] args) {
            SpringApplication.run(Consumer84Main.class,args);
        }
}
业务类
package com.wl.springcloud.controller;

import com.wl.springcloud.entities.CommonResult;
import com.wl.springcloud.entities.Payment;
import com.wl.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
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.RestController;

import javax.annotation.Resource;

/**
 * @Autor: wl
 * @Date: 2021-11-19     13:42
 * @Version 1.0
 * @Describe
 */
@RestController
@Slf4j
public class Order84FeignController {


    @Resource
    private PaymentService paymentService;

    @RequestMapping("/consumer/fallback/{id}")
    public CommonResult<Payment> fallback(@PathVariable("id") Long id) {
        CommonResult<Payment> result = paymentService.fallback(id);
        return result;
    }



}

package com.wl.springcloud.service;

import com.wl.springcloud.entities.CommonResult;
import com.wl.springcloud.entities.Payment;
import com.wl.springcloud.fallback.PaymentFallbackService;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @Autor: wl
 * @Date: 2021-11-21     15:07
 * @Version 1.0
 * @Describe
 */
@Component
@FeignClient(value = "nacos-provdier-Payment",fallback = PaymentFallbackService.class)
public interface PaymentService {
    @GetMapping("/paymentSQL/{id}")
    CommonResult<Payment> fallback(@PathVariable("id") Long id);
}
降级的类
package com.wl.springcloud.fallback;

import com.wl.springcloud.entities.CommonResult;
import com.wl.springcloud.entities.Payment;
import com.wl.springcloud.service.PaymentService;
import org.springframework.stereotype.Component;

/**
 * @Autor: wl
 * @Date: 2021-11-21     15:43
 * @Version 1.0
 * @Describe
 */
@Component
public class PaymentFallbackService implements PaymentService {
    @Override
    public CommonResult<Payment> fallback(Long id) {
        return new CommonResult<>(44444,"服务降级返回,---PaymentFallbackService",new Payment(id,"errorSerial"));
    }
}
实体类
package com.wl.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Autor: wl
 * @Date: 2021-10-24     16:46
 * @Version 1.0
 * @Describe
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {  //泛型:如果装的payment 返回payment,装的order 返回order

    private Integer code;
    private String message;
    private T data;

}
package com.wl.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @Autor: wl
 * @Date: 2021-11-19     13:16
 * @Version 1.0
 * @Describe
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Payment {
    private Long id;
    private String  flow;
}

省略测试sentinelResource和fallback注解...

总结:
  • fallback一般用于处理error页面的,blockhandler一般用于处理违规配置
  • 当两个条件同时生效的时候,fallback的优先级更大一些
熔断框架对比
- Sentinel Hystrix resilience4j
隔离策略 信号量隔离(并发线程数限流) 线程池隔商/信号量隔离 信号量隔离
熔断降级策略 基于响应时间、异常比率、异常数 基于异常比率 基于异常比率、响应时间
实时统计实现 滑动窗口(LeapArray) 滑动窗口(基于RxJava) Ring Bit Buffer
动态规则配置 支持多种数据源 支持多种数据源 有限支持
扩展性 多个扩展点 插件的形式 接口的形式
基于注解的支持 支持 支持 支持
限流 基于QPS,支持基于调用关系的限流 有限的支持 Rate Limiter
流量整形 支持预热模式匀速器模式、预热排队模式 不支持 简单的Rate Limiter模式
系统自适应保护 支持 不支持 不支持
控制台 提供开箱即用的控制台,可配置规则、查看秒级监控,机器发观等 简单的监控查看 不提供控制台,可对接其它监控系统

41.Sentinel持久化规则

是什么?

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

步骤

修改cloudalibaba-sentinel-service8401

在pom中添加以下内容
<!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
yml文件内容:
server:
  port: 8401
spring:
  application:
    name: cloud-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 # nacos注册中心地址
    sentinel:
     transport:
       dashboard: 127.0.0.1:8080 #sentinel注册中心地址
       port: 8719 #给nacos发送心跳的端口号
       clientIp: 127.0.0.1 #监控的ip
     datasource: #<---------------------------关注点,添加Nacos数据源配置
       ds1:
         nacos:
           server-addr: localhost:8848
           dataId: cloudalibaba-sentinel-service
           groupId: DEFAULT_GROUP
           data-type: json
           rule-type: flow
#监控暴露
management:
  endpoints:
    web:
      exposure:
        include: '*'

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

然后在nacos中配置以下内容
image

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

完成sentinel的持久化

posted @ 2021-11-19 10:44  没有梦想的java菜鸟  阅读(44)  评论(0编辑  收藏  举报