seata [SAGA] STATE_MACHINE_DEF 表 APP_NAME 字段无法替换为自定义字段

uqcuzwp8  于 2023-02-04  发布在  Mac
关注(0)|答案(5)|浏览(146)
  • I have searched the issues of this repository and believe that this is not a duplicate.

Ⅰ. Issue Description

SAGA STATE_MACHINE_DEF 表 APP_NAME 字段无法替换为自定义字段。
通过 DbStateMachineConfig.setApplicationId("XXX") 设置后的 ApplicationId 无法替换 APP_NAME
且通过 Saga.JSON 文件定义 AppName 也不生效

{
  "Name": "test",
  "Comment": "test example",
  "AppName": "XXX",
  "StartState": "step1",
  "Version": "0.0.1"
  ......
}

Ⅱ. Describe what happened

If there is an exception, please attach the exception trace:

Just paste your stack trace here!

Ⅲ. Describe what you expected to happen

Ⅳ. How to reproduce it (as minimally and precisely as possible)

  1. xxx
  2. xxx
  3. xxx

Ⅴ. Anything else we need to know?

Ⅵ. Environment:

  • JDK version :
  • OS :
  • Others:
v7pvogib

v7pvogib1#

请问你用的是seata几版本?

14ifxucb

14ifxucb2#

statelang里,是不能配置APP_NAME的。

rn0zuynd

rn0zuynd3#

applicationId 必须要在 DbStateMachineConfig.afterPropertiesSet 触发之前设置进去,才会生效。

oknwwptz

oknwwptz4#

applicationId 必须要在 DbStateMachineConfig.afterPropertiesSet 触发之前设置进去,才会生效。

我使用的版本是:1.3.0
以下是的代码

@ConditionalOnMissingBean(StateMachineConfig.class)
    @Bean
    public StateMachineConfig stateMachineConfig(SagaConfig sagaConfig) throws Exception {
        DbStateMachineConfig dbStateMachineConfig = new DbStateMachineConfig();
        // 在这里已经设置了ApplicationId
        dbStateMachineConfig.setApplicationId(sagaConfig.getApplicationId());
        // sdbStateMachineConfig.setDbType("oracle");
        dbStateMachineConfig.setTxServiceGroup(sagaConfig.getApplicationGroup());
        dbStateMachineConfig.setDefaultTenantId(sagaConfig.getApplicationTenant());
        DataSource dataSource = (DataSource) applicationContext.getBean(sagaConfig.getDataSource());
        dbStateMachineConfig.setDataSource(dataSource);
        dbStateMachineConfig.setResources(applicationContext.getResources(sagaConfig.getResources()));

        if (!StringUtils.isEmpty(sagaConfig.getTablePrefix())) {
            dbStateMachineConfig.setTablePrefix(sagaConfig.getTablePrefix());
        }

        dbStateMachineConfig.setTransOperationTimeout(sagaConfig.getTransOperationTimeout());
        dbStateMachineConfig.setServiceInvokeTimeout(sagaConfig.getServiceInvokeTimeout());

        String dbType = dbStateMachineConfig.getDbTypeFromDataSource(dataSource);

        DbAndReportTcStateLogStore dbStateLogStore = new DbAndReportTcStateLogStore();
        dbStateLogStore.setDataSource(dataSource);
        dbStateLogStore.setTablePrefix(dbStateMachineConfig.getTablePrefix());
        dbStateLogStore.setDbType(dbType);
        dbStateLogStore.setDefaultTenantId(dbStateMachineConfig.getDefaultTenantId());
        dbStateLogStore.setSeqGenerator(dbStateMachineConfig.getSeqGenerator());

        if (StringUtils.hasLength(dbStateMachineConfig.getSagaJsonParser())) {
            ParamsSerializer paramsSerializer = new ParamsSerializer();
            paramsSerializer.setJsonParserName(dbStateMachineConfig.getSagaJsonParser());
            dbStateLogStore.setParamsSerializer(paramsSerializer);
        }

        DefaultSagaTransactionalTemplate defaultSagaTransactionalTemplate
                = new DefaultSagaTransactionalTemplate();
        defaultSagaTransactionalTemplate.setApplicationContext(applicationContext);
        // 这里的ApplicationId 是正确的
        defaultSagaTransactionalTemplate.setApplicationId(dbStateMachineConfig.getApplicationId());
        defaultSagaTransactionalTemplate.setTxServiceGroup(dbStateMachineConfig.getTxServiceGroup());
        defaultSagaTransactionalTemplate.afterPropertiesSet();

        dbStateMachineConfig.setSagaTransactionalTemplate(defaultSagaTransactionalTemplate);

        dbStateLogStore.setSagaTransactionalTemplate(defaultSagaTransactionalTemplate);

        dbStateMachineConfig.setStateLogStore(dbStateLogStore);

        return dbStateMachineConfig;
    }

没有写入成功的原因:
StateMachineRepositoryImpl.java

@Override
    public void registryByResources(Resource[] resources, String tenantId) throws IOException {
        if (resources != null) {
            for (Resource resource : resources) {
                String json = IOUtils.toString(resource.getInputStream(), charset);
                StateMachine stateMachine = StateMachineParserFactory.getStateMachineParser(jsonParserName).parse(json);
                if (stateMachine != null) {
                    stateMachine.setContent(json);
                    // 这里只补充了租户信息,缺少ApplicationId 
                    if (StringUtils.isBlank(stateMachine.getTenantId())) {
                        stateMachine.setTenantId(tenantId);
                    }
                    registryStateMachine(stateMachine);
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("===== StateMachine Loaded: \n{}", json);
                    }
                }
            }
        }
    }

可以在StateMachineRepositoryImpl 增加ApplicationId 字段也可以在Parse侧附加ApplicationId 但都不太优雅,希望官方给个合适方案。

nwo49xxi

nwo49xxi5#

请参照 PR #2838
这个PR添加了Saga自动装配。目前还没有合并。

相关问题