增加seata-demo工程

This commit is contained in:
zlt
2019-09-15 10:48:17 +08:00
parent 6632da7b19
commit deac4ac957
44 changed files with 1045 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zlt</groupId>
<artifactId>seata-demo</artifactId>
<version>2.6.0</version>
</parent>
<artifactId>order-service</artifactId>
<dependencies>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>seata-common-starter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-ribbon-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,24 @@
package com.central.order;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* 订单
*
* @author zlt
* @date 2019/9/14
*/
@EnableDiscoveryClient
@EnableFeignClients
@MapperScan({"com.central.order.mapper"})
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
@@ -0,0 +1,32 @@
package com.central.order.controller;
import com.central.order.service.OrderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 订单
*
* @author zlt
* @date 2019/9/14
*/
@RestController
@RequestMapping("order")
public class OrderController {
@Resource
private OrderService orderService;
/**
* 创建订单
* @param userId 用户id
* @param commodityCode 订单编号
* @param count 数量
*/
@RequestMapping("/create")
public Boolean create(String userId, String commodityCode, Integer count) {
orderService.create(userId, commodityCode, count);
return true;
}
}
@@ -0,0 +1,15 @@
package com.central.order.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author zlt
* @date 2019/9/14
*/
@FeignClient(name = "account-service")
public interface AccountFeignClient {
@PostMapping("account/reduce")
Boolean reduce(@RequestParam("userId") String userId, @RequestParam("money") Integer money);
}
@@ -0,0 +1,12 @@
package com.central.order.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.central.order.model.Order;
/**
* @author zlt
* @date 2019/9/14
*/
public interface OrderMapper extends BaseMapper<Order> {
}
@@ -0,0 +1,28 @@
package com.central.order.model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
/**
* 订单
*
* @author zlt
* @date 2019/9/14
*/
@Data
@Accessors(chain = true)
@TableName("order_tbl")
public class Order {
@TableId(type = IdType.AUTO)
private Integer id;
private String userId;
private String commodityCode;
private Integer count;
private Integer money;
}
@@ -0,0 +1,39 @@
package com.central.order.service;
import com.central.order.mapper.OrderMapper;
import com.central.order.feign.AccountFeignClient;
import com.central.order.model.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
* @author zlt
* @date 2019/9/14
*/
@Slf4j
@Service
public class OrderService {
@Resource
private AccountFeignClient accountFeignClient;
@Resource
private OrderMapper orderMapper;
//@Transactional(rollbackFor = Exception.class)
public void create(String userId, String commodityCode, Integer count) {
//订单金额
Integer orderMoney = count * 2;
Order order = new Order()
.setUserId(userId)
.setCommodityCode(commodityCode)
.setCount(count)
.setMoney(orderMoney);
orderMapper.insert(order);
accountFeignClient.reduce(userId, orderMoney);
}
}
@@ -0,0 +1,11 @@
spring:
datasource:
url: jdbc:mysql://192.168.28.130:3306/seata-demo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: root
password: 1q2w3e4r
driver-class-name: com.mysql.jdbc.Driver
cloud:
alibaba:
seata:
# seata 服务分组,要与服务端nacos-config.txt中service.vgroup_mapping的后缀对应,默认是${spring.application.name}-fescar-service-group
tx-service-group: order-service-group
@@ -0,0 +1,10 @@
server:
port: 9091
spring:
application:
name: order-service
cloud:
nacos:
discovery:
server-addr: 192.168.28.130:8848
@@ -0,0 +1,26 @@
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
serverAddr = "192.168.28.130"
namespace = "public"
cluster = "default"
}
file {
name = "file.conf"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "192.168.28.130"
namespace = "public"
cluster = "default"
}
file {
name = "file.conf"
}
}