增加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
@@ -0,0 +1,24 @@
package com.central.storage;
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.storage.mapper"})
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class StorageServiceApplication {
public static void main(String[] args) {
SpringApplication.run(StorageServiceApplication.class, args);
}
}
@@ -0,0 +1,31 @@
package com.central.storage.controller;
import com.central.storage.service.StorageService;
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("storage")
public class StorageController {
@Resource
private StorageService storageService;
/**
* 减库存
* @param commodityCode 商品代码
* @param count 数量
*/
@RequestMapping(path = "/deduct")
public Boolean deduct(String commodityCode, Integer count) {
storageService.deduct(commodityCode, count);
return true;
}
}
@@ -0,0 +1,12 @@
package com.central.storage.mapper;
import com.central.db.mapper.SuperMapper;
import com.central.storage.model.Storage;
/**
* @author zlt
* @date 2019/9/14
*/
public interface StorageMapper extends SuperMapper<Storage> {
}
@@ -0,0 +1,23 @@
package com.central.storage.model;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 库存
*
* @author zlt
* @date 2019/9/14
*/
@Data
@Accessors(chain = true)
@TableName("storage_tbl")
public class Storage {
@TableId
private Long id;
private String commodityCode;
private Long count;
}
@@ -0,0 +1,37 @@
package com.central.storage.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.central.storage.model.Storage;
import com.central.storage.mapper.StorageMapper;
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 StorageService {
@Resource
private StorageMapper storageMapper;
/**
* 减库存
*
* @param commodityCode 商品编号
* @param count 数量
*/
//@Transactional(rollbackFor = Exception.class)
public void deduct(String commodityCode, int count) {
QueryWrapper<Storage> wrapper = new QueryWrapper<>();
wrapper.setEntity(new Storage().setCommodityCode(commodityCode));
Storage storage = storageMapper.selectOne(wrapper);
storage.setCount(storage.getCount() - count);
storageMapper.updateById(storage);
}
}
@@ -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: storage-service-group
@@ -0,0 +1,10 @@
server:
port: 9092
spring:
application:
name: storage-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"
}
}