栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springcloud alibaba全家桶之nacos,sentinel,seata集成

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springcloud alibaba全家桶之nacos,sentinel,seata集成

nacos 版本说明:点击查看

这里选择nacos1.4.3

下载地址

https://github.com/alibaba/nacos/releases/download/1.4.3/nacos-server-1.4.3.ziphttps://github.com/alibaba/nacos/releases/download/1.4.3/nacos-server-1.4.3.zip

配置

conf/application.properties中取消注释,并配置数据库信息

# db mysql
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://localhost:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=root
导入数据表

conf/nacos-mysql.sql

启动

startup.cmd -m standalone /sh startup.sh -m standalone (单机模式) 

配置命名空间

1. 登录 http://*****:8848/nacos ,账号密码默认 nacos
2. 在命名空间增加记录,全部都写 dev【由于我的项目中配置的都是dev.所以需要配置下命名空间,如果使用默认的无需配置】

至此naocs就已配置完成

项目集成

增加配置项:

spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        namespace: dev
        username: nacos
        password: nacos
        group: dev

启动项目后会在注册中心中可以看到服务已经注册成功

 至此已经完成了注册中心的集成。

 sentinel 版本

此处对应的cloud的sentinel的版本为1.8.0

下载地址
https://github.com/alibaba/Sentinel/releases/download/v1.8.0/sentinel-dashboard-1.8.0.jar
启动
java -jar Sentinel的jar包 
账户
默认账户密码 sentinel,sentinel 
项目集成

这里只把sentinel集成到了网关服务,一般情况下也是先集成到网关服务作为限流使用

		
			com.alibaba.cloud
			spring-cloud-starter-alibaba-sentinel
			
			
				
					com.fasterxml.jackson.dataformat
					jackson-dataformat-xml
				
			
		
		
		
			com.alibaba.csp
			sentinel-datasource-nacos
		
		
		
			com.alibaba.cloud
			spring-cloud-alibaba-sentinel-gateway
		

至此 sentinel已经完成,访问接口后会在sentinel中看到访问的情况

seata 版本

1.3.0

下载地址

https://github.com/seata/seata/releases/download/v1.3.0/seata-server-1.3.0.zip

配置

配置文件调整:conf/registry.conf

1.使用nacos作为配置中心和注册中心,调整registry为nacos注册中心以及相应的配置信息.调整config为nacos作为配置中心.
2.事务分组问题:项目配置seata的时候大部分的问题就是事务分组的问题。seata默认的事务分组名称为my_test_tx_group,但是实际项目中很多情况是需要调整默认事务分组的名称,根据issues:https://github.com/seata/seata-samples/issues/411中提到的。如果调整了事务分组是需要去nacos的配置中心中增加一个配置记录的。以下服务的配置使用my_serivce_default_ts_group作为分组名称进行配置
data-id:service.vgroupMapping.my_serivce_default_ts_group
说明:my_serivce_default_ts_group就是我自己的事务分组名称
配置内容:default
我的分组:dev
在配置中心中加入事务分组信息【分组注册失败请查看issues:https://github.com/seata/seata-samples/issues/411】
效果如下
存储介质 

使用mysql作为存储介质

下载地址:

https://github.com/seata/seata/blob/develop/script/server/db/mysql.sql

sql:

-- the table to store GlobalSession data
CREATE TABLE IF NOT EXISTS `global_table`
(
    `xid`                       VARCHAr(128) NOT NULL,
    `transaction_id`            BIGINT,
    `status`                    TINYINT      NOT NULL,
    `application_id`            VARCHAr(32),
    `transaction_service_group` VARCHAr(32),
    `transaction_name`          VARCHAr(128),
    `timeout`                   INT,
    `begin_time`                BIGINT,
    `application_data`          VARCHAr(2000),
    `gmt_create`                DATETIME,
    `gmt_modified`              DATETIME,
    PRIMARY KEY (`xid`),
    KEY `idx_status_gmt_modified` (`status` , `gmt_modified`),
    KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
    `branch_id`         BIGINT       NOT NULL,
    `xid`               VARCHAr(128) NOT NULL,
    `transaction_id`    BIGINT,
    `resource_group_id` VARCHAr(32),
    `resource_id`       VARCHAr(256),
    `branch_type`       VARCHAr(8),
    `status`            TINYINT,
    `client_id`         VARCHAr(64),
    `application_data`  VARCHAr(2000),
    `gmt_create`        DATETIME(6),
    `gmt_modified`      DATETIME(6),
    PRIMARY KEY (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
    `row_key`        VARCHAr(128) NOT NULL,
    `xid`            VARCHAr(128),
    `transaction_id` BIGINT,
    `branch_id`      BIGINT       NOT NULL,
    `resource_id`    VARCHAr(256),
    `table_name`     VARCHAr(32),
    `pk`             VARCHAr(36),
    `status`         TINYINT      NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_status` (`status`),
    KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

CREATE TABLE IF NOT EXISTS `distributed_lock`
(
    `lock_key`       CHAr(20) NOT NULL,
    `lock_value`     VARCHAr(20) NOT NULL,
    `expire`         BIGINT,
    primary key (`lock_key`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('HandleAllSession', ' ', 0);

在业务的相应数据库中增加undo_log表

下载地址:

https://github.com/seata/seata/blob/develop/script/client/at/db/mysql.sql

sql:

CREATE TABLE IF NOT EXISTS `undo_log`
(
    `branch_id`     BIGINT       NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAr(128) NOT NULL COMMENT 'global transaction id',
    `context`       VARCHAr(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    `rollback_info` LonGBLOB     NOT NULL COMMENT 'rollback info',
    `log_status`    INT(11)      NOT NULL COMMENT '0:normal status,1:defense status',
    `log_created`   DATETIME(6)  NOT NULL COMMENT 'create datetime',
    `log_modified`  DATETIME(6)  NOT NULL COMMENT 'modify datetime',
    UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';
启动
bin/相应平台的启动脚本
启动成功后会在nacos注册中心中可以看到一个seata-server

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/750331.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号