This is an automated email from the ASF dual-hosted git repository. ningjiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-saga.git
The following commit(s) were added to refs/heads/master by this push: new 5e37786 SCB-344 add document for how to use mysql as alpha's backend database (#137) 5e37786 is described below commit 5e3778646d303f339a9fd3585e1feeb187880304 Author: Eric Lee <dagang...@huawei.com> AuthorDate: Tue Feb 13 15:59:23 2018 +0800 SCB-344 add document for how to use mysql as alpha's backend database (#137) * SCB-330 added chinese version of README * SCB-330 add user guide for saga pack * SCB-330 attempt to fix test failure Signed-off-by: Eric Lee <dagang...@huawei.com> * SCB-344 add guide for mysql * SCB-344 add license header for sql files Signed-off-by: Eric Lee <dagang...@huawei.com> --- README.md | 4 ++ README_ZH.md | 3 + .../servicecomb/saga/alpha/core/Command.java | 2 + .../servicecomb/saga/alpha/core/TxEvent.java | 2 + .../servicecomb/saga/alpha/core/TxTimeout.java | 2 + .../src/main/resources/application.yaml | 19 ++++++- .../src/main/resources/schema-mysql.sql | 66 ++++++++++++++++++++++ .../src/main/resources/schema-postgresql.sql | 19 ++++++- alpha/alpha-server/src/test/resources/schema.sql | 17 ++++++ .../how_to_use_mysql_as_alpha_backend_database.md | 12 ++++ .../how_to_use_mysql_as_alpha_backend_database.md | 12 ++++ .../omega-spring-tx/src/test/resources/schema.sql | 17 ++++++ saga-demo/pack-demo/README.md | 26 +++++++-- .../pack-demo/docker-compose.mysql.yaml | 32 +++++++---- saga-demo/pack-demo/docker-compose.yaml | 17 ++++-- .../src/main/resources/schema-postgresql.sql | 17 ++++++ saga-spring/src/test/resources/data.sql | 17 ++++++ 17 files changed, 260 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index aa9b840..61c9c8f 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,10 @@ See [Booking Demo](saga-demo/pack-demo/README.md) for details. ## User Guide See [User Guide](docs/user_guide.md) for details. + +## FAQ +* [How to use MySQL as alpha's backend database?](docs/faq/en/how_to_use_mysql_as_alpha_backend_database.md) + ## Contact Us * [issues](https://issues.apache.org/jira/browse/SCB) * [gitter](https://gitter.im/ServiceCombUsers/Lobby) diff --git a/README_ZH.md b/README_ZH.md index 2ea467b..ac5f8a3 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -25,6 +25,9 @@ Saga是由 **alpha** 和 **omega**组成,其中: ## 用户指南 详情可浏览[用户指南](docs/user_guide_zh.md)。 +## 常见问题 +* [如何使用MySQL作为alpha的后台数据库?](docs/faq/cn/how_to_use_mysql_as_alpha_backend_database.md) + ## 联系我们 * [提交issues](https://issues.apache.org/jira/browse/SCB) * [gitter聊天室](https://gitter.im/ServiceCombUsers/Lobby) diff --git a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/Command.java b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/Command.java index 1e6f21b..0f016d3 100644 --- a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/Command.java +++ b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/Command.java @@ -25,9 +25,11 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.Table; import javax.persistence.Version; @Entity +@Table(name = "Command") public class Command { @Id diff --git a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxEvent.java b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxEvent.java index 42a202f..b17a120 100644 --- a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxEvent.java +++ b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxEvent.java @@ -25,9 +25,11 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.Table; import javax.persistence.Transient; @Entity +@Table(name = "TxEvent") public class TxEvent { @Transient private static final long MAX_TIMESTAMP = 253402214400000L; // 9999-12-31 00:00:00 diff --git a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxTimeout.java b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxTimeout.java index 00ca2ec..342321f 100644 --- a/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxTimeout.java +++ b/alpha/alpha-core/src/main/java/org/apache/servicecomb/saga/alpha/core/TxTimeout.java @@ -23,9 +23,11 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.Table; import javax.persistence.Version; @Entity +@Table(name = "TxTimeout") public class TxTimeout { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/alpha/alpha-server/src/main/resources/application.yaml b/alpha/alpha-server/src/main/resources/application.yaml index a4790e2..43b39dc 100644 --- a/alpha/alpha-server/src/main/resources/application.yaml +++ b/alpha/alpha-server/src/main/resources/application.yaml @@ -14,6 +14,10 @@ ## See the License for the specific language governing permissions and ## limitations under the License. ## --------------------------------------------------------------------------- +server: + port: 8090 + +--- spring: profiles: prd datasource: @@ -23,5 +27,16 @@ spring: platform: postgresql # continue-on-error: true -server: - port: 8090 +--- +spring: + profiles: mysql + datasource: + username: saga + password: password + url: jdbc:mysql://mysql.servicecomb.io:3306/saga?useSSL=false + platform: mysql + continue-on-error: true + jpa: + properties: + eclipselink: + ddl-generation: none \ No newline at end of file diff --git a/alpha/alpha-server/src/main/resources/schema-mysql.sql b/alpha/alpha-server/src/main/resources/schema-mysql.sql new file mode 100644 index 0000000..b0bc8d7 --- /dev/null +++ b/alpha/alpha-server/src/main/resources/schema-mysql.sql @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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. + */ + +CREATE TABLE IF NOT EXISTS TxEvent ( + surrogateId bigint NOT NULL AUTO_INCREMENT, + serviceName varchar(16) NOT NULL, + instanceId varchar(36) NOT NULL, + creationTime datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + globalTxId varchar(36) NOT NULL, + localTxId varchar(36) NOT NULL, + parentTxId varchar(36) DEFAULT NULL, + type varchar(50) NOT NULL, + compensationMethod varchar(256) NOT NULL, + expiryTime datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + payloads varbinary(10240), + PRIMARY KEY (surrogateId), + INDEX saga_events_index (surrogateId, globalTxId, localTxId, type, expiryTime) +) DEFAULT CHARSET=utf8; + + +CREATE TABLE IF NOT EXISTS Command ( + surrogateId bigint NOT NULL AUTO_INCREMENT, + eventId bigint NOT NULL UNIQUE, + serviceName varchar(16) NOT NULL, + instanceId varchar(36) NOT NULL, + globalTxId varchar(36) NOT NULL, + localTxId varchar(36) NOT NULL, + parentTxId varchar(36) DEFAULT NULL, + compensationMethod varchar(256) NOT NULL, + payloads varbinary(10240), + status varchar(12), + lastModified datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + version bigint NOT NULL, + PRIMARY KEY (surrogateId), + INDEX saga_commands_index (surrogateId, eventId, globalTxId, localTxId, status) +) DEFAULT CHARSET=utf8; + +CREATE TABLE IF NOT EXISTS TxTimeout ( + surrogateId bigint NOT NULL AUTO_INCREMENT, + eventId bigint NOT NULL UNIQUE, + serviceName varchar(16) NOT NULL, + instanceId varchar(36) NOT NULL, + globalTxId varchar(36) NOT NULL, + localTxId varchar(36) NOT NULL, + parentTxId varchar(36) DEFAULT NULL, + type varchar(50) NOT NULL, + expiryTime datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + status varchar(12), + version bigint NOT NULL, + PRIMARY KEY (surrogateId), + INDEX saga_timeouts_index (surrogateId, expiryTime, globalTxId, localTxId, status) +) DEFAULT CHARSET=utf8; \ No newline at end of file diff --git a/alpha/alpha-server/src/main/resources/schema-postgresql.sql b/alpha/alpha-server/src/main/resources/schema-postgresql.sql index e7f774b..41815ee 100644 --- a/alpha/alpha-server/src/main/resources/schema-postgresql.sql +++ b/alpha/alpha-server/src/main/resources/schema-postgresql.sql @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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. + */ + CREATE TABLE IF NOT EXISTS TxEvent ( surrogateId BIGSERIAL PRIMARY KEY, serviceName varchar(16) NOT NULL, @@ -12,7 +29,7 @@ CREATE TABLE IF NOT EXISTS TxEvent ( payloads bytea ); -CREATE INDEX IF NOT EXISTS saga_events_index ON TxEvent (surrogateId, globalTxId, localTxId, type); +CREATE INDEX IF NOT EXISTS saga_events_index ON TxEvent (surrogateId, globalTxId, localTxId, type, expiryTime); CREATE TABLE IF NOT EXISTS Command ( diff --git a/alpha/alpha-server/src/test/resources/schema.sql b/alpha/alpha-server/src/test/resources/schema.sql index 929c69f..a10a4e0 100644 --- a/alpha/alpha-server/src/test/resources/schema.sql +++ b/alpha/alpha-server/src/test/resources/schema.sql @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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. + */ + CREATE TABLE IF NOT EXISTS TxEvent ( surrogateId bigint GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY, serviceName varchar(36) NOT NULL, diff --git a/docs/faq/cn/how_to_use_mysql_as_alpha_backend_database.md b/docs/faq/cn/how_to_use_mysql_as_alpha_backend_database.md new file mode 100644 index 0000000..891a124 --- /dev/null +++ b/docs/faq/cn/how_to_use_mysql_as_alpha_backend_database.md @@ -0,0 +1,12 @@ +**问题描述:** 如何使用MySQL作为alpha的后台数据库? + +**解决方法:** 使用MySQL来替换默认的数据库PostgreSQL,只需以下两步: +1. 在`alpha/alpha-server/pom.xml`添加`mysql-connector-java`的依赖: + ```xml + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + </dependency> + ``` + +2. 在alpha启动时通过添加`-Dspring.profiles.active=mysql`的启动参数来使mysql的配置生效。 diff --git a/docs/faq/en/how_to_use_mysql_as_alpha_backend_database.md b/docs/faq/en/how_to_use_mysql_as_alpha_backend_database.md new file mode 100644 index 0000000..adb44e2 --- /dev/null +++ b/docs/faq/en/how_to_use_mysql_as_alpha_backend_database.md @@ -0,0 +1,12 @@ +**Problem:** How to use MySQL as alpha's backend database? + +**Solution:** To replace the default database from postgreSQL to MySQL: +1. add dependency of `mysql-connector-java` in `alpha/alpha-server/pom.xml` + ```xml + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + </dependency> + ``` + +2. activate mysql profile by specifying option `-Dspring.profiles.active=mysql` when booting alpha. \ No newline at end of file diff --git a/omega/omega-spring-tx/src/test/resources/schema.sql b/omega/omega-spring-tx/src/test/resources/schema.sql index cbc4330..e3b36c4 100644 --- a/omega/omega-spring-tx/src/test/resources/schema.sql +++ b/omega/omega-spring-tx/src/test/resources/schema.sql @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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. + */ + CREATE TABLE IF NOT EXISTS `User` ( `id` bigint NOT NULL AUTO_INCREMENT, `username` varchar(36) NOT NULL, diff --git a/saga-demo/pack-demo/README.md b/saga-demo/pack-demo/README.md index 9eecd62..4ceedaf 100644 --- a/saga-demo/pack-demo/README.md +++ b/saga-demo/pack-demo/README.md @@ -18,14 +18,28 @@ You will need: ## Running Demo 1. run the following command to create docker images in saga project root folder. -``` -mvn package -DskipTests -Pdocker -Pdemo -``` + ``` + mvn package -DskipTests -Pdocker -Pdemo + ``` 2. start application up in saga/saga-demo/pack-demo with the following command -``` -docker-compose up -``` + ``` + docker-compose up + ``` + + **Note:** If you prefer to use MySQL as alpha's backend database, you need to try the following steps instead: + 1. add dependency of `mysql-connector-java` in `alpha/alpha-server/pom.xml` + ```xml + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + </dependency> + ``` + + 2. start application up in saga/saga-demo/pack-demo with the following command + ``` + docker-compose -f docker-compose.yaml -f docker-compose.mysql.yaml up + ``` ## User Requests 1. Booking 2 rooms and 2 cars, this booking will be OK. diff --git a/alpha/alpha-server/src/main/resources/application.yaml b/saga-demo/pack-demo/docker-compose.mysql.yaml similarity index 65% copy from alpha/alpha-server/src/main/resources/application.yaml copy to saga-demo/pack-demo/docker-compose.mysql.yaml index a4790e2..43f3b5e 100644 --- a/alpha/alpha-server/src/main/resources/application.yaml +++ b/saga-demo/pack-demo/docker-compose.mysql.yaml @@ -14,14 +14,26 @@ ## See the License for the specific language governing permissions and ## limitations under the License. ## --------------------------------------------------------------------------- -spring: - profiles: prd - datasource: - username: saga - password: password - url: jdbc:postgresql://postgresql.servicecomb.io:5432/saga?useSSL=false - platform: postgresql -# continue-on-error: true -server: - port: 8090 +version: '2.1' + +services: + database: + image: "mysql/mysql-server:5.7" + hostname: mysql + environment: + - MYSQL_ROOT_PASSWORD=password + - MYSQL_DATABASE=saga + - MYSQL_USER=saga + - MYSQL_PASSWORD=password + healthcheck: + test: ["CMD-SHELL", "nc -z localhost 3306 &> /dev/null; echo $$?"] + interval: 30s + timeout: 10s + retries: 5 + + alpha: + links: + - "database:mysql.servicecomb.io" + environment: + - JAVA_OPTS=-Dspring.profiles.active=mysql diff --git a/saga-demo/pack-demo/docker-compose.yaml b/saga-demo/pack-demo/docker-compose.yaml index b452d24..e528c83 100644 --- a/saga-demo/pack-demo/docker-compose.yaml +++ b/saga-demo/pack-demo/docker-compose.yaml @@ -18,7 +18,7 @@ version: '2.1' services: - postgres: + database: image: "postgres" hostname: postgres environment: @@ -35,11 +35,16 @@ services: image: "alpha-server:0.0.3-SNAPSHOT" hostname: alpha-server links: - - "postgres:postgresql.servicecomb.io" + - "database:postgresql.servicecomb.io" environment: - JAVA_OPTS=-Dspring.profiles.active=prd + healthcheck: + test: ["CMD-SHELL", "nc -z localhost 8080 &> /dev/null; echo $$?"] + interval: 30s + timeout: 10s + retries: 5 depends_on: - postgres: + database: condition: service_healthy pack-hotel: @@ -50,7 +55,8 @@ services: ports: - "8081:8080" depends_on: - - alpha + alpha: + condition: service_healthy pack-car: image: "pack-car:0.0.3-SNAPSHOT" @@ -60,7 +66,8 @@ services: ports: - "8082:8080" depends_on: - - alpha + alpha: + condition: service_healthy pack-booking: image: "pack-booking:0.0.3-SNAPSHOT" diff --git a/saga-spring/src/main/resources/schema-postgresql.sql b/saga-spring/src/main/resources/schema-postgresql.sql index c4a5c70..7a2cd1f 100644 --- a/saga-spring/src/main/resources/schema-postgresql.sql +++ b/saga-spring/src/main/resources/schema-postgresql.sql @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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. + */ + CREATE TABLE IF NOT EXISTS SagaEventEntity ( id BIGSERIAL PRIMARY KEY, sagaId varchar(36) NOT NULL, diff --git a/saga-spring/src/test/resources/data.sql b/saga-spring/src/test/resources/data.sql index d0db33e..5cdb7d9 100644 --- a/saga-spring/src/test/resources/data.sql +++ b/saga-spring/src/test/resources/data.sql @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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. + */ + DROP TABLE IF EXISTS SagaEventEntity; CREATE TABLE `SagaEventEntity` ( -- To stop receiving notification emails like this one, please contact ningji...@apache.org.