Seata源码—2.seata-samples项目介绍

大纲

1.seata-samples的配置文件和启动类

2.seata-samples业务服务启动时的核心工作

3.seata-samples库存服务的连接池配置

4.Seata对数据库连接池代理配置的分析

5.Dubbo RPC通信过程中传递全局事务XID

6.Seata跟Dubbo整合的Filter(基于SPI机制)

7.seata-samples的AT事务例子原理流程

8.Seata核心配置文件file.conf的内容介绍

1.seata-samples的配置文件和启动类

(1)seata-samples的测试步骤

(2)seata-samples用户服务的配置和启动类

(3)seata-samples库存服务的配置和启动类

(4)seata-samples订单服务的配置和启动类

(5)seata-samples业务服务的配置和启动类

示例仓库:

https://github.com/seata/seata-samples

示例代码的模块ID:seata-samples-dubbo

(1)seata-samples的测试步骤

步骤一:启动DubboAccountServiceStarter

步骤二:启动DubboStorageServiceStarter

步骤三:启动DubboOrderServiceStarter

步骤四:运行DubboBusinessTester

(2)seata-samples用户服务的配置和启动类

dubbo-account-service.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
    <!-- 把jdbc.properties文件里的配置加载进来 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:jdbc.properties"/>
    </bean>
    
    <!-- 将配置文件里的值注入到库存服务的数据库连接池accountDataSource中 -->
    <bean name="accountDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${jdbc.account.url}"/>
        <property name="username" value="${jdbc.account.username}"/>
        <property name="password" value="${jdbc.account.password}"/>
        <property name="driverClassName" value="${jdbc.account.driver}"/>
        <property name="initialSize" value="0"/>
        <property name="maxActive" value="180"/>
        <property name="minIdle" value="0"/>
        <property name="maxWait" value="60000"/>
        <property name="validationQuery" value="Select 'x' from DUAL"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>
        <property name="testWhileIdle" value="true"/>
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
        <property name="minEvictableIdleTimeMillis" value="25200000"/>
        <property name="removeAbandoned" value="true"/>
        <property name="removeAbandonedTimeout" value="1800"/>
        <property name="logAbandoned" value="true"/>
        <property name="filters" value="mergeStat"/>
    </bean>
    
    <!-- 创建数据库连接池代理,通过DataSourceProxy代理accountDataSourceProxy数据库连接池 -->
    <bean id="accountDataSourceProxy" class="io.seata.rm.datasource.DataSourceProxy">
        <constructor-arg ref="accountDataSource"/>
    </bean>
    
    <!-- 将数据库连接池代理accountDataSourceProxy注入到JdbcTemplate数据库操作组件中-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="accountDataSourceProxy"/>
    </bean>

    <dubbo:application name="dubbo-demo-account-service">
        <dubbo:parameter key="qos.enable" value="false"/>
    </dubbo:application>
    <dubbo:registry address="zookeeper://localhost:2181" />
    <dubbo:protocol name="dubbo" port="20881"/>
    <dubbo:service interface="io.seata.samples.dubbo.service.AccountService" ref="service" timeout="10000"/>
    
    <!-- 将JdbcTemplate数据库操作组件注入到AccountServiceImpl中 -->
    <bean id="service" class="io.seata.samples.dubbo.service.impl.AccountServiceImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    
    <!-- 全局事务注解扫描组件 -->
    <bean class="io.seata.spring.annotation.GlobalTransactionScanner">
        <constructor-arg value="dubbo-demo-account-service"/>
        <constructor-arg value="my_test_tx_group"/>
    </bean>
</beans>

启动类:

public class DubboAccountServiceStarter {
    //Account service is ready. A buyer register an account: U100001 on my e-commerce platform
    public static void main(String[] args) {
        ClassPathXmlApplicationContext accountContext = new ClassPathXmlApplicationContext(
            new String[] {"spring/dubbo-account-service.xml"}
        );
        accountContext.getBean("service");
        JdbcTemplate accountJdbcTemplate = (JdbcTemplate)accountContext.getBean("jdbcTemplate");
        accountJdbcTemplate.update("delete from account_tbl where user_id = 'U100001'");
        accountJdbcTemplate.update("insert into account_tbl(user_id, money) values ('U100001', 999)");
        new ApplicationKeeper(accountContext).keep();
    }
}

//The type Application keeper.
public class ApplicationKeeper {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationKeeper.class);
    private final ReentrantLock LOCK = new ReentrantLock();
    private final Condition STOP = LOCK.newCondition();

    //Instantiates a new Application keeper.
    public ApplicationKeeper(AbstractApplicationContext applicationContext) {
        addShutdownHook(applicationContext);
    }
    
    private
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值