Skip to content

Commit

Permalink
接入seata
Browse files Browse the repository at this point in the history
  • Loading branch information
codefarmer008 committed Sep 8, 2022
1 parent aeed9af commit f490790
Show file tree
Hide file tree
Showing 20 changed files with 737 additions and 0 deletions.
62 changes: 62 additions & 0 deletions order/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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>
<artifactId>springCloudDemo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>order</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Seata -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
</dependency>
</dependencies>


</project>
17 changes: 17 additions & 0 deletions order/src/main/java/org/example/order/OrderServiceApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.example.order;

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

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class OrderServiceApplication {

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.example.order.controller;

import org.example.order.feign.StockFeignClient;
import org.example.order.service.OrderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("order")
public class OrderController {

@Resource
private OrderService orderService;
@Resource
private StockFeignClient stockFeignClient;

/**
* 下单:插入订单表、扣减库存,模拟回滚
*
* @return
*/
@RequestMapping("/placeOrder/commit")
public Boolean placeOrderCommit() {

orderService.placeOrder("1", "product-1", 1);
return true;
}

/**
* 下单:插入订单表、扣减库存,模拟回滚
*
* @return
*/
@RequestMapping("/placeOrder/rollback")
public Boolean placeOrderRollback() {
// product-2 扣库存时模拟了一个业务异常,
orderService.placeOrder("1", "product-2", 1);
return true;
}

@RequestMapping("/placeOrder")
public Boolean placeOrder(String userId, String commodityCode, Integer count) {
orderService.placeOrder(userId, commodityCode, count);
return true;
}
}
12 changes: 12 additions & 0 deletions order/src/main/java/org/example/order/dao/OrderDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.example.order.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.example.order.po.Order;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface OrderDAO extends BaseMapper<Order> {

}
12 changes: 12 additions & 0 deletions order/src/main/java/org/example/order/feign/StockFeignClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.example.order.feign;

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

@FeignClient(name = "stock-service")
public interface StockFeignClient {

@GetMapping("stock/deduct")
Boolean deduct(@RequestParam("commodityCode") String commodityCode, @RequestParam("count") Integer count);
}
61 changes: 61 additions & 0 deletions order/src/main/java/org/example/order/po/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.example.order.po;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.math.BigDecimal;

@TableName("order_tbl")
public class Order {
@TableId(type = IdType.AUTO)
private Integer id;

public String userId;

public String commodityCode;

public int count;

public BigDecimal money;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getCommodityCode() {
return commodityCode;
}

public void setCommodityCode(String commodityCode) {
this.commodityCode = commodityCode;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public BigDecimal getMoney() {
return money;
}

public void setMoney(BigDecimal money) {
this.money = money;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.order.service;


public interface OrderService {

void placeOrder(String userId, String commodityCode, Integer count);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.example.order.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import org.example.order.dao.OrderDAO;
import org.example.order.feign.StockFeignClient;
import org.example.order.po.Order;
import org.example.order.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class OrderServiceImpl implements OrderService {

// @Resource
// private AccountFeignClient accountFeignClient;
@Resource
private StockFeignClient stockFeignClient;
@Resource
private OrderDAO orderDAO;

/**
* 下单:创建订单、减库存,涉及到两个服务
*
* @param userId
* @param commodityCode
* @param count
*/
@GlobalTransactional
@Transactional(rollbackFor = Exception.class)
public void placeOrder(String userId, String commodityCode, Integer count) {
BigDecimal orderMoney = new BigDecimal(count).multiply(new BigDecimal(5));
Order order = new Order();
order.setUserId(userId);
order.setCommodityCode(commodityCode);
order.setCount(count);
order.setMoney(orderMoney);
orderDAO.insert(order);
stockFeignClient.deduct(commodityCode, count);

}

// @Transactional(rollbackFor = Exception.class)
// public void create(String userId, String commodityCode, int count) {
//
// BigDecimal orderMoney = new BigDecimal(count).multiply(new BigDecimal(5));
//
// Order order = new Order();
// order.setUserId(userId);
// order.setCommodityCode(commodityCode);
// order.setCount(count);
// order.setMoney(orderMoney);
// orderDAO.insert(order);
//
// accountFeignClient.reduce(userId, orderMoney);
//
// }

}
38 changes: 38 additions & 0 deletions order/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
server:
port: 9091
spring:
application:
name: order-service
cloud:
nacos:
config:
server-addr: 192.168.220.125:8848
file-extension: yml
discovery:
# Nacos 注册中心地址
server-addr: 192.168.220.125:8848
# seata 服务分组,要与服务端nacos-config.txt中service.vgroup_mapping的后缀对应
alibaba:
seata:
tx-service-group: my_test_tx_group
# 数据源配置
datasource:
url: jdbc:mysql://192.168.220.125:3306/seata_order?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
driverClassName: com.mysql.cj.jdbc.Driver
username: root
password: Root123
seata:
registry: # TC服务注册中心的配置,微服务根据这些信息去注册中心获取tc服务地址
# 参考tc服务自己的registry.conf中的配置
type: nacos
nacos: # tc
server-addr: 192.168.220.125:8848
namespace: ""
group: SEATA_GROUP
application: seata-server # tc服务在nacos中的服务名称
tx-service-group: my_test_tx_group # 事务组,根据这个获取tc服务的cluster名称

logging:
level:
io:
seata: debug
Loading

0 comments on commit f490790

Please sign in to comment.