diff --git a/pom.xml b/pom.xml index 0b711745d..0aefb93dd 100644 --- a/pom.xml +++ b/pom.xml @@ -180,11 +180,11 @@ - - - - - + + + + + org.apache.rocketmq rocketmq-client @@ -246,7 +246,9 @@ - ${session.executionRootDirectory}/eclipse/checkstyle-suppressions.xml + + ${session.executionRootDirectory}/eclipse/checkstyle-suppressions.xml + true true true diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index 157fc502c..e2c2fc14b 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -31,7 +31,9 @@ nacos-example/nacos-gateway-example seata-example/business-service seata-example/order-service - seata-example/storage-service + seata-example/seata-server + seata-example/storage-service-api + seata-example/storage-service-provider seata-example/account-service rocketmq-example/rocketmq-consume-example rocketmq-example/rocketmq-produce-example 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 3f54a457f..ddd26fb8d 100644 --- a/spring-cloud-alibaba-examples/seata-example/account-service/pom.xml +++ b/spring-cloud-alibaba-examples/seata-example/account-service/pom.xml @@ -44,12 +44,10 @@ mysql mysql-connector-java - 8.0.16 log4j log4j - 1.2.17 - \ No newline at end of file + 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 deleted file mode 100644 index d5f73adfe..000000000 --- a/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/application.properties +++ /dev/null @@ -1,30 +0,0 @@ -spring.application.name=account-service -server.port=18084 -spring.cloud.nacos.discovery.server-addr=localhost:8848 - -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://xxx:3306/seata?useSSL=false&serverTimezone=UTC -spring.datasource.username=xxx -spring.datasource.password=xxx -spring.datasource.druid.max-active=20 -spring.datasource.druid.min-idle=2 -spring.datasource.druid.initial-size=2 - -seata.enabled=true -spring.cloud.alibaba.seata.tx-service-group=account-service -seata.service.vgroup-mapping.account-service=default -seata.service.grouplist.default=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/account-service/src/main/resources/application.yml b/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/application.yml new file mode 100644 index 000000000..af7932cd6 --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/account-service/src/main/resources/application.yml @@ -0,0 +1,63 @@ +base: + config: + mdb: + hostname: 127.0.0.1 + dbname: seata + port: 3306 + username: root + password: 123456 + +server: + port: 18084 + +spring: + cloud: + nacos: + discovery: + server-addr: 127.0.0.1:8848 + application: + name: account-service + main: + allow-bean-definition-overriding: true + datasource: + name: storageDataSource + type: com.alibaba.druid.pool.DruidDataSource + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://${base.config.mdb.hostname}:${base.config.mdb.port}/${base.config.mdb.dbname}?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull + username: ${base.config.mdb.username} + password: ${base.config.mdb.password} + druid: + max-active: 20 + min-idle: 2 + initial-size: 2 + +seata: + enabled: true + application-id: ${spring.application.name} + + tx-service-group: ${spring.application.name}-tx-group + config: + type: nacos + + nacos: + namespace: a3f588d3-7be9-43d3-af7c-1e7919cf8af6 + serverAddr: 127.0.0.1:8848 + + group: SEATA_GROUP + dataId: "seata.properties" + username: "nacos" + password: "nacos" + registry: + type: nacos + nacos: + application: seata-server + server-addr: 127.0.0.1:8848 + group: SEATA_GROUP + namespace: a3f588d3-7be9-43d3-af7c-1e7919cf8af6 + username: "nacos" + password: "nacos" +dubbo: + registry: + address: spring-cloud://localhost + cloud: + subscribed-services: 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 0ae95db73..50a189b2a 100644 --- a/spring-cloud-alibaba-examples/seata-example/business-service/pom.xml +++ b/spring-cloud-alibaba-examples/seata-example/business-service/pom.xml @@ -14,6 +14,11 @@ jar + + + com.alibaba.cloud + spring-cloud-starter-dubbo + com.alibaba.cloud spring-cloud-starter-alibaba-seata @@ -46,6 +51,16 @@ com.alibaba.cloud spring-cloud-starter-alibaba-sentinel + + org.springframework + spring-tx + + + com.alibaba.cloud + storage-service-api + 2.2.8-SNAPSHOT + compile + - \ No newline at end of file + diff --git a/spring-cloud-alibaba-examples/seata-example/business-service/src/main/java/com/alibaba/cloud/examples/BusinessApplication.java b/spring-cloud-alibaba-examples/seata-example/business-service/src/main/java/com/alibaba/cloud/examples/BusinessApplication.java index e708c009f..bdabf5042 100644 --- a/spring-cloud-alibaba-examples/seata-example/business-service/src/main/java/com/alibaba/cloud/examples/BusinessApplication.java +++ b/spring-cloud-alibaba-examples/seata-example/business-service/src/main/java/com/alibaba/cloud/examples/BusinessApplication.java @@ -33,7 +33,7 @@ import org.springframework.web.client.RestTemplate; */ @SpringBootApplication @EnableFeignClients -@EnableDiscoveryClient(autoRegister = false) +@EnableDiscoveryClient(autoRegister = true) public class BusinessApplication { public static void main(String[] args) { 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 ec93cd4d4..6d0a7e9c6 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 @@ -18,7 +18,9 @@ package com.alibaba.cloud.examples; import com.alibaba.cloud.examples.BusinessApplication.OrderService; import com.alibaba.cloud.examples.BusinessApplication.StorageService; +import com.alibaba.cloud.examples.api.StorageDubboService; import io.seata.spring.annotation.GlobalTransactional; +import org.apache.dubbo.config.annotation.DubboReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,6 +58,9 @@ public class HomeController { private final StorageService storageService; + @DubboReference + private StorageDubboService storageDubboService; + public HomeController(RestTemplate restTemplate, OrderService orderService, StorageService storageService) { this.restTemplate = restTemplate; @@ -122,4 +127,24 @@ public class HomeController { } + @GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx") + @GetMapping(value = "/seata/dubbo", produces = "application/json") + public String dubbo() { + + String result = storageDubboService.updateStorage(COMMODITY_CODE, ORDER_COUNT); + + if (!SUCCESS.equals(result)) { + throw new RuntimeException(); + } + + result = orderService.order(USER_ID, COMMODITY_CODE, ORDER_COUNT); + + if (!SUCCESS.equals(result)) { + throw new RuntimeException(); + } + + return SUCCESS; + + } + } 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 deleted file mode 100644 index bc40bece3..000000000 --- a/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/application.properties +++ /dev/null @@ -1,29 +0,0 @@ -server.port=18081 -spring.application.name=business-service -spring.cloud.nacos.discovery.server-addr=localhost:8848 -# The following configuration can be omitted. - -#feign.hystrix.enabled=true -#feign.sentinel.enabled=true -feign.client.config.default.connectTimeout=10000 -feign.client.config.default.readTimeout=10000 - -logging.level.io.seata=debug - -seata.enabled=true -spring.cloud.alibaba.seata.tx-service-group=business-service -seata.service.vgroup-mapping.business-service=default -seata.service.grouplist.default=127.0.0.1:8091 -seata.service.disable-global-transaction=false - -spring.cloud.loadbalancer.ribbon.enabled=true - -## 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/business-service/src/main/resources/application.yml b/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/application.yml new file mode 100644 index 000000000..cc8617b6d --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/business-service/src/main/resources/application.yml @@ -0,0 +1,58 @@ +server: + port: 18081 + +spring: + cloud: + nacos: + discovery: + server-addr: 127.0.0.1:8848 + loadbalancer: + ribbon: + enabled:true + application: + name: business-service + +seata: + enabled: true + application-id: ${spring.application.name} + + tx-service-group: ${spring.application.name}-tx-group + config: + type: nacos + nacos: + namespace: a3f588d3-7be9-43d3-af7c-1e7919cf8af6 + serverAddr: 127.0.0.1:8848 + group: SEATA_GROUP + dataId: "seata.properties" + username: "nacos" + password: "nacos" + registry: + type: nacos + nacos: + application: seata-server + server-addr: 127.0.0.1:8848 + group: SEATA_GROUP + namespace: a3f588d3-7be9-43d3-af7c-1e7919cf8af6 + username: "nacos" + password: "nacos" +dubbo: + registry: + address: spring-cloud://localhost + protocol: + name: dubbo + port: -1 + provider: + telnet: ls,ps,cd,pwd,trace,count,invoke,select,status,log,help,clear,exit,shutdown + cloud: + subscribed-services: storage-service +feign: + client: + config: + default: + connectTimeout:10000 + readTimeout:10000 +logging: + level: + io: + seata: debug + 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 7fbd38baa..3f1d29fc0 100644 --- a/spring-cloud-alibaba-examples/seata-example/order-service/pom.xml +++ b/spring-cloud-alibaba-examples/seata-example/order-service/pom.xml @@ -43,14 +43,12 @@ mysql mysql-connector-java - 8.0.16 log4j log4j - 1.2.17 - + 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 deleted file mode 100644 index fb4a4b7c0..000000000 --- a/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/application.properties +++ /dev/null @@ -1,29 +0,0 @@ -spring.application.name=order-service -server.port=18083 -spring.cloud.nacos.discovery.server-addr=localhost:8848 - -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://xxx:3306/seata?useSSL=false&serverTimezone=UTC -spring.datasource.username=xxx -spring.datasource.password=xxx -spring.datasource.druid.max-active=20 -spring.datasource.druid.min-idle=2 -spring.datasource.druid.initial-size=2 - -seata.enabled=true -spring.cloud.alibaba.seata.tx-service-group=business-service -seata.service.vgroup-mapping.business-service=default -seata.service.grouplist.default=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/application.yml b/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/application.yml new file mode 100644 index 000000000..48be81617 --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/order-service/src/main/resources/application.yml @@ -0,0 +1,60 @@ +base: + config: + mdb: + hostname: 127.0.0.1 + dbname: seata + port: 3306 + username: root + password: 123456 + +server: + port: 18083 + +spring: + cloud: + nacos: + discovery: + server-addr: 127.0.0.1:8848 + application: + name: order-service + main: + allow-bean-definition-overriding: true + datasource: + name: storageDataSource + type: com.alibaba.druid.pool.DruidDataSource + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://${base.config.mdb.hostname}:${base.config.mdb.port}/${base.config.mdb.dbname}?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull + username: ${base.config.mdb.username} + password: ${base.config.mdb.password} + druid: + max-active: 20 + min-idle: 2 + initial-size: 2 + +seata: + enabled: true + application-id: ${spring.application.name} + tx-service-group: ${spring.application.name}-tx-group + config: + type: nacos + nacos: + namespace: a3f588d3-7be9-43d3-af7c-1e7919cf8af6 + serverAddr: 127.0.0.1:8848 + group: SEATA_GROUP + dataId: "seata.properties" + username: "nacos" + password: "nacos" + registry: + type: nacos + nacos: + application: seata-server + server-addr: 127.0.0.1:8848 + group: SEATA_GROUP + namespace: a3f588d3-7be9-43d3-af7c-1e7919cf8af6 + username: "nacos" + password: "nacos" +dubbo: + registry: + address: spring-cloud://localhost + cloud: + subscribed-services: diff --git a/spring-cloud-alibaba-examples/seata-example/readme-zh.md b/spring-cloud-alibaba-examples/seata-example/readme-zh.md index 2723de586..ba4007d86 100644 --- a/spring-cloud-alibaba-examples/seata-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/seata-example/readme-zh.md @@ -19,6 +19,9 @@ 1. 创建 示例中 业务所需要的数据库表 +1. 导入 Nacos 配置 +[https://github.com/seata/seata/blob/1.5.0/script/config-center/config.txt] + 1. 启动 Seata Server @@ -28,7 +31,7 @@ **注意**: 实际上,Seata 支持不同的应用使用完全不相干的数据库,但是这里为了简单地演示一个原理,所以我们选择了只使用一个数据库。 -将 `account-server`、`order-service`、`storage-service` 这三个应用中的 resources 目录下的 `application.properties` 文件中的如下配置修改成你运行环境中的实际配置。 +将 `account-server`、`order-service`、`storage-service` 这三个应用中的 resources 目录下的 `application.yml` 文件中的如下配置修改成你运行环境中的实际配置。 ``` mysql.server.ip=your mysql server ip address @@ -60,7 +63,83 @@ CREATE TABLE `undo_log` ( UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; ``` - +### 创建 seata-server 所需要的数据库表 +点击查看:https://github.com/seata/seata/blob/1.5.0/script/server/db/mysql.sql +```$sql +-- -------------------------------- The script used when storeMode is 'db' -------------------------------- +-- the table to store GlobalSession data +CREATE TABLE IF NOT EXISTS `global_table` +( + `xid` VARCHAR(128) NOT NULL, + `transaction_id` BIGINT, + `status` TINYINT NOT NULL, + `application_id` VARCHAR(32), + `transaction_service_group` VARCHAR(32), + `transaction_name` VARCHAR(128), + `timeout` INT, + `begin_time` BIGINT, + `application_data` VARCHAR(2000), + `gmt_create` DATETIME, + `gmt_modified` DATETIME, + PRIMARY KEY (`xid`), + KEY `idx_status_gmt_modified` (`status` , `gmt_modified`), + KEY `idx_transaction_id` (`transaction_id`) +) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4; + +-- the table to store BranchSession data +CREATE TABLE IF NOT EXISTS `branch_table` +( + `branch_id` BIGINT NOT NULL, + `xid` VARCHAR(128) NOT NULL, + `transaction_id` BIGINT, + `resource_group_id` VARCHAR(32), + `resource_id` VARCHAR(256), + `branch_type` VARCHAR(8), + `status` TINYINT, + `client_id` VARCHAR(64), + `application_data` VARCHAR(2000), + `gmt_create` DATETIME(6), + `gmt_modified` DATETIME(6), + PRIMARY KEY (`branch_id`), + KEY `idx_xid` (`xid`) +) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4; + +-- the table to store lock data +CREATE TABLE IF NOT EXISTS `lock_table` +( + `row_key` VARCHAR(128) NOT NULL, + `xid` VARCHAR(128), + `transaction_id` BIGINT, + `branch_id` BIGINT NOT NULL, + `resource_id` VARCHAR(256), + `table_name` VARCHAR(32), + `pk` VARCHAR(36), + `status` TINYINT NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking', + `gmt_create` DATETIME, + `gmt_modified` DATETIME, + PRIMARY KEY (`row_key`), + KEY `idx_status` (`status`), + KEY `idx_branch_id` (`branch_id`), + KEY `idx_xid_and_branch_id` (`xid` , `branch_id`) +) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4; + +CREATE TABLE IF NOT EXISTS `distributed_lock` +( + `lock_key` CHAR(20) NOT NULL, + `lock_value` VARCHAR(20) NOT NULL, + `expire` BIGINT, + primary key (`lock_key`) +) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4; + +INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0); +INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0); +INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0); +INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0); +``` ### 创建 示例中 业务所需要的数据库表 ```$sql @@ -96,9 +175,10 @@ CREATE TABLE `account_tbl` ( ### 启动 Seata Server -点击这个页面 [https://github.com/seata/seata/releases](https://github.com/seata/seata/releases),下载最新版本的 Seata Server 端. - +1.运行 seata-server 启动Seata server +示例中采用nacos 作为配置,注册中心 存储模式为:db 采用mysql +2.或点击这个页面 [https://github.com/seata/seata/releases](https://github.com/seata/seata/releases),下载最新版本的 Seata Server 端. 进入解压之后的 bin 目录,执行如下命令来启动, 所有启动参数为可选项。 ```$shell @@ -109,7 +189,7 @@ sh seata-server.sh -p $LISTEN_PORT -m $MODE(file or db) -h $HOST -e $ENV lock_table](https://github.com/seata/seata/blob/develop/server/src/main/resources/db_store.sql) -h 用于解决seata-server和业务侧跨网络问题,其配置的host值直接显示到注册中心的服务可用地址host,当跨网络时这里需要配置为公网IP或NATIP,若都在同一局域网则无需配置 -e 用于解决多环境配置中心隔离问题 -在这个示例中,采用如下命令来启动 Seata Server +采用如下命令来启动 Seata Server ```$shell sh seata-server.sh -p 8091 -m file @@ -122,12 +202,14 @@ sh seata-server.sh -p 8091 -m file 分别运行 `account-server`、`order-service`、`storage-service` 和 `business-service` 这三个应用的 Main 函数,启动示例。 -启动示例后,通过 HTTP 的 GET 方法访问如下两个 URL,可以分别验证在 `business-service` 中 通过 RestTemplate 和 FeignClient 调用其他服务的场景。 +启动示例后,通过 HTTP 的 GET 方法访问如下 URL,可以分别验证在 `business-service` 中 通过 RestTemplate 和 FeignClient 及Dubbo调用其他服务的场景。 ```$xslt http://127.0.0.1:18081/seata/feign http://127.0.0.1:18081/seata/rest + +http://127.0.0.1:18081/seata/dubbo ``` ## 如何验证分布式事务成功? diff --git a/spring-cloud-alibaba-examples/seata-example/seata-server/pom.xml b/spring-cloud-alibaba-examples/seata-example/seata-server/pom.xml new file mode 100644 index 000000000..f92ea2d3d --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/seata-server/pom.xml @@ -0,0 +1,24 @@ + + + + spring-cloud-alibaba-examples + com.alibaba.cloud + ${revision} + ../../pom.xml + + 4.0.0 + seata-server + Spring Cloud Starter Alibaba Seata Example - Seata Server + jar + + + + io.seata + seata-server + 1.5.0 + + + + diff --git a/spring-cloud-alibaba-examples/seata-example/seata-server/src/main/java/com/alibaba/cloud/examples/seata/SeataServerApplication.java b/spring-cloud-alibaba-examples/seata-example/seata-server/src/main/java/com/alibaba/cloud/examples/seata/SeataServerApplication.java new file mode 100644 index 000000000..45b31f0a7 --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/seata-server/src/main/java/com/alibaba/cloud/examples/seata/SeataServerApplication.java @@ -0,0 +1,32 @@ +/* + * 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.examples.seata; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author ZHANGQIAN + */ +@SpringBootApplication(scanBasePackages = { "io.seata" }) +public class SeataServerApplication { + + public static void main(String[] args) { + SpringApplication.run(io.seata.server.ServerApplication.class, args); + } + +} diff --git a/spring-cloud-alibaba-examples/seata-example/seata-server/src/main/resources/application.yml b/spring-cloud-alibaba-examples/seata-example/seata-server/src/main/resources/application.yml new file mode 100644 index 000000000..32448c2ca --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/seata-server/src/main/resources/application.yml @@ -0,0 +1,85 @@ +server: + port: 7091 + +spring: + application: + name: seata-server + +logging: + config: classpath:logback-spring.xml + file: + path: ${user.home}/logs/seata + extend: + logstash-appender: + destination: 127.0.0.1:4560 + kafka-appender: + bootstrap-servers: 127.0.0.1:9092 + topic: logback_to_logstash + +console: + user: + username: seata + password: seata + +seata: + config: + # support: nacos 、 consul 、 apollo 、 zk 、 etcd3 + type: nacos + nacos: + server-addr: 127.0.0.1:8848 + namespace: a3f588d3-7be9-43d3-af7c-1e7919cf8af6 + group: SEATA_GROUP + username: nacos + password: nacos + ##if use MSE Nacos with auth, mutex with username/password attribute + #access-key: "" + #secret-key: "" + data-id: seata.properties + registry: + # support: nacos 、 eureka 、 redis 、 zk 、 consul 、 etcd3 、 sofa + type: nacos + preferred-networks: 30.240.* + nacos: + application: seata-server + server-addr: 127.0.0.1:8848 + group: SEATA_GROUP + namespace: a3f588d3-7be9-43d3-af7c-1e7919cf8af6 + cluster: default + username: nacos + password: nacos + store: + # support: file 、 db 、 redis + mode: db + session: + mode: file + lock: + mode: file + file: + dir: sessionStore + max-branch-session-size: 16384 + max-global-session-size: 512 + file-write-buffer-cache-size: 16384 + session-reload-read-size: 100 + flush-disk-mode: async + db: + datasource: druid + db-type: mysql + driver-class-name: com.mysql.jdbc.Driver + url: jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true + user: root + password: 123456 + min-conn: 5 + max-conn: 100 + global-table: global_table + branch-table: branch_table + lock-table: lock_table + distributed-lock-table: distributed_lock + query-limit: 100 + max-wait: 5000 +# server: +# service-port: 8091 #If not configured, the default is '${server.port} + 1000' + security: + secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017 + tokenValidityInMilliseconds: 1800000 + ignore: + urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service-api/pom.xml b/spring-cloud-alibaba-examples/seata-example/storage-service-api/pom.xml new file mode 100644 index 000000000..d11f0c2d1 --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/storage-service-api/pom.xml @@ -0,0 +1,26 @@ + + + + spring-cloud-alibaba-examples + com.alibaba.cloud + ${revision} + ../../pom.xml + + 4.0.0 + + com.alibaba.cloud + storage-service-api + ${revision} + + + org.apache.dubbo + dubbo + + + Spring Cloud Alibaba Seata Examples + + + + diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service-api/src/main/java/com/alibaba/cloud/examples/api/StorageDubboService.java b/spring-cloud-alibaba-examples/seata-example/storage-service-api/src/main/java/com/alibaba/cloud/examples/api/StorageDubboService.java new file mode 100644 index 000000000..acddc42a8 --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/storage-service-api/src/main/java/com/alibaba/cloud/examples/api/StorageDubboService.java @@ -0,0 +1,27 @@ +/* + * 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.examples.api; + +/** + * @author zhangqian9158@gmail.com + */ + +public interface StorageDubboService { + + String updateStorage(String commodityCode, int count); + +} diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml b/spring-cloud-alibaba-examples/seata-example/storage-service-provider/pom.xml similarity index 80% rename from spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml rename to spring-cloud-alibaba-examples/seata-example/storage-service-provider/pom.xml index 2f98ad9aa..12b1b4601 100644 --- a/spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml +++ b/spring-cloud-alibaba-examples/seata-example/storage-service-provider/pom.xml @@ -9,7 +9,7 @@ ../../pom.xml 4.0.0 - storage-service + storage-service-provider Spring Cloud Starter Alibaba Seata Example - Storage Service jar @@ -22,6 +22,11 @@ com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery + + + com.alibaba.cloud + spring-cloud-starter-dubbo + org.springframework.boot spring-boot-starter-web @@ -42,12 +47,17 @@ mysql mysql-connector-java - 8.0.16 log4j log4j 1.2.17 + + com.alibaba.cloud + storage-service-api + 2.2.8-SNAPSHOT + compile + - \ No newline at end of file + diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/StorageApplication.java b/spring-cloud-alibaba-examples/seata-example/storage-service-provider/src/main/java/com/alibaba/cloud/examples/StorageApplication.java similarity index 91% rename from spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/StorageApplication.java rename to spring-cloud-alibaba-examples/seata-example/storage-service-provider/src/main/java/com/alibaba/cloud/examples/StorageApplication.java index 91e6c46cd..445336541 100644 --- a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/StorageApplication.java +++ b/spring-cloud-alibaba-examples/seata-example/storage-service-provider/src/main/java/com/alibaba/cloud/examples/StorageApplication.java @@ -16,6 +16,8 @@ package com.alibaba.cloud.examples; +import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -23,9 +25,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * @author xiaojing */ @SpringBootApplication +@EnableDubbo public class StorageApplication { public static void main(String[] args) { + SpringApplication.run(StorageApplication.class, args); } diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service-provider/src/main/java/com/alibaba/cloud/examples/api/impl/StorageDubboServiceImpl.java b/spring-cloud-alibaba-examples/seata-example/storage-service-provider/src/main/java/com/alibaba/cloud/examples/api/impl/StorageDubboServiceImpl.java new file mode 100644 index 000000000..8336fd85c --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/storage-service-provider/src/main/java/com/alibaba/cloud/examples/api/impl/StorageDubboServiceImpl.java @@ -0,0 +1,62 @@ +/* + * 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.examples.api.impl; + +import com.alibaba.cloud.examples.api.StorageDubboService; +import io.seata.core.context.RootContext; +import org.apache.dubbo.config.annotation.DubboService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.transaction.annotation.Transactional; + +/** + * @author ZHANGQIAN + */ + +@DubboService +public class StorageDubboServiceImpl implements StorageDubboService { + + private static final Logger LOGGER = LoggerFactory + .getLogger(StorageDubboServiceImpl.class); + + private static final String SUCCESS = "SUCCESS"; + + private static final String FAIL = "FAIL"; + + private final JdbcTemplate jdbcTemplate; + + public StorageDubboServiceImpl(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + @Override + @Transactional + public String updateStorage(String commodityCode, int count) { + LOGGER.info("Dubbo Storage Service Begin ... xid: " + RootContext.getXID()); + int result = jdbcTemplate.update( + "update storage_tbl set count = count - ? where commodity_code = ?", + new Object[] { count, commodityCode }); + LOGGER.info("Storage Service End ... "); + if (result == 1) { + return SUCCESS; + } + return FAIL; + } + +} 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-provider/src/main/java/com/alibaba/cloud/examples/config/DatabaseConfiguration.java similarity index 97% rename from spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/DatabaseConfiguration.java rename to spring-cloud-alibaba-examples/seata-example/storage-service-provider/src/main/java/com/alibaba/cloud/examples/config/DatabaseConfiguration.java index b358cdc1d..2ba77c4f4 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-provider/src/main/java/com/alibaba/cloud/examples/config/DatabaseConfiguration.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.cloud.examples; +package com.alibaba.cloud.examples.config; import javax.sql.DataSource; diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/StorageController.java b/spring-cloud-alibaba-examples/seata-example/storage-service-provider/src/main/java/com/alibaba/cloud/examples/controller/StorageController.java similarity index 97% rename from spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/StorageController.java rename to spring-cloud-alibaba-examples/seata-example/storage-service-provider/src/main/java/com/alibaba/cloud/examples/controller/StorageController.java index 31bf0607b..6e37d5ca0 100644 --- a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/StorageController.java +++ b/spring-cloud-alibaba-examples/seata-example/storage-service-provider/src/main/java/com/alibaba/cloud/examples/controller/StorageController.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.alibaba.cloud.examples; +package com.alibaba.cloud.examples.controller; import io.seata.core.context.RootContext; import org.slf4j.Logger; diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service-provider/src/main/resources/application.yml b/spring-cloud-alibaba-examples/seata-example/storage-service-provider/src/main/resources/application.yml new file mode 100644 index 000000000..b3193cb1c --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/storage-service-provider/src/main/resources/application.yml @@ -0,0 +1,68 @@ +base: + config: + mdb: + hostname: 127.0.0.1 + dbname: seata + port: 3306 + username: root + password: 123456 + +server: + port: 18082 + +spring: + cloud: + nacos: + discovery: + server-addr: 127.0.0.1:8848 + application: + name: storage-service + main: + allow-bean-definition-overriding: true + datasource: + name: storageDataSource + type: com.alibaba.druid.pool.DruidDataSource + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://${base.config.mdb.hostname}:${base.config.mdb.port}/${base.config.mdb.dbname}?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull + # jdbc_url: jdbc:mysql://${base.config.mdb.hostname}:${base.config.mdb.port}/${base.config.mdb.dbname}?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull + username: ${base.config.mdb.username} + password: ${base.config.mdb.password} + druid: + max-active: 20 + min-idle: 2 + initial-size: 2 + +seata: + enabled: true + application-id: ${spring.application.name} + + tx-service-group: ${spring.application.name}-tx-group + config: + type: nacos + + nacos: + namespace: a3f588d3-7be9-43d3-af7c-1e7919cf8af6 + serverAddr: 127.0.0.1:8848 + + group: SEATA_GROUP + dataId: "seata.properties" + username: "nacos" + password: "nacos" + registry: + type: nacos + nacos: + application: seata-server + server-addr: 127.0.0.1:8848 + group: SEATA_GROUP + namespace: a3f588d3-7be9-43d3-af7c-1e7919cf8af6 + username: "nacos" + password: "nacos" +dubbo: + registry: + address: spring-cloud://localhost + protocol: dubbo + port: -1 + cloud: + subscribed-services: null + provider: + telnet: ls,ps,cd,pwd,trace,count,invoke,select,status,log,help,clear,exit,shutdown 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 deleted file mode 100644 index a2bd463a0..000000000 --- a/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/application.properties +++ /dev/null @@ -1,30 +0,0 @@ -spring.application.name=storage-service -server.port=18082 -spring.cloud.nacos.discovery.server-addr=localhost:8848 - -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://xxx:3306/seata?useSSL=false&serverTimezone=UTC -spring.datasource.username=xxx -spring.datasource.password=xxx -spring.datasource.druid.max-active=20 -spring.datasource.druid.min-idle=2 -spring.datasource.druid.initial-size=2 - - -seata.enabled=true -spring.cloud.alibaba.seata.tx-service-group=business-service -seata.service.vgroup-mapping.business-service=default -seata.service.grouplist.default=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