diff --git a/spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml b/spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml new file mode 100644 index 000000000..f7c5ca141 --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/storage-service/pom.xml @@ -0,0 +1,52 @@ + + + + spring-cloud-alibaba-examples + com.alibaba.cloud + ${revision} + ../../pom.xml + + 4.0.0 + storage-service + Spring Cloud Starter Alibaba Seata Example - Storage Service + jar + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-seata + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-jdbc + + + com.alibaba + druid-spring-boot-starter + 1.1.10 + + + mysql + mysql-connector-java + + + log4j + log4j + 1.2.17 + + + 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/src/main/java/com/alibaba/cloud/examples/StorageApplication.java new file mode 100644 index 000000000..8f15a499a --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/StorageApplication.java @@ -0,0 +1,33 @@ +/* + * 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; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author xiaojing + */ +@SpringBootApplication +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/src/main/java/com/alibaba/cloud/examples/config/DatabaseConfiguration.java b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/config/DatabaseConfiguration.java new file mode 100644 index 000000000..2ba77c4f4 --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/config/DatabaseConfiguration.java @@ -0,0 +1,55 @@ +/* + * 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.config; + +import javax.sql.DataSource; + +import com.alibaba.druid.pool.DruidDataSource; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * @author xiaojing + */ +@Configuration +public class DatabaseConfiguration { + + @Bean + @Primary + @ConfigurationProperties("spring.datasource") + public DataSource storageDataSource() { + return new DruidDataSource(); + } + + @Bean + public JdbcTemplate jdbcTemplate(DataSource dataSource) { + + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); + + 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/java/com/alibaba/cloud/examples/controller/StorageController.java b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/controller/StorageController.java new file mode 100644 index 000000000..6e37d5ca0 --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/java/com/alibaba/cloud/examples/controller/StorageController.java @@ -0,0 +1,59 @@ +/* + * 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.controller; + +import io.seata.core.context.RootContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author xiaojing + */ +@RestController +public class StorageController { + + private static final Logger LOGGER = LoggerFactory.getLogger(StorageController.class); + + private static final String SUCCESS = "SUCCESS"; + + private static final String FAIL = "FAIL"; + + private final JdbcTemplate jdbcTemplate; + + public StorageController(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + @GetMapping(value = "/storage/{commodityCode}/{count}", produces = "application/json") + public String echo(@PathVariable String commodityCode, @PathVariable int count) { + LOGGER.info("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/resources/application.yml b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/application.yml new file mode 100644 index 000000000..d72de64b4 --- /dev/null +++ b/spring-cloud-alibaba-examples/seata-example/storage-service/src/main/resources/application.yml @@ -0,0 +1,57 @@ +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 + 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" +