Skip to content

Commit

Permalink
add springcloud-jpa-seata sample (#87)
Browse files Browse the repository at this point in the history
* add springcloud-jpa-seata sample

* add spring repo

* remove order-service dependencyManagement

* fix userId equals
  • Loading branch information
fangls authored and slievrly committed Apr 11, 2019
1 parent b151e0f commit 351c7db
Show file tree
Hide file tree
Showing 42 changed files with 1,496 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -18,3 +18,4 @@ Samples for Fescar. This project contains several sub-projects, each of which is
* [nacos](https://github.com/fescar-group/fescar-samples/tree/master/nacos) - Integration example of [Fescar](https://github.com/alibaba/fescar)[Apache Dubbo](https://github.com/apache/incubator-dubbo) and [Alibaba Nacos](https://github.com/alibaba/nacos/)
* [springboot-dubbo-fescar](https://github.com/fescar-group/fescar-samples/tree/master/nutzboot-dubbo-fescar) - Integration example of [Fescar](https://github.com/alibaba/fescar)[Apache Dubbo](https://github.com/apache/incubator-dubbo) and [Spring Boot](https://github.com/spring-projects/spring-boot/)
* [nutzboot-dubbo-fescar](https://github.com/fescar-group/fescar-samples/tree/master/nutzboot-dubbo-fescar) - Integration example of [Fescar](https://github.com/alibaba/fescar)[Apache Dubbo](https://github.com/apache/incubator-dubbo) and [NutzBoot](https://github.com/nutzam/nutzboot/)
* [springcloud-jpa-seata](https://github.com/fescar-group/fescar-samples/tree/master/springcloud-jpa-seata) - Integration example of [Seata](https://github.com/Seata) and [Spring Cloud](https://github.com/spring-cloud)
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -61,7 +61,7 @@
<module>nacos</module>
<module>springboot-dubbo-fescar</module>
<module>tcc</module>

<module>springcloud-jpa-seata</module>
</modules>
<dependencyManagement>
<dependencies>
Expand Down
27 changes: 27 additions & 0 deletions springcloud-jpa-seata/README.md
@@ -0,0 +1,27 @@
# seata-sample
基于spring cloud+feign+spring jpa+spring cloud alibaba fescar+mysql

### 准备工作
1. 执行sql/all_in_one.sql

2. 下载[0.4.1](https://github.com/seata/seata/releases/tag/v0.4.1)版本server

客户端与服务端版本号保持一致
3. 启动fescar server

sh fescar-server.sh 8091 ../data/
4. 启动business、storage、account、order

数据库默认连接127.0.0.1:3306,不同的注意修改

5. 事务成功 GET http://127.0.0.1:8084/purchase/commit

6. 事务回滚 GET http://127.0.0.1:8084/purchase/rollback

### 验证数据
1. 事务成功

库存减1、订单加1、余额减5
2. 事务回滚

数据无变化
50 changes: 50 additions & 0 deletions springcloud-jpa-seata/business-service/pom.xml
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.alibaba.fescar</groupId>
<artifactId>springcloud-jpa-seata</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>business-service</artifactId>
<packaging>jar</packaging>

<name>business-service</name>
<description>Demo project for Spring Boot</description>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-fescar</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.fescar</groupId>
<artifactId>fescar-spring</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
@@ -0,0 +1,15 @@
package com.alibaba.seata.sample;

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

@SpringBootApplication
@EnableFeignClients
public class BusinessApplication {

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

}
@@ -0,0 +1,39 @@
package com.alibaba.seata.sample.controller;

import com.alibaba.seata.sample.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BusinessController {

@Autowired
private BusinessService businessService;

/**
* 购买下单,模拟全局事务提交
* @return
*/
@RequestMapping("/purchase/commit")
public Boolean purchaseCommit(){
businessService.purchase("1001", "2001", 1);
return true;
}

/**
* 购买下单,模拟全局事务回滚
* @return
*/
@RequestMapping("/purchase/rollback")
public Boolean purchaseRollback(){
try {
businessService.purchase("1002", "2001", 1);
}catch (Exception e){
e.printStackTrace();
return false;
}

return true;
}
}
@@ -0,0 +1,22 @@
package com.alibaba.seata.sample.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

/**
* Description:
*
* @author fangliangsheng
* @date 2019-04-04
*/
@FeignClient(name = "order-service", url = "127.0.0.1:8082")
public interface OrderFeignClient {

@GetMapping("/create")
void create(@RequestParam("userId") String userId,
@RequestParam("commodityCode") String commodityCode,
@RequestParam("count") Integer count);

}
@@ -0,0 +1,21 @@
package com.alibaba.seata.sample.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

/**
* Description:
*
* @author fangliangsheng
* @date 2019-04-04
*/
@FeignClient(name ="storage-service", url = "127.0.0.1:8081")
public interface StorageFeignClient {

@GetMapping("/deduct")
void deduct(@RequestParam("commodityCode") String commodityCode,
@RequestParam("count") Integer count);

}
@@ -0,0 +1,35 @@
package com.alibaba.seata.sample.service;

import com.alibaba.fescar.spring.annotation.GlobalTransactional;
import com.alibaba.seata.sample.feign.OrderFeignClient;
import com.alibaba.seata.sample.feign.StorageFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* Description:
*
* @author fangliangsheng
* @date 2019-04-05
*/
@Service
public class BusinessService {

@Autowired
private StorageFeignClient storageFeignClient;
@Autowired
private OrderFeignClient orderFeignClient;

/**
* 减库存,下订单
* @param userId
* @param commodityCode
* @param orderCount
*/
@GlobalTransactional
public void purchase(String userId, String commodityCode, int orderCount){
storageFeignClient.deduct(commodityCode, orderCount);

orderFeignClient.create(userId, commodityCode, orderCount);
}
}
@@ -0,0 +1,5 @@
spring.application.name=business-service
server.port=8084

logging.level.com.alibaba.fescar=debug
spring.cloud.alibaba.fescar.tx-service-group=my_test_tx_group
@@ -0,0 +1,41 @@
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
}
}
service {
#vgroup->rgroup
vgroup_mapping.my_test_tx_group = "default"
#only support single node
default.grouplist = "127.0.0.1:8091"
#degrade current not support
enableDegrade = false
#disable
disable = false
disableGlobalTransaction = false
}

client {
async.commit.buffer.limit = 10000
lock {
retry.internal = 10
retry.times = 30
}
}
@@ -0,0 +1,51 @@
registry {
# file 、nacos 、eureka、redis、zk
type = "file"

nacos {
serverAddr = "localhost"
namespace = "public"
cluster = "default"
}
eureka {
serviceUrl = "http://localhost:1001/eureka"
application = "default"
weight = "1"
}
redis {
serverAddr = "localhost:6381"
db = "0"
}
zk {
cluster = "default"
serverAddr = "127.0.0.1:2181"
session.timeout = 6000
connect.timeout = 2000
}
file {
name = "file.conf"
}
}

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

nacos {
serverAddr = "localhost"
namespace = "public"
cluster = "default"
}
apollo {
app.id = "fescar-server"
apollo.meta = "http://192.168.1.204:8801"
}
zk {
serverAddr = "127.0.0.1:2181"
session.timeout = 6000
connect.timeout = 2000
}
file {
name = "file.conf"
}
}

0 comments on commit 351c7db

Please sign in to comment.