From 34d82670684025410fe3f183ba10e8d265a796a4 Mon Sep 17 00:00:00 2001 From: slievrly Date: Tue, 31 Dec 2019 18:40:14 +0800 Subject: [PATCH] adapter: use seata-spring-boot-starter's autoConfiguration instead of spring-cloud-alibaba-seata-starter's autoConfiguration Signed-off-by: slievrly --- spring-cloud-alibaba-dependencies/pom.xml | 4 +- .../seata-example/account-service/pom.xml | 11 +++ .../cloud/examples/DatabaseConfiguration.java | 73 ++++--------------- .../src/main/resources/application.properties | 29 ++++++-- .../src/main/resources/file.conf | 67 ----------------- .../src/main/resources/registry.conf | 73 ------------------- .../seata-example/business-service/pom.xml | 4 + .../cloud/examples/HomeController.java | 11 ++- .../src/main/resources/application.properties | 15 ++++ .../src/main/resources/file.conf | 65 +---------------- .../seata-example/order-service/pom.xml | 11 +++ .../cloud/examples/DatabaseConfiguration.java | 70 ++++-------------- .../src/main/resources/application.properties | 28 +++++-- .../src/main/resources/file.conf | 67 ----------------- .../src/main/resources/registry.conf | 73 ------------------- .../seata-example/storage-service/pom.xml | 17 +++-- .../cloud/examples/DatabaseConfiguration.java | 73 ++++--------------- .../src/main/resources/application.properties | 29 ++++++-- .../src/main/resources/file.conf | 67 ----------------- .../src/main/resources/registry.conf | 73 ------------------- spring-cloud-alibaba-seata/pom.xml | 2 +- .../GlobalTransactionAutoConfiguration.java | 61 ---------------- .../alibaba/cloud/seata/SeataProperties.java | 42 ----------- .../main/resources/META-INF/spring.factories | 1 - 24 files changed, 181 insertions(+), 785 deletions(-) delete mode 100644 spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/file.conf delete mode 100644 spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/registry.conf delete mode 100644 spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/file.conf delete mode 100644 spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/registry.conf delete mode 100644 spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/file.conf delete mode 100644 spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/registry.conf delete mode 100644 spring-cloud-alibaba-seata/src/main/java/com/alibaba/cloud/seata/GlobalTransactionAutoConfiguration.java delete mode 100644 spring-cloud-alibaba-seata/src/main/java/com/alibaba/cloud/seata/SeataProperties.java diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 326931d4..3e58e7f8 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -20,7 +20,7 @@ 1.6.3 3.1.0 - 0.9.0 + 1.0.0 1.1.4 0.8.0 1.0.9 @@ -204,7 +204,7 @@ io.seata - seata-all + seata-spring-boot-starter ${seata.version} diff --git a/spring-cloud-alibaba-examples/seata-example/account-service/pom.xml b/spring-cloud-alibaba-examples/seata-example/account-service/pom.xml index 428fcebc..00bfe262 100644 --- a/spring-cloud-alibaba-examples/seata-example/account-service/pom.xml +++ b/spring-cloud-alibaba-examples/seata-example/account-service/pom.xml @@ -29,9 +29,20 @@ spring-boot-starter-jdbc + + com.alibaba + druid-spring-boot-starter + 1.1.10 + mysql mysql-connector-java + 5.1.31 + + + log4j + log4j + 1.2.17 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/seata-example/account-service/src/main/java/com/alibaba/cloud/examples/DatabaseConfiguration.java b/spring-cloud-alibaba-examples/seata-example/account-service/src/main/java/com/alibaba/cloud/examples/DatabaseConfiguration.java index 624bf47b..5f143f18 100644 --- a/spring-cloud-alibaba-examples/seata-example/account-service/src/main/java/com/alibaba/cloud/examples/DatabaseConfiguration.java +++ b/spring-cloud-alibaba-examples/seata-example/account-service/src/main/java/com/alibaba/cloud/examples/DatabaseConfiguration.java @@ -16,15 +16,14 @@ package com.alibaba.cloud.examples; -import java.sql.SQLException; +import javax.sql.DataSource; import com.alibaba.druid.pool.DruidDataSource; -import io.seata.rm.datasource.DataSourceProxy; -import org.springframework.context.ApplicationContext; +import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.Environment; +import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate; /** @@ -33,61 +32,21 @@ import org.springframework.jdbc.core.JdbcTemplate; @Configuration public class DatabaseConfiguration { - private final ApplicationContext applicationContext; + @Bean + @Primary + @ConfigurationProperties("spring.datasource") + public DataSource storageDataSource() { + return new DruidDataSource(); + } - public DatabaseConfiguration(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; - } + @Bean + public JdbcTemplate jdbcTemplate(DataSource dataSource) { + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); - @Bean(initMethod = "init", destroyMethod = "close") - public DruidDataSource storageDataSource() throws SQLException { + jdbcTemplate.update("delete from account_tbl where user_id = 'U100001'"); + jdbcTemplate.update("insert into account_tbl(user_id, money) values ('U100001', 10000)"); - Environment environment = applicationContext.getEnvironment(); - - String ip = environment.getProperty("mysql.server.ip"); - String port = environment.getProperty("mysql.server.port"); - String dbName = environment.getProperty("mysql.db.name"); - - String userName = environment.getProperty("mysql.user.name"); - String password = environment.getProperty("mysql.user.password"); - - DruidDataSource druidDataSource = new DruidDataSource(); - druidDataSource.setUrl( - "jdbc:mysql://" + ip + ":" + port + "/" + dbName + "?serverTimezone=UTC"); - druidDataSource.setUsername(userName); - druidDataSource.setPassword(password); - druidDataSource.setDriverClassName("com.mysql.jdbc.Driver"); - druidDataSource.setInitialSize(0); - druidDataSource.setMaxActive(180); - druidDataSource.setMaxWait(60000); - druidDataSource.setMinIdle(0); - druidDataSource.setValidationQuery("Select 'x' from DUAL"); - druidDataSource.setTestOnBorrow(false); - druidDataSource.setTestOnReturn(false); - druidDataSource.setTestWhileIdle(true); - druidDataSource.setTimeBetweenEvictionRunsMillis(60000); - druidDataSource.setMinEvictableIdleTimeMillis(25200000); - druidDataSource.setRemoveAbandoned(true); - druidDataSource.setRemoveAbandonedTimeout(1800); - druidDataSource.setLogAbandoned(true); - druidDataSource.setFilters("mergeStat"); - return druidDataSource; - } - - @Bean - public DataSourceProxy dataSourceProxy(DruidDataSource druidDataSource) { - return new DataSourceProxy(druidDataSource); - } - - @Bean - public JdbcTemplate jdbcTemplate(DataSourceProxy dataSourceProxy) { - JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSourceProxy); - - jdbcTemplate.update("delete from account_tbl where user_id = 'U100001'"); - jdbcTemplate.update( - "insert into account_tbl(user_id, money) values ('U100001', 10000)"); - - return jdbcTemplate; - } + return jdbcTemplate; + } } diff --git a/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/application.properties b/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/application.properties index 8884c924..3670a662 100644 --- a/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/application.properties @@ -1,9 +1,28 @@ spring.application.name=account-service server.port=18084 -mysql.server.ip=127.0.0.1 -mysql.server.port=3306 -mysql.db.name=demo +spring.datasource.name="accountDataSource" +spring.datasource.type=com.alibaba.druid.pool.DruidDataSource +spring.datasource.driver-class-name=com.mysql.jdbc.Driver +spring.datasource.url=jdbc:mysql://rm-2zetd9474ydd1g5955o.mysql.rds.aliyuncs.com:3306/fescar?useSSL=false&serverTimezone=UTC +spring.datasource.username=workshop +spring.datasource.password=Workshop123 +spring.datasource.druid.max-active=20 +spring.datasource.druid.min-idle=2 +spring.datasource.druid.initial-size=2 + +seata.enabled=true +seata.service.vgroup-mapping=default +seata.service.grouplist=127.0.0.1:8091 +seata.service.disable-global-transaction=false + +## if use registry center +#seata.registry.type=nacos +#seata.registry.nacos.cluster=default +#seata.registry.nacos.server-addr=localhost +# +## if use config center +#seata.config.type=apollo +#seata.config.apollo.apollo-meta=http://192.168.1.204:8801 +#seata.config.apollo.app-id=seata-server -mysql.user.name=root -mysql.user.password=123456 diff --git a/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/file.conf b/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/file.conf deleted file mode 100644 index 0777ae8a..00000000 --- a/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/file.conf +++ /dev/null @@ -1,67 +0,0 @@ -transport { - # tcp udt unix-domain-socket - type = "TCP" - #NIO NATIVE - server = "NIO" - #enable heartbeat - heartbeat = false - #thread factory for netty - thread-factory { - boss-thread-prefix = "NettyBoss" - worker-thread-prefix = "NettyServerNIOWorker" - server-executor-thread-prefix = "NettyServerBizHandler" - share-boss-worker = false - client-selector-thread-prefix = "NettyClientSelector" - client-selector-thread-size = 1 - client-worker-thread-prefix = "NettyClientWorkerThread" - # netty boss thread size,will not be used for UDT - boss-thread-size = 1 - #auto default pin or 8 - worker-thread-size = 8 - } - shutdown { - # when destroy server, wait seconds - wait = 3 - } - serialization = "seata" - compressor = "none" -} -service { - #vgroup->rgroup - vgroup_mapping.account-service-seata-service-group = "default" - #only support single node - default.grouplist = "127.0.0.1:8091" - #degrade current not support - enableDegrade = false - #disable - disable = false - #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent - max.commit.retry.timeout = "-1" - max.rollback.retry.timeout = "-1" -} -client { - async.commit.buffer.limit = 10000 - lock { - retry.internal = 10 - retry.times = 30 - } - report.retry.count = 5 - tm.commit.retry.count = 1 - tm.rollback.retry.count = 1 -} -transaction { - undo.data.validation = true - undo.log.serialization = "jackson" - undo.log.save.days = 7 - #schedule delete expired undo_log in milliseconds - undo.log.delete.period = 86400000 - undo.log.table = "undo_log" -} - -support { - ## spring - spring { - # auto proxy the DataSource bean - datasource.autoproxy = false - } -} \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/registry.conf b/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/registry.conf deleted file mode 100644 index b98f5704..00000000 --- a/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/registry.conf +++ /dev/null @@ -1,73 +0,0 @@ -registry { - # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa - type = "file" - - nacos { - serverAddr = "localhost" - namespace = "" - cluster = "default" - } - eureka { - serviceUrl = "http://localhost:8761/eureka" - application = "default" - weight = "1" - } - redis { - serverAddr = "localhost:6379" - db = "0" - } - zk { - cluster = "default" - serverAddr = "127.0.0.1:2181" - session.timeout = 6000 - connect.timeout = 2000 - } - consul { - cluster = "default" - serverAddr = "127.0.0.1:8500" - } - etcd3 { - cluster = "default" - serverAddr = "http://localhost:2379" - } - sofa { - serverAddr = "127.0.0.1:9603" - application = "default" - region = "DEFAULT_ZONE" - datacenter = "DefaultDataCenter" - cluster = "default" - group = "SEATA_GROUP" - addressWaitTime = "3000" - } - file { - name = "file.conf" - } -} - -config { - # file、nacos 、apollo、zk、consul、etcd3 - type = "file" - - nacos { - serverAddr = "localhost" - namespace = "" - } - consul { - serverAddr = "127.0.0.1:8500" - } - apollo { - app.id = "seata-server" - apollo.meta = "http://192.168.1.204:8801" - } - zk { - serverAddr = "127.0.0.1:2181" - session.timeout = 6000 - connect.timeout = 2000 - } - etcd3 { - serverAddr = "http://localhost:2379" - } - file { - name = "file.conf" - } -} diff --git a/spring-cloud-alibaba-examples/seata-example/business-service/pom.xml b/spring-cloud-alibaba-examples/seata-example/business-service/pom.xml index bd58fe41..50425c36 100644 --- a/spring-cloud-alibaba-examples/seata-example/business-service/pom.xml +++ b/spring-cloud-alibaba-examples/seata-example/business-service/pom.xml @@ -20,6 +20,10 @@ org.springframework.cloud spring-cloud-starter-openfeign + + org.springframework.cloud + spring-cloud-starter-loadbalancer + org.springframework.cloud spring-cloud-starter-netflix-hystrix diff --git a/spring-cloud-alibaba-examples/seata-example/business-service/src/main/java/com/alibaba/cloud/examples/HomeController.java b/spring-cloud-alibaba-examples/seata-example/business-service/src/main/java/com/alibaba/cloud/examples/HomeController.java index 2b671552..d12d820c 100644 --- a/spring-cloud-alibaba-examples/seata-example/business-service/src/main/java/com/alibaba/cloud/examples/HomeController.java +++ b/spring-cloud-alibaba-examples/seata-example/business-service/src/main/java/com/alibaba/cloud/examples/HomeController.java @@ -24,6 +24,7 @@ import org.slf4j.LoggerFactory; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.util.LinkedMultiValueMap; @@ -87,11 +88,13 @@ public class HomeController { HttpEntity> request = new HttpEntity>( map, headers); - ResponseEntity response = restTemplate.postForEntity(url, request, - String.class); - + ResponseEntity response; + try { + response = restTemplate.postForEntity(url, request, String.class); + } catch (Exception exx) { + return "mock error"; + } result = response.getBody(); - if (!SUCCESS.equals(result)) { throw new RuntimeException(); } diff --git a/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/application.properties b/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/application.properties index 9ac7ad4d..33e396bd 100644 --- a/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/application.properties @@ -6,3 +6,18 @@ spring.application.name=business-service #feign.sentinel.enabled=true logging.level.io.seata=debug + +seata.enabled=true +seata.service.vgroup-mapping=default +seata.service.grouplist=127.0.0.1:8091 +seata.service.disable-global-transaction=false + +## if use registry center +#seata.registry.type=nacos +#seata.registry.nacos.cluster=default +#seata.registry.nacos.server-addr=localhost +# +## if use config center +#seata.config.type=apollo +#seata.config.apollo.apollo-meta=http://192.168.1.204:8801 +#seata.config.apollo.app-id=seata-server \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/file.conf b/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/file.conf index a6e431bc..a0fd5dd8 100644 --- a/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/file.conf +++ b/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/file.conf @@ -1,67 +1,4 @@ -transport { - # tcp udt unix-domain-socket - type = "TCP" - #NIO NATIVE - server = "NIO" - #enable heartbeat - heartbeat = false - #thread factory for netty - thread-factory { - boss-thread-prefix = "NettyBoss" - worker-thread-prefix = "NettyServerNIOWorker" - server-executor-thread-prefix = "NettyServerBizHandler" - share-boss-worker = false - client-selector-thread-prefix = "NettyClientSelector" - client-selector-thread-size = 1 - client-worker-thread-prefix = "NettyClientWorkerThread" - # netty boss thread size,will not be used for UDT - boss-thread-size = 1 - #auto default pin or 8 - worker-thread-size = 8 - } - shutdown { - # when destroy server, wait seconds - wait = 3 - } - serialization = "seata" - compressor = "none" -} service { - #vgroup->rgroup - vgroup_mapping.business-service-seata-service-group = "default" - #only support single node - default.grouplist = "127.0.0.1:8091" - #degrade current not support - enableDegrade = false #disable - disable = false - #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent - max.commit.retry.timeout = "-1" - max.rollback.retry.timeout = "-1" -} -client { - async.commit.buffer.limit = 10000 - lock { - retry.internal = 10 - retry.times = 30 - } - report.retry.count = 5 - tm.commit.retry.count = 1 - tm.rollback.retry.count = 1 -} -transaction { - undo.data.validation = true - undo.log.serialization = "jackson" - undo.log.save.days = 7 - #schedule delete expired undo_log in milliseconds - undo.log.delete.period = 86400000 - undo.log.table = "undo_log" -} - -support { - ## spring - spring { - # auto proxy the DataSource bean - datasource.autoproxy = false - } + disableGlobalTransaction = false } \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/seata-example/order-service/pom.xml b/spring-cloud-alibaba-examples/seata-example/order-service/pom.xml index 0749a727..65cbc0c0 100644 --- a/spring-cloud-alibaba-examples/seata-example/order-service/pom.xml +++ b/spring-cloud-alibaba-examples/seata-example/order-service/pom.xml @@ -29,9 +29,20 @@ spring-boot-starter-jdbc + + com.alibaba + druid-spring-boot-starter + 1.1.10 + mysql mysql-connector-java + 5.1.31 + + + log4j + log4j + 1.2.17 diff --git a/spring-cloud-alibaba-examples/seata-example/order-service/src/main/java/com/alibaba/cloud/examples/DatabaseConfiguration.java b/spring-cloud-alibaba-examples/seata-example/order-service/src/main/java/com/alibaba/cloud/examples/DatabaseConfiguration.java index cd83beec..65df1e82 100644 --- a/spring-cloud-alibaba-examples/seata-example/order-service/src/main/java/com/alibaba/cloud/examples/DatabaseConfiguration.java +++ b/spring-cloud-alibaba-examples/seata-example/order-service/src/main/java/com/alibaba/cloud/examples/DatabaseConfiguration.java @@ -16,15 +16,14 @@ package com.alibaba.cloud.examples; -import java.sql.SQLException; +import javax.sql.DataSource; import com.alibaba.druid.pool.DruidDataSource; -import io.seata.rm.datasource.DataSourceProxy; -import org.springframework.context.ApplicationContext; +import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.Environment; +import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate; /** @@ -33,59 +32,20 @@ import org.springframework.jdbc.core.JdbcTemplate; @Configuration public class DatabaseConfiguration { - private final ApplicationContext applicationContext; + @Bean + @Primary + @ConfigurationProperties("spring.datasource") + public DataSource storageDataSource() { + return new DruidDataSource(); + } - public DatabaseConfiguration(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; - } + @Bean + public JdbcTemplate jdbcTemplate(DataSource dataSource) { + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); - @Bean(initMethod = "init", destroyMethod = "close") - public DruidDataSource storageDataSource() throws SQLException { + jdbcTemplate.execute("TRUNCATE TABLE order_tbl"); - Environment env = applicationContext.getEnvironment(); - - String ip = env.getProperty("mysql.server.ip"); - String port = env.getProperty("mysql.server.port"); - String dbName = env.getProperty("mysql.db.name"); - - String userName = env.getProperty("mysql.user.name"); - String password = env.getProperty("mysql.user.password"); - - DruidDataSource druidDataSource = new DruidDataSource(); - druidDataSource.setUrl( - "jdbc:mysql://" + ip + ":" + port + "/" + dbName + "?serverTimezone=UTC"); - druidDataSource.setUsername(userName); - druidDataSource.setPassword(password); - druidDataSource.setDriverClassName("com.mysql.jdbc.Driver"); - druidDataSource.setInitialSize(0); - druidDataSource.setMaxActive(180); - druidDataSource.setMaxWait(60000); - druidDataSource.setMinIdle(0); - druidDataSource.setValidationQuery("Select 'x' from DUAL"); - druidDataSource.setTestOnBorrow(false); - druidDataSource.setTestOnReturn(false); - druidDataSource.setTestWhileIdle(true); - druidDataSource.setTimeBetweenEvictionRunsMillis(60000); - druidDataSource.setMinEvictableIdleTimeMillis(25200000); - druidDataSource.setRemoveAbandoned(true); - druidDataSource.setRemoveAbandonedTimeout(1800); - druidDataSource.setLogAbandoned(true); - druidDataSource.setFilters("mergeStat"); - return druidDataSource; - } - - @Bean - public DataSourceProxy dataSourceProxy(DruidDataSource druidDataSource) { - return new DataSourceProxy(druidDataSource); - } - - @Bean - public JdbcTemplate jdbcTemplate(DataSourceProxy dataSourceProxy) { - JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSourceProxy); - - jdbcTemplate.execute("TRUNCATE TABLE order_tbl"); - - return jdbcTemplate; - } + return jdbcTemplate; + } } diff --git a/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/application.properties b/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/application.properties index 4be899e9..498c63ed 100644 --- a/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/application.properties @@ -1,9 +1,27 @@ spring.application.name=order-service server.port=18083 -mysql.server.ip=127.0.0.1 -mysql.server.port=3306 -mysql.db.name=demo +spring.datasource.name="orderDataSource" +spring.datasource.type=com.alibaba.druid.pool.DruidDataSource +spring.datasource.driver-class-name=com.mysql.jdbc.Driver +spring.datasource.url=jdbc:mysql://rm-2zetd9474ydd1g5955o.mysql.rds.aliyuncs.com:3306/fescar?useSSL=false&serverTimezone=UTC +spring.datasource.username=workshop +spring.datasource.password=Workshop123 +spring.datasource.druid.max-active=20 +spring.datasource.druid.min-idle=2 +spring.datasource.druid.initial-size=2 -mysql.user.name=root -mysql.user.password=123456 +seata.enabled=true +seata.service.vgroup-mapping=default +seata.service.grouplist=127.0.0.1:8091 +seata.service.disable-global-transaction=false + +## if use registry center +#seata.registry.type=nacos +#seata.registry.nacos.cluster=default +#seata.registry.nacos.server-addr=localhost +# +## if use config center +#seata.config.type=apollo +#seata.config.apollo.apollo-meta=http://192.168.1.204:8801 +#seata.config.apollo.app-id=seata-server diff --git a/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/file.conf b/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/file.conf deleted file mode 100644 index 89f55344..00000000 --- a/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/file.conf +++ /dev/null @@ -1,67 +0,0 @@ -transport { - # tcp udt unix-domain-socket - type = "TCP" - #NIO NATIVE - server = "NIO" - #enable heartbeat - heartbeat = false - #thread factory for netty - thread-factory { - boss-thread-prefix = "NettyBoss" - worker-thread-prefix = "NettyServerNIOWorker" - server-executor-thread-prefix = "NettyServerBizHandler" - share-boss-worker = false - client-selector-thread-prefix = "NettyClientSelector" - client-selector-thread-size = 1 - client-worker-thread-prefix = "NettyClientWorkerThread" - # netty boss thread size,will not be used for UDT - boss-thread-size = 1 - #auto default pin or 8 - worker-thread-size = 8 - } - shutdown { - # when destroy server, wait seconds - wait = 3 - } - serialization = "seata" - compressor = "none" -} -service { - #vgroup->rgroup - vgroup_mapping.order-service-seata-service-group = "default" - #only support single node - default.grouplist = "127.0.0.1:8091" - #degrade current not support - enableDegrade = false - #disable - disable = false - #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent - max.commit.retry.timeout = "-1" - max.rollback.retry.timeout = "-1" -} -client { - async.commit.buffer.limit = 10000 - lock { - retry.internal = 10 - retry.times = 30 - } - report.retry.count = 5 - tm.commit.retry.count = 1 - tm.rollback.retry.count = 1 -} -transaction { - undo.data.validation = true - undo.log.serialization = "jackson" - undo.log.save.days = 7 - #schedule delete expired undo_log in milliseconds - undo.log.delete.period = 86400000 - undo.log.table = "undo_log" -} - -support { - ## spring - spring { - # auto proxy the DataSource bean - datasource.autoproxy = false - } -} \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/registry.conf b/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/registry.conf deleted file mode 100644 index b98f5704..00000000 --- a/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/registry.conf +++ /dev/null @@ -1,73 +0,0 @@ -registry { - # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa - type = "file" - - nacos { - serverAddr = "localhost" - namespace = "" - cluster = "default" - } - eureka { - serviceUrl = "http://localhost:8761/eureka" - application = "default" - weight = "1" - } - redis { - serverAddr = "localhost:6379" - db = "0" - } - zk { - cluster = "default" - serverAddr = "127.0.0.1:2181" - session.timeout = 6000 - connect.timeout = 2000 - } - consul { - cluster = "default" - serverAddr = "127.0.0.1:8500" - } - etcd3 { - cluster = "default" - serverAddr = "http://localhost:2379" - } - sofa { - serverAddr = "127.0.0.1:9603" - application = "default" - region = "DEFAULT_ZONE" - datacenter = "DefaultDataCenter" - cluster = "default" - group = "SEATA_GROUP" - addressWaitTime = "3000" - } - file { - name = "file.conf" - } -} - -config { - # file、nacos 、apollo、zk、consul、etcd3 - type = "file" - - nacos { - serverAddr = "localhost" - namespace = "" - } - consul { - serverAddr = "127.0.0.1:8500" - } - apollo { - app.id = "seata-server" - apollo.meta = "http://192.168.1.204:8801" - } - zk { - serverAddr = "127.0.0.1:2181" - session.timeout = 6000 - connect.timeout = 2000 - } - etcd3 { - serverAddr = "http://localhost:2379" - } - file { - name = "file.conf" - } -} diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml b/spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml index a2c8f3ee..b5c3f3bb 100644 --- a/spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml +++ b/spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml @@ -24,19 +24,24 @@ org.springframework.boot spring-boot-starter-actuator - - com.alibaba - druid - 1.1.10 - org.springframework.boot spring-boot-starter-jdbc - + + com.alibaba + druid-spring-boot-starter + 1.1.10 + mysql mysql-connector-java + 5.1.31 + + + log4j + log4j + 1.2.17 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/DatabaseConfiguration.java b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/DatabaseConfiguration.java index dc8d9a85..b08363c1 100644 --- a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/DatabaseConfiguration.java +++ b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/DatabaseConfiguration.java @@ -16,15 +16,14 @@ package com.alibaba.cloud.examples; -import java.sql.SQLException; +import javax.sql.DataSource; import com.alibaba.druid.pool.DruidDataSource; -import io.seata.rm.datasource.DataSourceProxy; -import org.springframework.context.ApplicationContext; +import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.Environment; +import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate; /** @@ -33,63 +32,23 @@ import org.springframework.jdbc.core.JdbcTemplate; @Configuration public class DatabaseConfiguration { - private final ApplicationContext applicationContext; + @Bean + @Primary + @ConfigurationProperties("spring.datasource") + public DataSource storageDataSource() { + return new DruidDataSource(); + } - public DatabaseConfiguration(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; - } + @Bean + public JdbcTemplate jdbcTemplate(DataSource dataSource) { - @Bean(initMethod = "init", destroyMethod = "close") - public DruidDataSource storageDataSource() throws SQLException { + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); - Environment environment = applicationContext.getEnvironment(); + jdbcTemplate.update("delete from storage_tbl where commodity_code = 'C00321'"); + jdbcTemplate.update("insert into storage_tbl(commodity_code, count) values ('C00321', 100)"); - String ip = environment.getProperty("mysql.server.ip"); - String port = environment.getProperty("mysql.server.port"); - String dbName = environment.getProperty("mysql.db.name"); + return jdbcTemplate; - String userName = environment.getProperty("mysql.user.name"); - String password = environment.getProperty("mysql.user.password"); - - DruidDataSource druidDataSource = new DruidDataSource(); - druidDataSource.setUrl( - "jdbc:mysql://" + ip + ":" + port + "/" + dbName + "?serverTimezone=UTC"); - druidDataSource.setUsername(userName); - druidDataSource.setPassword(password); - druidDataSource.setDriverClassName("com.mysql.jdbc.Driver"); - druidDataSource.setInitialSize(0); - druidDataSource.setMaxActive(180); - druidDataSource.setMaxWait(60000); - druidDataSource.setMinIdle(0); - druidDataSource.setValidationQuery("Select 'x' from DUAL"); - druidDataSource.setTestOnBorrow(false); - druidDataSource.setTestOnReturn(false); - druidDataSource.setTestWhileIdle(true); - druidDataSource.setTimeBetweenEvictionRunsMillis(60000); - druidDataSource.setMinEvictableIdleTimeMillis(25200000); - druidDataSource.setRemoveAbandoned(true); - druidDataSource.setRemoveAbandonedTimeout(1800); - druidDataSource.setLogAbandoned(true); - druidDataSource.setFilters("mergeStat"); - return druidDataSource; - } - - @Bean - public DataSourceProxy dataSourceProxy(DruidDataSource druidDataSource) { - return new DataSourceProxy(druidDataSource); - } - - @Bean - public JdbcTemplate jdbcTemplate(DataSourceProxy dataSourceProxy) { - - JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSourceProxy); - - jdbcTemplate.update("delete from storage_tbl where commodity_code = 'C00321'"); - jdbcTemplate.update( - "insert into storage_tbl(commodity_code, count) values ('C00321', 100)"); - - return jdbcTemplate; - - } + } } diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/application.properties b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/application.properties index 08866ced..92233b7f 100644 --- a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/application.properties @@ -1,9 +1,28 @@ spring.application.name=storage-service server.port=18082 -mysql.server.ip=127.0.0.1 -mysql.server.port=3306 -mysql.db.name=demo +spring.datasource.name="storageDataSource" +spring.datasource.type=com.alibaba.druid.pool.DruidDataSource +spring.datasource.driver-class-name=com.mysql.jdbc.Driver +spring.datasource.url=jdbc:mysql://rm-2zetd9474ydd1g5955o.mysql.rds.aliyuncs.com:3306/fescar?useSSL=false&serverTimezone=UTC +spring.datasource.username=workshop +spring.datasource.password=Workshop123 +spring.datasource.druid.max-active=20 +spring.datasource.druid.min-idle=2 +spring.datasource.druid.initial-size=2 -mysql.user.name=root -mysql.user.password=123456 + +seata.enabled=true +seata.service.vgroup-mapping=default +seata.service.grouplist=127.0.0.1:8091 +seata.service.disable-global-transaction=false + +## if use registry center +#seata.registry.type=nacos +#seata.registry.nacos.cluster=default +#seata.registry.nacos.server-addr=localhost +# +## if use config center +#seata.config.type=apollo +#seata.config.apollo.apollo-meta=http://192.168.1.204:8801 +#seata.config.apollo.app-id=seata-server diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/file.conf b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/file.conf deleted file mode 100644 index 2915b99c..00000000 --- a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/file.conf +++ /dev/null @@ -1,67 +0,0 @@ -transport { - # tcp udt unix-domain-socket - type = "TCP" - #NIO NATIVE - server = "NIO" - #enable heartbeat - heartbeat = false - #thread factory for netty - thread-factory { - boss-thread-prefix = "NettyBoss" - worker-thread-prefix = "NettyServerNIOWorker" - server-executor-thread-prefix = "NettyServerBizHandler" - share-boss-worker = false - client-selector-thread-prefix = "NettyClientSelector" - client-selector-thread-size = 1 - client-worker-thread-prefix = "NettyClientWorkerThread" - # netty boss thread size,will not be used for UDT - boss-thread-size = 1 - #auto default pin or 8 - worker-thread-size = 8 - } - shutdown { - # when destroy server, wait seconds - wait = 3 - } - serialization = "seata" - compressor = "none" -} -service { - #vgroup->rgroup - vgroup_mapping.storage-service-seata-service-group = "default" - #only support single node - default.grouplist = "127.0.0.1:8091" - #degrade current not support - enableDegrade = false - #disable - disable = false - #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent - max.commit.retry.timeout = "-1" - max.rollback.retry.timeout = "-1" -} -client { - async.commit.buffer.limit = 10000 - lock { - retry.internal = 10 - retry.times = 30 - } - report.retry.count = 5 - tm.commit.retry.count = 1 - tm.rollback.retry.count = 1 -} -transaction { - undo.data.validation = true - undo.log.serialization = "jackson" - undo.log.save.days = 7 - #schedule delete expired undo_log in milliseconds - undo.log.delete.period = 86400000 - undo.log.table = "undo_log" -} - -support { - ## spring - spring { - # auto proxy the DataSource bean - datasource.autoproxy = false - } -} \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/registry.conf b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/registry.conf deleted file mode 100644 index b98f5704..00000000 --- a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/registry.conf +++ /dev/null @@ -1,73 +0,0 @@ -registry { - # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa - type = "file" - - nacos { - serverAddr = "localhost" - namespace = "" - cluster = "default" - } - eureka { - serviceUrl = "http://localhost:8761/eureka" - application = "default" - weight = "1" - } - redis { - serverAddr = "localhost:6379" - db = "0" - } - zk { - cluster = "default" - serverAddr = "127.0.0.1:2181" - session.timeout = 6000 - connect.timeout = 2000 - } - consul { - cluster = "default" - serverAddr = "127.0.0.1:8500" - } - etcd3 { - cluster = "default" - serverAddr = "http://localhost:2379" - } - sofa { - serverAddr = "127.0.0.1:9603" - application = "default" - region = "DEFAULT_ZONE" - datacenter = "DefaultDataCenter" - cluster = "default" - group = "SEATA_GROUP" - addressWaitTime = "3000" - } - file { - name = "file.conf" - } -} - -config { - # file、nacos 、apollo、zk、consul、etcd3 - type = "file" - - nacos { - serverAddr = "localhost" - namespace = "" - } - consul { - serverAddr = "127.0.0.1:8500" - } - apollo { - app.id = "seata-server" - apollo.meta = "http://192.168.1.204:8801" - } - zk { - serverAddr = "127.0.0.1:2181" - session.timeout = 6000 - connect.timeout = 2000 - } - etcd3 { - serverAddr = "http://localhost:2379" - } - file { - name = "file.conf" - } -} diff --git a/spring-cloud-alibaba-seata/pom.xml b/spring-cloud-alibaba-seata/pom.xml index 216008cc..a48df0a1 100644 --- a/spring-cloud-alibaba-seata/pom.xml +++ b/spring-cloud-alibaba-seata/pom.xml @@ -16,7 +16,7 @@ io.seata - seata-all + seata-spring-boot-starter diff --git a/spring-cloud-alibaba-seata/src/main/java/com/alibaba/cloud/seata/GlobalTransactionAutoConfiguration.java b/spring-cloud-alibaba-seata/src/main/java/com/alibaba/cloud/seata/GlobalTransactionAutoConfiguration.java deleted file mode 100644 index b7aaa7a0..00000000 --- a/spring-cloud-alibaba-seata/src/main/java/com/alibaba/cloud/seata/GlobalTransactionAutoConfiguration.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2013-2018 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.alibaba.cloud.seata; - -import io.seata.spring.annotation.GlobalTransactionScanner; - -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.util.StringUtils; - -/** - * @author xiaojing - */ - -@Configuration -@EnableConfigurationProperties(SeataProperties.class) -public class GlobalTransactionAutoConfiguration { - - private final ApplicationContext applicationContext; - - private final SeataProperties seataProperties; - - public GlobalTransactionAutoConfiguration(ApplicationContext applicationContext, - SeataProperties seataProperties) { - this.applicationContext = applicationContext; - this.seataProperties = seataProperties; - } - - @Bean - public GlobalTransactionScanner globalTransactionScanner() { - - String applicationName = applicationContext.getEnvironment() - .getProperty("spring.application.name"); - - String txServiceGroup = seataProperties.getTxServiceGroup(); - - if (StringUtils.isEmpty(txServiceGroup)) { - txServiceGroup = applicationName + "-seata-service-group"; - seataProperties.setTxServiceGroup(txServiceGroup); - } - - return new GlobalTransactionScanner(applicationName, txServiceGroup); - } - -} diff --git a/spring-cloud-alibaba-seata/src/main/java/com/alibaba/cloud/seata/SeataProperties.java b/spring-cloud-alibaba-seata/src/main/java/com/alibaba/cloud/seata/SeataProperties.java deleted file mode 100644 index 6c369ac6..00000000 --- a/spring-cloud-alibaba-seata/src/main/java/com/alibaba/cloud/seata/SeataProperties.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2013-2018 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.alibaba.cloud.seata; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -/** - * @author xiaojing - */ -@ConfigurationProperties("spring.cloud.alibaba.seata") -public class SeataProperties { - - // todo support config Seata server information - - /** - * Seata tx service group.default is ${spring.application.name}-seata-service-group. - */ - private String txServiceGroup; - - public String getTxServiceGroup() { - return txServiceGroup; - } - - public void setTxServiceGroup(String txServiceGroup) { - this.txServiceGroup = txServiceGroup; - } - -} diff --git a/spring-cloud-alibaba-seata/src/main/resources/META-INF/spring.factories b/spring-cloud-alibaba-seata/src/main/resources/META-INF/spring.factories index ae993684..54b578ae 100644 --- a/spring-cloud-alibaba-seata/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-alibaba-seata/src/main/resources/META-INF/spring.factories @@ -1,7 +1,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.alibaba.cloud.seata.rest.SeataRestTemplateAutoConfiguration,\ com.alibaba.cloud.seata.web.SeataHandlerInterceptorConfiguration,\ -com.alibaba.cloud.seata.GlobalTransactionAutoConfiguration,\ com.alibaba.cloud.seata.feign.SeataFeignClientAutoConfiguration,\ com.alibaba.cloud.seata.feign.hystrix.SeataHystrixAutoConfiguration