# weibo_demo **Repository Path**: lgc653/weibo_demo ## Basic Information - **Project Name**: weibo_demo - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-08-14 - **Last Updated**: 2024-10-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # weibo_demo ## DEMO配置讲解 ### 安装seata 这里采用最简单的无注册中心,file存储,该模式下,不需要注册中心,也不需要任何第三方存储中心。 `docker-compose.yaml` ```yaml version: "3.1" services: seata-server: image: seataio/seata-server:latest hostname: seata-server ports: - "7091:7091" - "8091:8091" environment: - SEATA_PORT=8091 - STORE_MODE=file volumes: - "/usr/share/zoneinfo/Asia/Shanghai:/etc/localtime" - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone" ``` 启动seata ```sh cd /root/weibo_demo docker-compose up [+] Running 1/0 ✔ Container weibo_demo-seata-server-1 Created 0.0s Attaching to seata-server-1 ``` ### 修改配置 `src\main\resources\file.conf`,主要是设置default.grouplis为刚才的seata的地址 ```groovy service { vgroupMapping.my_test_tx_group = "default" default.grouplist = "172.25.187.132:8091" } ``` 数据库中追加表 ```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 = utf8mb4 COMMENT ='AT transaction mode undo table'; ALTER TABLE `undo_log` ADD INDEX `ix_log_created` (`log_created`); ``` ### 运行测试代码 把项目跑起来,通过curl测试,例如在power shell下面 ```sh $uri = "http://localhost:8080/weibo/publish" $content = '这是我发布的微博内容!' $response = Invoke-RestMethod -Uri $uri -Method Post -Body $content -ContentType "application/json" $response ``` 得到结果 ```sh 微博发布成功! ``` 观察seata-server、db、redis,检验事务是否成功 ## docker-compose.yaml参考 ```yaml version: "3.1" services: mysql: image: mysql:5.7 restart: always environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: seata ports: - "3306:3306" volumes: - ./mysql-data:/var/lib/mysql redis: image: redis:6.2-alpine restart: always ports: - "6379:6379" seata-server: image: seataio/seata-server:latest ports: - "7091:7091" - "8091:8091" environment: - SEATA_PORT=8091 - STORE_MODE=file volumes: - "/usr/share/zoneinfo/Asia/Shanghai:/etc/localtime" - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone" my-spring-boot-app: build: ./my-spring-boot-app # Replace with your Spring Boot app directory depends_on: - mysql - redis - seata-server ports: - "8080:8080" ```