增加 seata 示例

This commit is contained in:
Administrator
2020-11-02 15:33:14 +08:00
parent fb945b16d9
commit 002b5a6281
41 changed files with 963 additions and 6 deletions

View File

@ -0,0 +1,24 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.chinaunicom.mall.ebtp.cloud</groupId>
<artifactId>seata-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.chinaunicom.mall.ebtp.cloud</groupId>
<artifactId>storage-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>storage-service</name>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,18 @@
package com.chinaunicom.mall.ebtp.cloud.storage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Import;
import com.chinaunicom.mall.ebtp.cloud.eureka.starter.EurekaStarterConfiguration;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableFeignClients
@Import(EurekaStarterConfiguration.class)
public class StorageSeataApplication {
public static void main(String[] args) {
SpringApplication.run(StorageSeataApplication.class, args);
}
}

View File

@ -0,0 +1,42 @@
package com.chinaunicom.mall.ebtp.cloud.storage.config;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;
@Configuration
public class SeataDatasourceConfiguration {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
return new DruidDataSource();
}
@Primary
@Bean("dataSource")
public DataSourceProxy dataSource(DataSource druidDataSource) {
return new DataSourceProxy(druidDataSource);
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSourceProxy);
sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
return sqlSessionFactoryBean.getObject();
}
}

View File

@ -0,0 +1,22 @@
package com.chinaunicom.mall.ebtp.cloud.storage.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.chinaunicom.mall.ebtp.cloud.storage.service.StorageService;
@RestController
@RequestMapping("storage")
public class StorageController {
private @Autowired StorageService storageService;
@RequestMapping("decrease")
public String decrease(@RequestParam("productId") Long productId, @RequestParam("count") Integer count) {
storageService.decrease(productId, count);
return "Decrease Storage success";
}
}

View File

@ -0,0 +1,19 @@
package com.chinaunicom.mall.ebtp.cloud.storage.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@TableName
@Accessors(chain = true)
public class Storage {
private Long id;
private Long productId;
private Integer total;
private Integer used;
private Integer residue;
}

View File

@ -0,0 +1,16 @@
package com.chinaunicom.mall.ebtp.cloud.storage.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chinaunicom.mall.ebtp.cloud.storage.entity.Storage;
@Mapper
public interface StorageMapper extends BaseMapper<Storage> {
@Update("UPDATE storage SET used = used + #{count}, residue = residue - #{count} WHERE product_id = #{productId}")
void decrease(@Param("productId") Long productId, @Param("count") Integer count);
}

View File

@ -0,0 +1,7 @@
package com.chinaunicom.mall.ebtp.cloud.storage.service;
public interface StorageService {
void decrease(Long productId, Integer count);
}

View File

@ -0,0 +1,24 @@
package com.chinaunicom.mall.ebtp.cloud.storage.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chinaunicom.mall.ebtp.cloud.storage.mapper.StorageMapper;
import com.chinaunicom.mall.ebtp.cloud.storage.service.StorageService;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class StorageServiceImpl implements StorageService {
private @Autowired StorageMapper storageMapper;
@Override
public void decrease(Long productId, Integer count) {
log.info("------->扣减库存开始");
storageMapper.decrease(productId, count);
log.info("------->扣减库存结束");
}
}

View File

@ -0,0 +1,18 @@
logging:
level:
com.chinaunicom.mall.ebtp.cloud: info
server:
port: 8674
spring:
cloud:
alibaba:
seata:
tx-service-group: test_tx_group
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://125.32.114.204:13306/ebtp_mall_evaluation?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: mall3-ebtp-dev
password: mall3-ebtp-dev

View File

@ -0,0 +1,5 @@
spring:
profiles:
active: dev
application:
name: storage-service

View File

@ -0,0 +1,38 @@
package com.chinaunicom.mall.ebtp.cloud.storage;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}