springcloud09-SpringAlibaba

RM - 资源管理器简介

为什么会出现alibaba

因为整个nextflix项目进入了维护模式。意味着不在开发新的组件的。只会进行打的block级别的bug维护。所有的项目都会被新的功能代替

官网:https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md

Nacos服务注册中心与配置中心

1、简介

为什么叫Nacos :前四个字母分 别为Naming和Configuration的前两个字母,最后的s为Service.

是什么:

  • 一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
  • Nacos: Dynamic Naming and Configuration Service
  • Nacos就是注册中心+配置中心的组合

能干嘛

  • 替代Eureka做服务注册中心
  • 替代Config做服务配置中心

下载地址:https://nacos.io

官方文档:https://nacos.io

2、安装并运行

1、本地Java8 + Maven环境E经OK
先从官网下载Nacos

解压安装包,直接运行bin目录下的startup.cmd
命令运行成功后直接访问http://localhost:8848/nacos

结果页面
image-20210219125232103.png

(1)、初始化项目9101

    <dependencies>
        <!-- SpringCloud ailibaba nacos-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <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>
        <!--热部署-->
        <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>
server:
  port: 9101
spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
management:
  endpoints:
    web:
      exposure:
        include: "*"
@SpringBootApplication
@EnableDiscoveryClient
public class Payment9101 {
    public static void main(String[] args) {
        SpringApplication.run(Payment9101.class,args);
    }
}
@RestController
public class PyamnetController {
    @Value("${server.port}")
    private String serverport;

    @GetMapping("/payment/port")
    public String getServerport(){
        return "微服务 "+ serverport+"运行成功";
    }
}

同样配置9002服务

(2)、技术技巧

启动微服务的时候可以使用新的技术,在想要复制的类上右键选择copy的选项就可以找到这个界面,这个界面可以配置一个以已经存在的代码为基础的不同配置的服务。我们如果不想穿件新的微服务可以使用这个来创建一个虚拟的微服务。方便测试。
image-20210219135844745.png

(3)、初始化项目83(微服务消费者)

<dependencies>
    <!-- SpringCloud ailibaba nacos-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency><!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
        <groupId>com.dkf.cloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <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>
    <!--热部署-->
    <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>
server:
  port: 83

spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #配置Nacos地址

#消费者将去访问的微服务名称(注册成功进nacos的微服务提供者)
service-url:
  nacos-user-service: http://nacos-payment-provider
@SpringBootApplication
@EnableDiscoveryClient
public class OrderClient83 {
    public static void main(String[] args) {
        SpringApplication.run(OrderClient83.class,args);
    }
}
@RestController
@Slf4j
public class OrderNacosController {
    @Resource
    private RestTemplate restTemplate;

    @Value("${service-url.nacos-use-service}")
    private String serverURL;

    @GetMapping("/consumer/payment/nacos")
    public String paymentInfo(){
        return restTemplate.getForObject(serverURL+"/payment/nacos",String.class);
    }
}
@Configuration
public class ApplicationContextConfig {

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

3、服务中心的对比:

image-20210219192809017.png

nacos的cap模式可以切换可选cp和ap

image-20210219193258233.png
image-20210219193436394.png

4、演示配置中心3377

<dependencies>
    <!-- nacos config-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    <!-- SpringCloud ailibaba nacos-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency><!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
        <groupId>com.dkf.cloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <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>
    <!--热部署-->
    <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>

application.yml

spring:
  profiles:
    active: dev #表示开发环境

bootstrap.yml

server:
  port: 3377

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #指定yaml格式配置
@RestController
@RefreshScope
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/config/info")
    public String getConfigInfo(){
        return configInfo;
    }
}
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClient {

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

重点:服务名称匹配规则,在nacos中填写对应的服务配置文件的时候命名有一定的规则

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传{spring.profile.active}.${file-extension}

image-20210219201551079.png

5、nacos中的分组

image-20210219201741991.png

按三种方式进行区分配置文件的区分:

active

active: dev

namespace

namespace: 命名空间UUID

group

group: 组名

分别对应下图三个地方

image-20210219225611343.png

Nacos集群和持久化配置(重要)

1、windows下切换数据库

1、官网:https://nacos.io/zh-cn/docs/deployment.html

2、找到nacos-mysql.sql数据库文件生成对应的数据库

3、修改application.properties文件

spring.datasource.platform=mysql

db.num=1
db.url.0=jdbc:mysql://11.162.196.16:3306/nacos_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=nacos_devtest
db.password=youdontknow

重启,完成

2、linux安装nacos

image-20210219233946766.png

普通的安装流程:下载->安装->启动

3、配置集群

1、配置好mysql

create database nacos default character set utf8;
use nacos;
source /opt/nacos/conf/nacos-mysql.sql

2、配置properties

spring.datasource.platform=mysql

db.num=1
db.url.0=jdbc:mysql://11.162.196.16:3306/nacos_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=nacos_devtest
db.password=youdontknow

image-20210220093204055.png

3、配置集群文件conf/cluster.conf

image-20210220093508166.png

4、修改启动脚本startup.sh
image-20210220093734117.png

5、分别启动
image-20210220094009589.png

6、配置nginx

安装好nginx后配置他的配置中心

对conf/nginx.conf进行配置

image-20210220101846740.png

启动nginx后访问http:你的ip:1111/nacos会自动进入到nacos页面

Sentinel熔断与限流

1、sentinel

官网:https://github.com/alibaba/Sentinel

中文文档:https://github.com/alibaba/Sentinel/wiki/介绍

sentinel就是之前学习的hystrix的阿里版,但是阿里做了界面以及优化。可以控制流量,服务降级,熔断等操作。

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

image-20210220122128132.png

2、安装sentinel

Sentinel分为两个部分:

  • 核心库(Java客户端)不依赖任何框架/库,能够运行于所有Java运行时环境,同时对Dubbo /
    • Spring Cloud等框架也有较好的支持。
  • 控制台(Dashboard) 基于Spring Boot开发,打包后可以直接运行,不需要额外的Tomcat等应用容器。

运行命令:

  • 前提:
    • java8环境
    • 8080端口不被占用
  • 命令:java -jar sentinel-dashboard-1.7.0.jar

访问前台界面:

用户名和密码默认为sentinel

image-20210220122706553.png

3、初始化演示工程

1、启动nacos8848

2、新建一个module

    <dependencies>
        <!-- SpringCloud ailibaba nacos-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <!-- SpringCloud ailibaba sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <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>
        <!--热部署-->
        <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>
server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinal-service
  cloud:
    nacos:
      discovery:
        #Nacos服务注册中心地址
        server-addr: localhost:8848
    sentinel:
      transport:
        #配置Sentin dashboard地址
        dashboard: localhost:8080
        # 默认8719端口,假如被占用了会自动从8719端口+1进行扫描,直到找到未被占用的 端口
        port: 8719

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

3、启动sentinel8080

4、启动微服务8401

5、启动8401微服务后查看sentienl控制台

说明sentinel使用的是懒加载,所以必须先要访问才可以看到配置,不然显示页面就是空的。

线程数阈值:与QPS相比,线程数表示后台只有一个线程处理数据,如果前来请求的线程数量过多那么就会报错。

QPS表示所有请求都来了,但是我只让阈值数量的请求进去。剩下的全都不让进。

4、流程控制

1、基本介绍

网址:https://github.com/alibaba/Sentinel/wiki/流量控制#基于qps并发数的流量控制

2、流控模式

3、流控效果

预热warm up

为了防止访问量的突然上升。所以我们慢慢的把访问量涨上去。

warm up 预热/冷启动方式,慢慢的把流量加上去。默认冷加载因子为3.

image-20210220155658014.png

排队等待

image-20210220155255411.png

5、降级规则

image-20210220174842406.png

Sentinel熔断降级会在调用链路中某个资源出现不稳定状态时(例如调用超时或异常比例升高),对这个资源的调用进行限制,让请求快速失败,避免影响到其它的资源而导致级联错误。

当资源被降级后,在接下来的降级时间窗之内,对该资源的调用都自动熔断(默认行为是抛出DegradeException)。

(1)、降级策略

  • RT

我们通常用以下几种方式来衡量资源是否处于稳定的状态:

平均响应时间( DEGRADE_ GRADE_ RT ):当1s内持续进入5个请求,对应时刻的平均响应时间(秒级)均超过阈值( count,以ms为单位),那么在接下的时间窗口( DegradeRule中的timewindow, 以s为单位)之内,对这个方法的调用都会自动地熔断(拋出DegradeException)。注意Sentinel默认统计的RT. 上限是4900ms,超出此阈值的都会算作4900 ms,若需要变更此上限可以通过启动配置项Dcsp. sentinel. statistic. max. rt=XXx来配置。

  • 异常比例

常总数占通过是的比值超过聞值Iouen当资源的每秒清夫量>=5.并且每秒异.E( DegradeRule中的个0万法的间用都会自动地区目。异物地调品范地cont)之后,资源进入降级状timewindow。以s为单位)之内,对这[0.0, 1.0]. 代表0%-

image-20210220175211362.png

  • 异常数

异常数( DEGRADE GRADE_ EXCEPTION COUNT ):当资源近1分钟的异常数目超过阈值之后会进行熔断。注意由于统计时间窗C ]是分钟级别的,若timeWindow 小于60s,则结束熔断状态后吗可能再进入熔断状态。

异常数是安装分钟进行统计的

6、热点key限流

热点规则:针对某一个属性设置限制。当这个属性存在的时候就判断这个属性的访问限制是否符合条件。符合同行。不符合就降级(配置了降级的函数)或者报错。

参数例外项:可以设置一个特殊的值来更改这个值的对应的参数的规则和其他的值不一样。

image-20210220201123090.png

1、后台配置

    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false)String p1,
                             @RequestParam(value = "p2",required = false)String p2) {
        return "----testHotKey";
    }

    public String deal_testHotKey(String p1, String p2, BlockException exception) {
        return "----deal_testHotKey, o(╥﹏╥)o"; // sentinel的默认提示都是: Blocked by Sentinel (flow limiting)
    }

blockHandler只处理请求,不处理异常。只对前端配置了对应的热点规则的请求尽心控制,内部代码报错就只能使用其他的方法处理。

7、系统规则

Sentinel 系统自适应限流从整体维度对应用入口流量进行控制,结合应用的 Load、CPU 使用率、总体平均 RT、入口 QPS 和并发线程数等几个维度的监控指标,通过自适应的流控策略,让系统的入口流量和系统的负载达到一个平衡,让系统尽可能跑在最大吞吐量的同时保证系统整体的稳定性。

image-20210221095746002.png
image-20210221095827135.png
image-20210221100201674.png

1、按照名称限流+后续处理

启动nacos->启动Sentinel

更新9401modul

1、pom文件添加commone的依赖

<dependency><!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
    <groupId>com.eiletxie.springcloud</groupId>
    <artifactId>cloud-api-commons</artifactId>
    <version>${project.version}</version>
</dependency>

2、业务类添加对应代码

@RestController
public class RateLimitController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommonResult byResource() {
        return  new CommonResult(200,"按照资源名称限流测试",new Payment(2020L,"serial001"));
    }

    public CommonResult handleException(BlockException exception) {
        return  new CommonResult(444,exception.getClass().getCanonicalName() + "\t 服务不可用");
    }
}

3、配置好对应的流量控制规则

image-20210221100523298.png

访问的时候就会受到次数的限制。

4、其他问题,当我们在关闭后台服务的时候配置的对应规则就会消失。规则是临时的并没有持久化

2、按照Url地址限流+后续处理

1、之前的配置都配置好的情况下,添加controller操作

@RestController
public class RealController {

    @GetMapping("/byResource")
    @SentinelResource(value = "byResource")
    public String ByResource(){
        return "这个好诶";
    }

}

2、配置对应的路径规则
image-20210221101219521.png

3、兜底方案面临的问题

1系统默认的, 没有体现我们自己的业务要求。

2依照现有条件,我们自定义的处理方法又和业务代码耦合在一块,不直观。

3每个业务方法都添加一个兜底的,那代码膨胀加剧。

4全局统-的处理方法没有体现。

4、客户自定义限流处理逻辑

1、创建一个CustomerHandler类来单独处理兜底方案

image-20210221101716881.png

2、在controller层中添加对应指向性注解

image-20210221101820717.png

8、服务熔断功能

1、新建9003和9004服务进行负载均衡

新建cloudalibaba-provider-payment9003/9004

pom文件:

<dependencies>
    <!-- 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>
    <dependency><!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
        <groupId>com.dkf.cloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <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>
    <!--热部署-->
    <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>

启动类:

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9003 {

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

yml文件:

server:
  port: 9003

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

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

业务类:

@RestController
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    public static HashMap<Long, Payment> map = new HashMap<>();
    static {
        map.put(1L,new Payment(1L,"1111"));
        map.put(1L,new Payment(2L,"2222"));
        map.put(1L,new Payment(3L,"3333"));
    }

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

2、新建84客户端

新建cloudalibaba-consumer-nacos-order84

pom文件

<dependencies>
    <!-- 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>
    <dependency><!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
        <groupId>com.dkf.cloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <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>
    <!--热部署-->
    <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: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8719

#消费者将去访问的微服务名称
server-url:
  nacos-user-service: http://nacos-payment-provider

启动类文件:

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

业务类:

@RestController
public class CircleBreakerController {

    public static  final  String SERVICE_URL = "http://nacos-payment-provider";

    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/consumer/fallback/{id}")
    @SentinelResource(value = "fallback")
    public CommonResult<Payment> fallback(@PathVariable Long id) {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id,CommonResult.class,id);

        if(id == 4){
            throw new IllegalArgumentException("IllegalArgument ,非法参数异常G:\DesktopBackup\note\springcloud.");
        }else if(result.getData() == null) {
            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
        }

        return  result;
    }

}

配置类:

@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestemplate(){
        return new RestTemplate();
    }
}

3、配置服务降级以及异常处理

在使用@SentinelResource的时候我们知道了他有一个blockHandler属性可以用来配置前端配置的规则的服务降级,但是不会对服务内部的异常进行降级处理。fallback属性可以对服务内部的异常进行服务降级处理。

1、如果只使用fallback属性

    @RequestMapping("/consumer/fallback/{id}")
//    @SentinelResource(value = "fallback")
    @SentinelResource(value = "fallback",fallback = "handlerFallback")
    public CommonResult<Payment> fallback(@PathVariable Long id) {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id,CommonResult.class,id);

        if(id == 4){
            throw new IllegalArgumentException("IllegalArgument ,非法参数异常G:\DesktopBackup\note\springcloud.");
        }else if(result.getData() == null) {
            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
        }

        return  result;
    }

    public CommonResult handlerFallback(@PathVariable Long id,Throwable e) {
        Payment payment = new Payment(id,"null");
        return new CommonResult(444,"异常handlerFallback,exception内容: " + e.getMessage(), payment);
    }

只要服务不出错就没有访问限制。

2、只使用blockHandler属性,服务内部出错无法进行降级直接显示报错页面。界面不友好

image-20210221140801513.png

3、同时使用

    @RequestMapping("/consumer/fallback/{id}")
//    @SentinelResource(value = "fallback")
    @SentinelResource(value = "fallback",fallback = "handlerFallback",blockHandler = "blockHandler")
    public CommonResult<Payment> fallback(@PathVariable Long id) {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id,CommonResult.class,id);

        if(id == 4){
            throw new IllegalArgumentException("IllegalArgument ,非法参数异常G:\DesktopBackup\note\springcloud.");
        }else if(result.getData() == null) {
            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
        }

        return  result;
    }

    public CommonResult handlerFallback(@PathVariable Long id,Throwable e) {
        Payment payment = new Payment(id,"null");
        return new CommonResult(444,"异常handlerFallback,exception内容: " + e.getMessage(), payment);
    }

    public CommonResult blockHandler(@PathVariable Long id, BlockException e) {
        Payment payment = new Payment(id,"null");
        return new CommonResult(444,"blockHandler-sentinel 限流,BlockException: " + e.getMessage(), payment);
    }

如果访问次数超过了配置的规则次数。那么会优先使用blockHandler的降级方法。

4、忽略指定的异常

在sentinelResource中有一个可以忽略某个异常的属性。当内部出现这个异常的时候只要这个属性添加了异常。我们就让服务运行下去而不是降级。

@SentinelResource(value = "fallback",fallback = "handlerFallback",blockHandler = "blockHandler",exceptionsToIgnore = {IllegalArgumentException.class})

4、使用Feign进行服务熔断

1、添加对应的feign依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2、添加yml

server:
  port: 84

spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8719

#消费者将去访问的微服务名称
server-url:
  nacos-user-service: http://nacos-payment-provider
#激活feigen对sentinel的支持
feign:
  sentinel:
    enabled: true

3、配置启动类

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

4、添加接口调用

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

    @GetMapping(value = "/paymentSQL/{id}")
    public CommonResult<Payment> paymentSQL(@PathVariable("id")Long id);

}
@Component
public class PaymentFallbackService implements PaymentService{
    @Override
    public CommonResult<Payment> paymentSQL(Long id) {
        return new CommonResult<>(444,"服务降级返回,来自于PaymentFallbackService",new Payment(id,"errorSerial"));
    }
}

如果使用远程调用的时候出现了问题使用feignclient有一个非常不错的方法

image-20210221143440093.png

9、sentine规则持久化

为什么需要使用持久化,我们对业务创建规则的时候规则只是临时的。当服务重启的时候需要重新编写。我们希望可以把服务的规则保存在本地这样就不需要在每次重启的时候编写对应的规则。

我们将sentinel的规则持久化保存到nacos中

<!--这里是配置sentinel持久化的依赖-->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

yml

spring:
  application:
    name: cloudalibaba-sentinal-service
  cloud:
    nacos:
      discovery:
        #Nacos服务注册中心地址
        server-addr: localhost:8848
    sentinel:
      transport:
        #配置Sentin dashboard地址
        dashboard: localhost:8080
        # 默认8719端口,假如被占用了会自动从8719端口+1进行扫描,直到找到未被占用的 端口
        port: 8719
      datasource:
        ds1:
          nacos:
            server-addr: localhost:8848
            dataId: ${spring.application.name}
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow

在nacos中添加yml

image-20210221144731449.png

[
    {
        "resource": "/byResource",
        "limitApp": "default",
        "grade": 1,
        "count": 1,
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode": false
    }
]

image-20210221144810558.png

配置完成后我们发现对应服务规则保存起来了。不过因为是懒加载,所以再起启动的时候需要访问才可以看到。

Seata分布式事务

1、分布式问题的由来

分布式的服务从1:1,1:n,n:n越来越多。所以如果多个项目访问多个数据源的时候就有可能造成数据的不统一,为了解决这个问题seata就出现了
image-20210221170545408.png

2、Seata简介

1、官网:http://seata.io/zh-cn/

seata是一款开源的分布式解决方案,致力于在微服务架构下提供高性能和简单易用的分布事物服务。

Seata术语

TC -事务协调者

维护全局和分支事务的状态,驱动全局事务提交或回滚。

TM -事务管理器

定义全局事务的范围:开始全局事务、提交或回滚全局事务。

RM -资源管理器

管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。

2、一个典型的分布式事务过程:

  • 分布式事务处理过程的ID+三组件模型
    • Transcation ID全局唯一的事务ID
    • 三组件的概念
      • TC - 事务协调者:维护全局和分支事务的状态,驱动全局事务提交或回滚。
      • TM - 事务管理器:定义全局事务的范围:开始全局事务、提交或回滚全局事务。
      • RM - 资源管理器:管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。
  • 处理过程
    • TM向TC申请开启一 个全局事务,全局事务创建成功并生成一 个全局唯一 的XID;
    • XID在微服务调用链路的上下文中传播;
    • RM向TC注册分支事务,将其纳入XID对应全局事务的管辖;
    • TM向TC发起针对XID的全局提交或回滚决议;
    • TC调度XID下管辖的全部分支事务完成提交或回滚请求。

image-20210221172032866.png

3、下载:https://github.com/seata/seata/releases

4、使用:

本地@Transational

全局@GlobalTransactional

image-20210221172444625.png

3、Seata-Server安装

1、下载地址

https://seata.io/zh-cn/blog/download.html 优先选用和视频中老师一个版本为0.9版本

2、修改配置文件

主要修改:自定义事务组名称+事务日志存储模式为db +数据库连接信息

1、修改/conf目录下的file.conf文件

image-20210221181458586.png
image-20210221181642425.png

2、建表的sql在/conf/db_store.sql

image-20210221181841198.png

3、修改/conf/registry.conf文件

image-20210221182040159.png
4、启动seata服务

4、订单/库存/账户业务数据库准备

1、创建业务数据库

create database seata_order;
create database setat_storage;
create database setat_account;

2、建表
image-20210222100206388.png

t_order表;

CREATE TABLE t_order(
    `id` BIGINT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `user_id` BIGINT(11) DEFAULT NULL COMMENT '用户id',
    `product_id` BIGINT(11) DEFAULT NULL COMMENT '产品id',
    `count` BIGINT(11) DEFAULT NULL COMMENT '数量',
    `money` BIGINT(11) DEFAULT NULL COMMENT '金额',
    `status` BIGINT(11) DEFAULT NULL COMMENT '订单状态: 0创建中 1已完结'
) ENGINE = INNODB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

select * from t_order

t_storage表:

CREATE TABLE t_storage(
    `id` BIGINT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `product_id` BIGINT(11) DEFAULT NULL COMMENT '产品id',
    `total` BIGINT(11) DEFAULT NULL COMMENT '总库存',
    `used` BIGINT(11) DEFAULT NULL COMMENT '已用库存',
    `residue` BIGINT(11) DEFAULT NULL COMMENT '剩余库存'
) ENGINE = INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
 
INSERT INTO t_storage( `id`,`product_id`, `total`,`used`,`residue`) values(1,1,100,0,100);
SELECT * FROM t_storage;

t_account表:

CREATE TABLE t_account(
    `id` BIGINT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `user_id` BIGINT(11) DEFAULT NULL COMMENT '用户id',
    `total` BIGINT(11) DEFAULT NULL COMMENT '总额度',
    `used` BIGINT(11) DEFAULT NULL COMMENT '已用额度',
    `residue` BIGINT(11) DEFAULT NULL COMMENT '剩余额度'
) ENGINE = INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
 
INSERT INTO t_account( `id`,`user_id`, `total`,`used`,`residue`) values(1,1,1000,0,1000);
SELECT * FROM t_account;

3、分别创建回滚日志

去seata/conf/db_undo_liog.sql回滚的日志表,可以进行日志回滚的配置,分别去三个数据库运行下面的代码

-- the table to store seata xid data
-- 0.7.0+ add context
-- you must to init this sql for you business databese. the seata server not need it.
-- 此脚本必须初始化在你当前的业务数据库中,用于AT 模式XID记录。与server端无关(注:业务数据库)
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
drop table `undo_log`;
CREATE TABLE `undo_log` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(100) NOT NULL,
  `context` varchar(128) NOT NULL,
  `rollback_info` longblob NOT NULL,
  `log_status` int(11) NOT NULL,
  `log_created` datetime NOT NULL,
  `log_modified` datetime NOT NULL,
  `ext` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

5、订单/库存/账户业务微服务准备

1、客户端模块的编写

1、创建seata-order-service12001服务的框架

pom文件

<dependencies>
    <!-- nacos -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- seata-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
        <exclusions>
            <exclusion>
                <groupId>io.seata</groupId>
                <artifactId>seata-all</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>io.seata</groupId>
        <artifactId>seata-all</artifactId>
        <version>0.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <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>
    <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>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
    </dependency>
    <!--jdbc-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.13</version>
    </dependency>
    <dependency>
        <groupId>com.dkf.cloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    <!--hutool 测试雪花算法-->
    <!--        <dependency>-->
    <!--            <groupId>cn.hutool</groupId>-->
    <!--            <artifactId>hutool-captcha</artifactId>-->
    <!--            <version>5.2.0</version>-->
    <!--        </dependency>-->
</dependencies>

添加yml文件

server:
  port: 12001
spring:
  application:
    name: seata-order-service
  cloud:
    alibaba:
      seata:
        #自定义事务组名称与seata-server中对应
        tx-service-group: fsp_tx_group
    nacos:
      discovery:
        server-addr: localhost:8848
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/seata_order?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123456
feign:
  hystrix:
    enabled: false
logging:
  level:
    io:
      seata: info

mybatis:
  mapper-locations: classpath:mapper/*.xml

添加file.conf文件

transport {
  # tcp udt unix-domain-socket
  type = "TCP"
  #NIO NATIVE
  server = "NIO"
  #enable heartbeat
  heartbeat = true
  #thread factory for netty
  thread-factory {
    boss-thread-prefix = "NettyBoss"
    worker-thread-prefix = "NettyServerNIOWorker"
    server-executor-thread-prefix = "NettyServerBizHandler"
    share-boss-worker = false
    client-selector-thread-prefix = "NettyClientSelector"
    client-selector-thread-size = 1
    client-worker-thread-prefix = "NettyClientWorkerThread"
    # netty boss thread size,will not be used for UDT
    boss-thread-size = 1
    #auto default pin or 8
    worker-thread-size = 8
  }
  shutdown {
    # when destroy server, wait seconds
    wait = 3
  }
  serialization = "seata"
  compressor = "none"
}
service {
  #vgroup->rgroup
  vgroup_mapping.fsp_tx_group = "default"
  #only support single node
  default.grouplist = "127.0.0.1:8091"
  #degrade current not support
  enableDegrade = false
  #disable
  disable = false
  #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
  max.commit.retry.timeout = "-1"
  max.rollback.retry.timeout = "-1"
}

client {
  async.commit.buffer.limit = 10000
  lock {
    retry.internal = 10
    retry.times = 30
  }
  report.retry.count = 5
  tm.commit.retry.count = 1
  tm.rollback.retry.count = 1
}

## transaction log store
store {
  ## store mode: file、db
  mode = "db"

  ## file store
  file {
    dir = "sessionStore"

    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    max-branch-session-size = 16384
    # globe session size , if exceeded throws exceptions
    max-global-session-size = 512
    # file buffer size , if exceeded allocate new buffer
    file-write-buffer-cache-size = 16384
    # when recover batch read size
    session.reload.read_size = 100
    # async, sync
    flush-disk-mode = async
  }

  ## database store
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
    datasource = "dbcp"
    ## mysql/oracle/h2/oceanbase etc.
    db-type = "mysql"
    driver-class-name = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://127.0.0.1:3306/seata"
    user = "root"
    password = "123456"
    min-conn = 1
    max-conn = 3
    global.table = "global_table"
    branch.table = "branch_table"
    lock-table = "lock_table"
    query-limit = 100
  }
}
lock {
  ## the lock store mode: local、remote
  mode = "remote"

  local {
    ## store locks in user's database
  }

  remote {
    ## store locks in the seata's server
  }
}
recovery {
  #schedule committing retry period in milliseconds
  committing-retry-period = 1000
  #schedule asyn committing retry period in milliseconds
  asyn-committing-retry-period = 1000
  #schedule rollbacking retry period in milliseconds
  rollbacking-retry-period = 1000
  #schedule timeout retry period in milliseconds
  timeout-retry-period = 1000
}

transaction {
  undo.data.validation = true
  undo.log.serialization = "jackson"
  undo.log.save.days = 7
  #schedule delete expired undo_log in milliseconds
  undo.log.delete.period = 86400000
  undo.log.table = "undo_log"
}

## metrics settings
metrics {
  enabled = false
  registry-type = "compact"
  # multi exporters use comma divided
  exporter-list = "prometheus"
  exporter-prometheus-port = 9898
}

support {
  ## spring
  spring {
    # auto proxy the DataSource bean
    datasource.autoproxy = false
  }
}

添加registry.conf

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    serverAddr = "localhost:8848"
    namespace = ""
    cluster = "default"
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = "0"
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
  }
  etcd3 {
    cluster = "default"
    serverAddr = "http://localhost:2379"
  }
  sofa {
    serverAddr = "127.0.0.1:9603"
    application = "default"
    region = "DEFAULT_ZONE"
    datacenter = "DefaultDataCenter"
    cluster = "default"
    group = "SEATA_GROUP"
    addressWaitTime = "3000"
  }
  file {
    name = "file.conf"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "file"

  nacos {
    serverAddr = "localhost"
    namespace = ""
  }
  consul {
    serverAddr = "127.0.0.1:8500"
  }
  apollo {
    app.id = "seata-server"
    apollo.meta = "http://192.168.1.204:8801"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}

添加启动类

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableFeignClients
@EnableDiscoveryClient
public class SeataOrderMainApp12001 {

    public static void main(String[] args) {
        SpringApplication.run(SeataOrderMainApp12001.class,args);
    }
}
2、编写对应的逻辑代码

两个配置类

MyBatisConfig

@Configuration
@MapperScan({"com.atguigu.springcloud.alibaba.dao"})
public class MyBatisConfig {
}

DataSourceProxyConfig配置类

@Configuration
public class DataSourceProxyConfig {

    @Value("${mybatis.mapper-locations}")
    private String mapperLocations;

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }

    @Bean
    public DataSourceProxy dataSourceProxy(DataSource druidDataSource) {
        return new DataSourceProxy(druidDataSource);
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSourceProxy);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setMapperLocations(resolver.getResources(mapperLocations));
        return bean.getObject();
    }
}

service层

@FeignClient("seata-account-service")
public interface AccountService {
    @PostMapping("/account/decrease")
    CommonResult decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money);

}
@Service
public interface OrderService {
    //1、新建订单
    void create(Order order);

}
@FeignClient("seata-storage-service")
public interface StorageService {

    @PostMapping("/storage/decrease")
    CommonResult decrease(@RequestParam("productId") Long productId,@RequestParam("count") Integer count);
}
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;

    @Resource
    private StorageService storageService;

    @Resource
    private AccountService accountService;

    /**
     * 下订单-》减库存-》减余额-》该状态
     *
     * @param order
     */
    @Override
    public void create(Order order) {
        log.info("----->开始新建订单");
        orderDao.create(order);
        log.info("------->订单微服务开始调用库存,做扣减count");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("------->订单微服务开始调用库存,做扣减end");

        log.info("--------->订单微服务开始调用账户,做扣减money");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("--------->订单微服务开始调用账户,做扣减end");


        log.info("--------->订单微服务开始修改订单,修改status");
        orderDao.update(order.getUserId(), 0);
        log.info("--------->修改完成");

        log.info("----->完成新建订单");

    }
}

DOMAIN层

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {

    private Long id;
    private Long userId;
    private Long productId;
    private Integer count;
    private BigDecimal money;
    private Integer status;
}

dao层


@Mapper
public interface OrderDao {

    //1、新建订单
    void create(Order order);

    //2、修改订单状态,从零改为1
    void update(@Param("userId") Long userId, @Param("status") Integer status);
}

controlelr层

@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    @GetMapping("/order/create")
    public CommonResult create(Order order){
        orderService.create(order);
        return new CommonResult(200,"订单创建成功");
    }
}

mapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.atguigu.springcloud.alibaba.dao.OrderDao">
    <resultMap id="BaseResultMap" type="com.atguigu.springcloud.alibaba.domain.Order">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="user_id" property="userId" jdbcType="BIGINT"/>
        <result column="product_id" property="productId" jdbcType="BIGINT"/>
        <result column="count" property="count" jdbcType="INTEGER"/>
        <result column="money" property="money" jdbcType="DECIMAL" />
        <result column="status" property="status" jdbcType="INTEGER"/>
    </resultMap>
    <insert id="create">
        insert into t_order(id,user_id,product_id,count,money,status) values(null,#{userId},#{productId},#{count},#{money},0);
    </insert>
    <update id="update">
        update t_order set status =1 where user_id = #{userId} and status = #{status}
    </update>
</mapper>

自己做出另外另个服务,就不在粘贴更多的代码了

6、Test

如果我们没有添加全局事务回滚标签@GlobalTransactional

那么当我们运行业务的时候就有可能有些业务完成有一些业务无法完成。就会出现有的数据库更改了有的数据库没有更改,进而造成数据不一致的现象。

由于feign的重复机制,就有可能出现账户余额多扣多次的现象。

添加@GlobalTranscational注解

@GlobalTransactional(name="fsp-create-order",rollbackFor = Exception.class)

7、补充

seata0.9版本是无法进行集群的。1.0之后才可以。工作的时候使用1.0版本之后的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值