美文网首页
Seata之Storage-Module配置搭建

Seata之Storage-Module配置搭建

作者: Shaw_Young | 来源:发表于2020-10-05 14:04 被阅读0次

    1.seata-storage-service2002

    2.POM

    <dependencies>
        <!--nacos-->
        <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>
            <exclusions>
                <exclusion>
                    <artifactId>seata-all</artifactId>
                    <groupId>io.seata</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-all</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <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.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
    

    3.YML

    server:
      port: 2002
    
    spring:
      application:
        name: seata-storage-service
      cloud:
        alibaba:
          seata:
            tx-service-group: fsp_tx_group
        nacos:
          discovery:
            server-addr: localhost:8848
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/seata_storage
        username: root
        password: 
    
    logging:
      level:
        io:
          seata: info
    mybatis:
      mapperLocations: classpath:mapper/*.xml
    

    4.将file.config和regstry.conf放入resources目录下

    5.domain

    package com.young.springcloud.domain;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class CommonResult<T> {
        private Integer code;
        private String message;
        private T data;
    
        public CommonResult(Integer code, String message) {
            this(code, message, null);
        }
    }
    
    package com.young.springcloud.domain;
     
    import lombok.Data;
     
    @Data
    public class Storage {
     
        private Long id;
     
        // 产品id
        private Long productId;
     
        //总库存
        private Integer total;
        
        //已用库存
        private Integer used;
        
        //剩余库存
        private Integer residue;
    }
    

    6.Dao接口及实现

    package com.young.springcloud.alibaba.dao;
    
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    
    @Mapper
    public interface StorageDao {
    
        //扣减库存信息
        void decrease(@Param("productId") Long productId, @Param("count") Integer count);
    }
    

    7.resources文件夹下新建mapper文件夹后添加

    StorageMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.young.springcloud.alibaba.dao.StorageDao">
    
        <resultMap id="BaseResultMap" type="com.young.springcloud.alibaba.domain.Storage">
            <id column="id" property="id" jdbcType="BIGINT"/>
            <result column="product_id" property="productId" jdbcType="BIGINT"/>
            <result column="total" property="total" jdbcType="INTEGER"/>
            <result column="used" property="used" jdbcType="INTEGER"/>
            <result column="residue" property="residue" jdbcType="INTEGER"/>
        </resultMap>
    
        <update id="decrease">
            UPDATE
                t_storage
            SET
                used = used + #{count},residue = residue - #{count}
            WHERE
                product_id = #{productId}
        </update>
    
    </mapper>
    

    8.Service接口及实现

    package com.young.springcloud.alibaba.service;
    
    public interface StorageService {
        // 扣减库存
        void decrease(Long productId, Integer count);
    }
    
    package com.young.springcloud.alibaba.service.impl;
    
    import com.young.springcloud.alibaba.dao.StorageDao;
    import com.young.springcloud.alibaba.service.StorageService;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    
    @Service
    public class StorageServiceImpl implements StorageService {
     
        private static final Logger LOGGER = LoggerFactory.getLogger(StorageServiceImpl.class);
    
        @Resource
        private StorageDao storageDao;
    
        // 扣减库存
        @Override
        public void decrease(Long productId, Integer count) {
            LOGGER.info("------->storage-service中扣减库存开始");
            storageDao.decrease(productId, count);
            LOGGER.info("------->storage-service中扣减库存结束");
        }
    }
    

    9.Controller

    package com.young.springcloud.alibaba.controller;
    
    import com.young.springcloud.alibaba.domain.CommonResult;
    import com.young.springcloud.alibaba.service.StorageService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
     
    @RestController
    public class StorageController {
     
        @Autowired
        private StorageService storageService;
        
        //扣减库存
        @RequestMapping("/storage/decrease")
        public CommonResult decrease(Long productId, Integer count) {
            storageService.decrease(productId, count);
            return new CommonResult(200,"扣减库存成功!");
        }
    }
    

    10.Config配置

    package com.young.springcloud.alibaba.config;
     
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @MapperScan({"com.young.springcloud.alibaba.dao"})
    public class MyBatisConfig {
    }
    
    package com.young.springcloud.alibaba.config;
     
    import com.alibaba.druid.pool.DruidDataSource;
    import io.seata.rm.datasource.DataSourceProxy;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
     
    import javax.sql.DataSource;
    
    @Configuration
    public class DataSourceProxyConfig {
     
        @Value("${mybatis.mapperLocations}")
        private String mapperLocations;
     
        @Bean
        @ConfigurationProperties(prefix = "spring.datasource")
        public DataSource druidDataSource(){
            return new DruidDataSource();
        }
     
        @Bean
        public DataSourceProxy dataSourceProxy(DataSource dataSource) {
            return new DataSourceProxy(dataSource);
        }
     
        @Bean
        public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSourceProxy);
            sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
            sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
            return sqlSessionFactoryBean.getObject();
        }
     
    }
    

    11.主启动类

    package com.young.springcloud.alibaba;
    
    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;
    
    @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
    @EnableDiscoveryClient
    public class SeataStorageServiceApplication2002 {
        public static void main(String[] args) {
            SpringApplication.run(SeataStorageServiceApplication2002.class, args);
        }
    }
    

    相关文章

      网友评论

          本文标题:Seata之Storage-Module配置搭建

          本文链接:https://www.haomeiwen.com/subject/osesdktx.html