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
commit 86b2523e1948964538da0ca972c6338864498b98 Author: zhengyangyong <yangyong.zh...@huawei.com> AuthorDate: Wed Jun 20 15:04:21 2018 +0800 SCB-244 add servicecomb demo Signed-off-by: zhengyangyong <yangyong.zh...@huawei.com> --- .../omega-transport-servicecomb/pom.xml | 4 + .../transport/servicecomb/SagaBootListener.java | 64 --------- .../transport/servicecomb/SagaConsumerHandler.java | 12 ++ .../transport/servicecomb/SagaProviderHandler.java | 12 ++ .../servicecomb/SagaConsumerHandlerTest.java | 3 +- .../servicecomb/SagaProviderHandlerTest.java | 3 +- saga-demo/pom.xml | 1 + saga-demo/saga-servicecomb-demo/pom.xml | 148 +++++++++++++++++++++ .../saga-servicecomb-demo/scb-booking}/pom.xml | 24 +--- .../saga/demo/scb/booking/BookingApplication.java | 32 +++++ .../saga/demo/scb/booking/BookingController.java | 57 ++++++++ .../src/main/resources/application.yaml | 22 +++ .../scb-booking/src/main/resources/log4j2.xml | 30 +++++ .../src/main/resources/microservice.yaml | 46 +++++++ .../saga-servicecomb-demo/scb-car}/pom.xml | 24 +--- .../saga/demo/scb/car/CarApplication.java | 32 +++++ .../servicecomb/saga/demo/scb/car/CarBooking.java | 74 +++++++++++ .../saga/demo/scb/car/CarBookingController.java | 62 +++++++++ .../saga/demo/scb/car/CarBookingService.java | 51 +++++++ .../scb-car/src/main/resources/application.yaml | 22 +++ .../scb-car/src/main/resources/log4j2.xml | 30 +++++ .../scb-car/src/main/resources/microservice.yaml | 46 +++++++ .../saga-servicecomb-demo/scb-hotel}/pom.xml | 24 +--- .../saga/demo/scb/hotel/HotelApplication.java | 32 +++++ .../saga/demo/scb/hotel/HotelBooking.java | 74 +++++++++++ .../demo/scb/hotel/HotelBookingController.java | 62 +++++++++ .../saga/demo/scb/hotel/HotelBookingService.java | 54 ++++++++ .../scb-hotel/src/main/resources/application.yaml | 22 +++ .../scb-hotel/src/main/resources/log4j2.xml | 30 +++++ .../scb-hotel/src/main/resources/microservice.yaml | 46 +++++++ 30 files changed, 1017 insertions(+), 126 deletions(-) diff --git a/omega/omega-transport/omega-transport-servicecomb/pom.xml b/omega/omega-transport/omega-transport-servicecomb/pom.xml index 8eb6203..a85d8bf 100644 --- a/omega/omega-transport/omega-transport-servicecomb/pom.xml +++ b/omega/omega-transport/omega-transport-servicecomb/pom.xml @@ -28,6 +28,10 @@ <artifactId>omega-transport-servicecomb</artifactId> + <properties> + <java-chassis.version>1.0.0-m2</java-chassis.version> + </properties> + <dependencies> <!-- We just need this jar for compile, the runtime should provide full dependencies --> <dependency> diff --git a/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaBootListener.java b/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaBootListener.java deleted file mode 100644 index 412aa12..0000000 --- a/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaBootListener.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2017 Huawei Technologies Co., Ltd - * - * 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 - * - * 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. - */ - -package org.apache.servicecomb.saga.omega.transport.servicecomb; - -import java.util.List; - -import org.apache.servicecomb.core.BootListener; -import org.apache.servicecomb.core.Handler; -import org.apache.servicecomb.core.handler.ConsumerHandlerManager; -import org.apache.servicecomb.core.handler.ProducerHandlerManager; -import org.apache.servicecomb.saga.omega.context.OmegaContext; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class SagaBootListener implements BootListener { - @Autowired(required = false) - private OmegaContext omegaContext; - - @Override - public void onBootEvent(BootEvent bootEvent) { - if (EventType.AFTER_HANDLER.equals(bootEvent.getEventType())) { - for (List<Handler> handlers : ConsumerHandlerManager.INSTANCE.values()) { - injectOmegaContextIntoConsumerHandler(handlers); - } - - for (List<Handler> handlers : ProducerHandlerManager.INSTANCE.values()) { - injectOmegaContextIntoProducerHandler(handlers); - } - } - } - - private void injectOmegaContextIntoConsumerHandler(List<Handler> handlers) { - for (Handler handler : handlers) { - if (handler.getClass().equals(SagaConsumerHandler.class)) { - ((SagaConsumerHandler) handler).setOmegaContext(omegaContext); - return; - } - } - } - - private void injectOmegaContextIntoProducerHandler(List<Handler> handlers) { - for (Handler handler : handlers) { - if (handler.getClass().equals(SagaProviderHandler.class)) { - ((SagaProviderHandler) handler).setOmegaContext(omegaContext); - return; - } - } - } -} \ No newline at end of file diff --git a/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandler.java b/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandler.java index a0b0b40..41f16ed 100644 --- a/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandler.java +++ b/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandler.java @@ -24,17 +24,29 @@ import java.lang.invoke.MethodHandles; import org.apache.servicecomb.core.Handler; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.foundation.common.utils.BeanUtils; import org.apache.servicecomb.saga.omega.context.OmegaContext; import org.apache.servicecomb.swagger.invocation.AsyncResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.annotations.VisibleForTesting; + public class SagaConsumerHandler implements Handler { private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private OmegaContext omegaContext; + public SagaConsumerHandler() { + try { + omegaContext = BeanUtils.getBean("omegaContext"); + } catch (NullPointerException npe) { + LOG.info("The OmegaContext is not injected, The SagaConsumerHandler is disabled."); + } + } + + @VisibleForTesting public void setOmegaContext(OmegaContext omegaContext) { this.omegaContext = omegaContext; } diff --git a/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandler.java b/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandler.java index 66e4a7f..bd73c31 100644 --- a/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandler.java +++ b/omega/omega-transport/omega-transport-servicecomb/src/main/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandler.java @@ -24,17 +24,29 @@ import java.lang.invoke.MethodHandles; import org.apache.servicecomb.core.Handler; import org.apache.servicecomb.core.Invocation; +import org.apache.servicecomb.foundation.common.utils.BeanUtils; import org.apache.servicecomb.saga.omega.context.OmegaContext; import org.apache.servicecomb.swagger.invocation.AsyncResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.annotations.VisibleForTesting; + public class SagaProviderHandler implements Handler { private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private OmegaContext omegaContext; + public SagaProviderHandler() { + try { + omegaContext = BeanUtils.getBean("omegaContext"); + } catch (NullPointerException npe) { + LOG.info("The OmegaContext is not injected, The SagaProviderHandler is disabled."); + } + } + + @VisibleForTesting public void setOmegaContext(OmegaContext omegaContext) { this.omegaContext = omegaContext; } diff --git a/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandlerTest.java b/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandlerTest.java index edf2c79..691b285 100644 --- a/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandlerTest.java +++ b/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaConsumerHandlerTest.java @@ -45,11 +45,12 @@ public class SagaConsumerHandlerTest { private final Invocation invocation = mock(Invocation.class); private final AsyncResponse asyncResponse = mock(AsyncResponse.class); - private final SagaConsumerHandler handler = new SagaConsumerHandler(omegaContext); + private final SagaConsumerHandler handler = new SagaConsumerHandler(); @Before public void setUp() { when(idGenerator.nextId()).thenReturn(globalTxId, localTxId); + handler.setOmegaContext(omegaContext); } @Test diff --git a/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandlerTest.java b/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandlerTest.java index d789c93..4a6e5d6 100644 --- a/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandlerTest.java +++ b/omega/omega-transport/omega-transport-servicecomb/src/test/java/org/apache/servicecomb/saga/omega/transport/servicecomb/SagaProviderHandlerTest.java @@ -45,11 +45,12 @@ public class SagaProviderHandlerTest { private final Invocation invocation = mock(Invocation.class); private final AsyncResponse asyncResponse = mock(AsyncResponse.class); - private final SagaProviderHandler handler = new SagaProviderHandler(omegaContext); + private final SagaProviderHandler handler = new SagaProviderHandler(); @Before public void setUp() { omegaContext.clear(); + handler.setOmegaContext(omegaContext); } @Test diff --git a/saga-demo/pom.xml b/saga-demo/pom.xml index 8b23e35..e321a16 100644 --- a/saga-demo/pom.xml +++ b/saga-demo/pom.xml @@ -32,6 +32,7 @@ <modules> <module>saga-dubbo-demo</module> <module>saga-spring-cloud-demo</module> + <module>saga-servicecomb-demo</module> </modules> <dependencyManagement> diff --git a/saga-demo/saga-servicecomb-demo/pom.xml b/saga-demo/saga-servicecomb-demo/pom.xml new file mode 100644 index 0000000..3c90925 --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/pom.xml @@ -0,0 +1,148 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>saga-demo</artifactId> + <groupId>org.apache.servicecomb.saga.demo</groupId> + <version>0.3.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>saga-servicecomb-demo</artifactId> + <name>Saga::Demo::ServiceComb-Demo</name> + <packaging>pom</packaging> + + + <modules> + <module>scb-car</module> + <module>scb-hotel</module> + <module>scb-booking</module> + </modules> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <maven.compiler.source>1.8</maven.compiler.source> + <maven.compiler.target>1.8</maven.compiler.target> + <saga.version>0.3.0-SNAPSHOT</saga.version> + <java-chassis.version>1.0.0-m2</java-chassis.version> + </properties> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.apache.servicecomb</groupId> + <artifactId>java-chassis-dependencies</artifactId> + <version>${java-chassis.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + + <dependency> + <groupId>org.apache.servicecomb.saga</groupId> + <artifactId>omega-transport-servicecomb</artifactId> + <version>${saga.version}</version> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + <version>20.0</version> + </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + <version>3.6</version> + <scope>Compile</scope> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.servicecomb</groupId> + <artifactId>spring-boot-starter-provider</artifactId> + </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-validator</artifactId> + </dependency> + <dependency> + <groupId>org.apache.servicecomb.saga</groupId> + <artifactId>omega-spring-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.servicecomb.saga</groupId> + <artifactId>omega-transport-servicecomb</artifactId> + </dependency> + </dependencies> + + <build> + <plugins> + <!-- mixin plugin configurations declared in another pom, + just like importing dependencies managed in another pom --> + <plugin> + <groupId>com.github.odavid.maven.plugins</groupId> + <artifactId>mixin-maven-plugin</artifactId> + </plugin> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + </plugin> + </plugins> + </build> + + <profiles> + <profile> + <id>docker</id> + <activation> + <file> + <exists>/var/run/docker.sock</exists> + </file> + </activation> + <build> + <plugins> + <plugin> + <groupId>io.fabric8</groupId> + <artifactId>docker-maven-plugin</artifactId> + </plugin> + <plugin> + <groupId>org.commonjava.maven.plugins</groupId> + <artifactId>directory-maven-plugin</artifactId> + </plugin> + </plugins> + </build> + </profile> + <profile> + <id>demo</id> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + </plugin> + </plugins> + </build> + </profile> + </profiles> +</project> \ No newline at end of file diff --git a/omega/omega-transport/omega-transport-servicecomb/pom.xml b/saga-demo/saga-servicecomb-demo/scb-booking/pom.xml similarity index 64% copy from omega/omega-transport/omega-transport-servicecomb/pom.xml copy to saga-demo/saga-servicecomb-demo/scb-booking/pom.xml index 8eb6203..fccd810 100644 --- a/omega/omega-transport/omega-transport-servicecomb/pom.xml +++ b/saga-demo/saga-servicecomb-demo/scb-booking/pom.xml @@ -20,28 +20,12 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> - <artifactId>omega-transport</artifactId> - <groupId>org.apache.servicecomb.saga</groupId> + <artifactId>saga-servicecomb-demo</artifactId> + <groupId>org.apache.servicecomb.saga.demo</groupId> <version>0.3.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> - <artifactId>omega-transport-servicecomb</artifactId> + <artifactId>scb-booking</artifactId> - <dependencies> - <!-- We just need this jar for compile, the runtime should provide full dependencies --> - <dependency> - <groupId>org.apache.servicecomb</groupId> - <artifactId>java-chassis-core</artifactId> - <version>${java.chassis.version}</version> - <scope>provided</scope> - </dependency> - - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-simple</artifactId> - <scope>test</scope> - </dependency> - </dependencies> - -</project> +</project> \ No newline at end of file diff --git a/saga-demo/saga-servicecomb-demo/scb-booking/src/main/java/org/apache/servicecomb/saga/demo/scb/booking/BookingApplication.java b/saga-demo/saga-servicecomb-demo/scb-booking/src/main/java/org/apache/servicecomb/saga/demo/scb/booking/BookingApplication.java new file mode 100644 index 0000000..bc24337 --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-booking/src/main/java/org/apache/servicecomb/saga/demo/scb/booking/BookingApplication.java @@ -0,0 +1,32 @@ +/* + * 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. + */ + +package org.apache.servicecomb.saga.demo.scb.booking; + +import org.apache.servicecomb.saga.omega.spring.EnableOmega; +import org.apache.servicecomb.springboot.starter.provider.EnableServiceComb; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@EnableServiceComb +@EnableOmega +public class BookingApplication { + public static void main(String[] args) { + SpringApplication.run(BookingApplication.class, args); + } +} diff --git a/saga-demo/saga-servicecomb-demo/scb-booking/src/main/java/org/apache/servicecomb/saga/demo/scb/booking/BookingController.java b/saga-demo/saga-servicecomb-demo/scb-booking/src/main/java/org/apache/servicecomb/saga/demo/scb/booking/BookingController.java new file mode 100644 index 0000000..cc6947b --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-booking/src/main/java/org/apache/servicecomb/saga/demo/scb/booking/BookingController.java @@ -0,0 +1,57 @@ +/* + * 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. + */ + +package org.apache.servicecomb.saga.demo.scb.booking; + +import org.apache.servicecomb.provider.rest.common.RestSchema; +import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder; +import org.apache.servicecomb.saga.omega.context.annotations.SagaStart; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestSchema(schemaId = "booking") +@RequestMapping(path = "/") +public class BookingController { + + private RestTemplate template = RestTemplateBuilder.create(); + + @SagaStart + @PostMapping("/booking/{name}/{rooms}/{cars}") + public String order(@PathVariable String name, @PathVariable Integer rooms, @PathVariable Integer cars) { + template.postForEntity( + "cse://car/order/{name}/{cars}", + null, String.class, name, cars); + + template.postForEntity( + "cse://hotel/order/{name}/{rooms}", + null, String.class, name, rooms); + + postBooking(); + + return name + " booking " + rooms + " rooms and " + cars + " cars OK"; + } + + // This method is used by the byteman to inject the faults such as the timeout or the crash + private void postBooking() { + + } +} diff --git a/saga-demo/saga-servicecomb-demo/scb-booking/src/main/resources/application.yaml b/saga-demo/saga-servicecomb-demo/scb-booking/src/main/resources/application.yaml new file mode 100644 index 0000000..568f739 --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-booking/src/main/resources/application.yaml @@ -0,0 +1,22 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +spring: + application: + name: booking +alpha: + cluster: + address: 192.168.99.100:8080 diff --git a/saga-demo/saga-servicecomb-demo/scb-booking/src/main/resources/log4j2.xml b/saga-demo/saga-servicecomb-demo/scb-booking/src/main/resources/log4j2.xml new file mode 100644 index 0000000..cae04cb --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-booking/src/main/resources/log4j2.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> + +<Configuration status="WARN"> + <Appenders> + <Console name="Console" target="SYSTEM_OUT"> + <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> + </Console> + </Appenders> + <Loggers> + <AsyncRoot level="info"> + <AppenderRef ref="Console"/> + </AsyncRoot> + </Loggers> +</Configuration> diff --git a/saga-demo/saga-servicecomb-demo/scb-booking/src/main/resources/microservice.yaml b/saga-demo/saga-servicecomb-demo/scb-booking/src/main/resources/microservice.yaml new file mode 100644 index 0000000..0cc5767 --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-booking/src/main/resources/microservice.yaml @@ -0,0 +1,46 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +#More details can be found : +# 1.http://servicecomb.incubator.apache.org/users/service-definition/ +# 2.http://servicecomb.incubator.apache.org/users/service-configurations/ +# 3.http://servicecomb.incubator.apache.org/users/communicate-protocol/ + +#Indicates an application name +APPLICATION_ID: demo +service_description: +#Indicates a microservice name +#The microservice name should be unique within an application. +#The name can contain digits, uppercase and lowercase letters, hyphens(-), underscores(_), and periods(.); and can neither start nor end with punctuations. +#The naming rule is as follows: ^[a-zA-Z0-9]+$|^[a-zA-Z0-9][a-zA-Z0-9_-.]*[a-zA-Z0-9]$. + name: booking +#Indicates a service version + version: 1.0.0 +servicecomb: + service: + #Specifies the service center IP address. + registry: + address: http://127.0.0.1:30100 + #Specifies the rest transport listening IP address. + rest: + address: 127.0.0.1:18080 + + #Add Saga Handler + handler: + chain: + Consumer: + default: loadbalance,saga-consumer \ No newline at end of file diff --git a/omega/omega-transport/omega-transport-servicecomb/pom.xml b/saga-demo/saga-servicecomb-demo/scb-car/pom.xml similarity index 64% copy from omega/omega-transport/omega-transport-servicecomb/pom.xml copy to saga-demo/saga-servicecomb-demo/scb-car/pom.xml index 8eb6203..8620d20 100644 --- a/omega/omega-transport/omega-transport-servicecomb/pom.xml +++ b/saga-demo/saga-servicecomb-demo/scb-car/pom.xml @@ -20,28 +20,12 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> - <artifactId>omega-transport</artifactId> - <groupId>org.apache.servicecomb.saga</groupId> + <artifactId>saga-servicecomb-demo</artifactId> + <groupId>org.apache.servicecomb.saga.demo</groupId> <version>0.3.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> - <artifactId>omega-transport-servicecomb</artifactId> + <artifactId>scb-car</artifactId> - <dependencies> - <!-- We just need this jar for compile, the runtime should provide full dependencies --> - <dependency> - <groupId>org.apache.servicecomb</groupId> - <artifactId>java-chassis-core</artifactId> - <version>${java.chassis.version}</version> - <scope>provided</scope> - </dependency> - - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-simple</artifactId> - <scope>test</scope> - </dependency> - </dependencies> - -</project> +</project> \ No newline at end of file diff --git a/saga-demo/saga-servicecomb-demo/scb-car/src/main/java/org/apache/servicecomb/saga/demo/scb/car/CarApplication.java b/saga-demo/saga-servicecomb-demo/scb-car/src/main/java/org/apache/servicecomb/saga/demo/scb/car/CarApplication.java new file mode 100644 index 0000000..435168d --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-car/src/main/java/org/apache/servicecomb/saga/demo/scb/car/CarApplication.java @@ -0,0 +1,32 @@ +/* + * 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. + */ + +package org.apache.servicecomb.saga.demo.scb.car; + +import org.apache.servicecomb.saga.omega.spring.EnableOmega; +import org.apache.servicecomb.springboot.starter.provider.EnableServiceComb; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@EnableServiceComb +@EnableOmega +public class CarApplication { + public static void main(String[] args) { + SpringApplication.run(CarApplication.class, args); + } +} diff --git a/saga-demo/saga-servicecomb-demo/scb-car/src/main/java/org/apache/servicecomb/saga/demo/scb/car/CarBooking.java b/saga-demo/saga-servicecomb-demo/scb-car/src/main/java/org/apache/servicecomb/saga/demo/scb/car/CarBooking.java new file mode 100644 index 0000000..497f278 --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-car/src/main/java/org/apache/servicecomb/saga/demo/scb/car/CarBooking.java @@ -0,0 +1,74 @@ +/* + * 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. + */ + +package org.apache.servicecomb.saga.demo.scb.car; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; +import com.fasterxml.jackson.annotation.JsonIgnore; + +@JsonAutoDetect(fieldVisibility = Visibility.ANY) +class CarBooking { + @JsonIgnore + private Integer id; + private String name; + private Integer amount; + private boolean confirmed; + private boolean cancelled; + + Integer getId() { + return id; + } + + void setId(Integer id) { + this.id = id; + } + + String getName() { + return name; + } + + void setName(String name) { + this.name = name; + } + + Integer getAmount() { + return amount; + } + + void setAmount(Integer amount) { + this.amount = amount; + } + + boolean isConfirmed() { + return confirmed; + } + + void confirm() { + this.confirmed = true; + this.cancelled = false; + } + + boolean isCancelled() { + return cancelled; + } + + void cancel() { + this.confirmed = false; + this.cancelled = true; + } +} diff --git a/saga-demo/saga-servicecomb-demo/scb-car/src/main/java/org/apache/servicecomb/saga/demo/scb/car/CarBookingController.java b/saga-demo/saga-servicecomb-demo/scb-car/src/main/java/org/apache/servicecomb/saga/demo/scb/car/CarBookingController.java new file mode 100644 index 0000000..35ff146 --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-car/src/main/java/org/apache/servicecomb/saga/demo/scb/car/CarBookingController.java @@ -0,0 +1,62 @@ +/* + * 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. + */ + +package org.apache.servicecomb.saga.demo.scb.car; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.servicecomb.provider.rest.common.RestSchema; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@RestSchema(schemaId = "car") +@RequestMapping(path = "/") +public class CarBookingController { + @Autowired + private CarBookingService service; + + private final AtomicInteger id = new AtomicInteger(0); + + @CrossOrigin + @GetMapping(path = "/bookings") + public List<CarBooking> getAll() { + return new ArrayList<>(service.getAllBookings()); + } + + @PostMapping(path = "/order/{name}/{cars}") + public CarBooking order(@PathVariable String name, @PathVariable Integer cars) { + CarBooking booking = new CarBooking(); + booking.setId(id.incrementAndGet()); + booking.setName(name); + booking.setAmount(cars); + service.order(booking); + return booking; + } + + @DeleteMapping(path = "/bookings") + void clear() { + service.clearAllBookings(); + id.set(0); + } +} diff --git a/saga-demo/saga-servicecomb-demo/scb-car/src/main/java/org/apache/servicecomb/saga/demo/scb/car/CarBookingService.java b/saga-demo/saga-servicecomb-demo/scb-car/src/main/java/org/apache/servicecomb/saga/demo/scb/car/CarBookingService.java new file mode 100644 index 0000000..a94cf09 --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-car/src/main/java/org/apache/servicecomb/saga/demo/scb/car/CarBookingService.java @@ -0,0 +1,51 @@ +/* + * 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. + */ + +package org.apache.servicecomb.saga.demo.scb.car; + +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.servicecomb.saga.omega.transaction.annotations.Compensable; +import org.springframework.stereotype.Service; + +@Service +class CarBookingService { + private Map<Integer, CarBooking> bookings = new ConcurrentHashMap<>(); + + @Compensable(compensationMethod = "cancel") + void order(CarBooking booking) { + booking.confirm(); + bookings.put(booking.getId(), booking); + } + + void cancel(CarBooking booking) { + Integer id = booking.getId(); + if (bookings.containsKey(id)) { + bookings.get(id).cancel(); + } + } + + Collection<CarBooking> getAllBookings() { + return bookings.values(); + } + + void clearAllBookings() { + bookings.clear(); + } +} diff --git a/saga-demo/saga-servicecomb-demo/scb-car/src/main/resources/application.yaml b/saga-demo/saga-servicecomb-demo/scb-car/src/main/resources/application.yaml new file mode 100644 index 0000000..bef9ad1 --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-car/src/main/resources/application.yaml @@ -0,0 +1,22 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +spring: + application: + name: car +alpha: + cluster: + address: 192.168.99.100:8080 diff --git a/saga-demo/saga-servicecomb-demo/scb-car/src/main/resources/log4j2.xml b/saga-demo/saga-servicecomb-demo/scb-car/src/main/resources/log4j2.xml new file mode 100644 index 0000000..cae04cb --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-car/src/main/resources/log4j2.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> + +<Configuration status="WARN"> + <Appenders> + <Console name="Console" target="SYSTEM_OUT"> + <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> + </Console> + </Appenders> + <Loggers> + <AsyncRoot level="info"> + <AppenderRef ref="Console"/> + </AsyncRoot> + </Loggers> +</Configuration> diff --git a/saga-demo/saga-servicecomb-demo/scb-car/src/main/resources/microservice.yaml b/saga-demo/saga-servicecomb-demo/scb-car/src/main/resources/microservice.yaml new file mode 100644 index 0000000..b6dbdbe --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-car/src/main/resources/microservice.yaml @@ -0,0 +1,46 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +#More details can be found : +# 1.http://servicecomb.incubator.apache.org/users/service-definition/ +# 2.http://servicecomb.incubator.apache.org/users/service-configurations/ +# 3.http://servicecomb.incubator.apache.org/users/communicate-protocol/ + +#Indicates an application name +APPLICATION_ID: demo +service_description: +#Indicates a microservice name +#The microservice name should be unique within an application. +#The name can contain digits, uppercase and lowercase letters, hyphens(-), underscores(_), and periods(.); and can neither start nor end with punctuations. +#The naming rule is as follows: ^[a-zA-Z0-9]+$|^[a-zA-Z0-9][a-zA-Z0-9_-.]*[a-zA-Z0-9]$. + name: car +#Indicates a service version + version: 1.0.0 +servicecomb: + service: + #Specifies the service center IP address. + registry: + address: http://127.0.0.1:30100 + #Specifies the rest transport listening IP address. + rest: + address: 127.0.0.1:28080 + + #Add Saga Handler + handler: + chain: + Provider: + default: saga-provider \ No newline at end of file diff --git a/omega/omega-transport/omega-transport-servicecomb/pom.xml b/saga-demo/saga-servicecomb-demo/scb-hotel/pom.xml similarity index 64% copy from omega/omega-transport/omega-transport-servicecomb/pom.xml copy to saga-demo/saga-servicecomb-demo/scb-hotel/pom.xml index 8eb6203..3e68be4 100644 --- a/omega/omega-transport/omega-transport-servicecomb/pom.xml +++ b/saga-demo/saga-servicecomb-demo/scb-hotel/pom.xml @@ -20,28 +20,12 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> - <artifactId>omega-transport</artifactId> - <groupId>org.apache.servicecomb.saga</groupId> + <artifactId>saga-servicecomb-demo</artifactId> + <groupId>org.apache.servicecomb.saga.demo</groupId> <version>0.3.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> - <artifactId>omega-transport-servicecomb</artifactId> + <artifactId>scb-hotel</artifactId> - <dependencies> - <!-- We just need this jar for compile, the runtime should provide full dependencies --> - <dependency> - <groupId>org.apache.servicecomb</groupId> - <artifactId>java-chassis-core</artifactId> - <version>${java.chassis.version}</version> - <scope>provided</scope> - </dependency> - - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-simple</artifactId> - <scope>test</scope> - </dependency> - </dependencies> - -</project> +</project> \ No newline at end of file diff --git a/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/java/org/apache/servicecomb/saga/demo/scb/hotel/HotelApplication.java b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/java/org/apache/servicecomb/saga/demo/scb/hotel/HotelApplication.java new file mode 100644 index 0000000..ce8c0cb --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/java/org/apache/servicecomb/saga/demo/scb/hotel/HotelApplication.java @@ -0,0 +1,32 @@ +/* + * 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. + */ + +package org.apache.servicecomb.saga.demo.scb.hotel; + +import org.apache.servicecomb.saga.omega.spring.EnableOmega; +import org.apache.servicecomb.springboot.starter.provider.EnableServiceComb; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@EnableServiceComb +@EnableOmega +public class HotelApplication { + public static void main(String[] args) { + SpringApplication.run(HotelApplication.class, args); + } +} diff --git a/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/java/org/apache/servicecomb/saga/demo/scb/hotel/HotelBooking.java b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/java/org/apache/servicecomb/saga/demo/scb/hotel/HotelBooking.java new file mode 100644 index 0000000..b183a79 --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/java/org/apache/servicecomb/saga/demo/scb/hotel/HotelBooking.java @@ -0,0 +1,74 @@ +/* + * 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. + */ + +package org.apache.servicecomb.saga.demo.scb.hotel; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; +import com.fasterxml.jackson.annotation.JsonIgnore; + +@JsonAutoDetect(fieldVisibility = Visibility.ANY) +class HotelBooking { + @JsonIgnore + private Integer id; + private String name; + private Integer amount; + private boolean confirmed; + private boolean cancelled; + + Integer getId() { + return id; + } + + void setId(Integer id) { + this.id = id; + } + + String getName() { + return name; + } + + void setName(String name) { + this.name = name; + } + + Integer getAmount() { + return amount; + } + + void setAmount(Integer amount) { + this.amount = amount; + } + + boolean isConfirmed() { + return confirmed; + } + + void confirm() { + this.confirmed = true; + this.cancelled = false; + } + + boolean isCancelled() { + return cancelled; + } + + void cancel() { + this.confirmed = false; + this.cancelled = true; + } +} diff --git a/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/java/org/apache/servicecomb/saga/demo/scb/hotel/HotelBookingController.java b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/java/org/apache/servicecomb/saga/demo/scb/hotel/HotelBookingController.java new file mode 100644 index 0000000..d7d3cd3 --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/java/org/apache/servicecomb/saga/demo/scb/hotel/HotelBookingController.java @@ -0,0 +1,62 @@ +/* + * 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. + */ + +package org.apache.servicecomb.saga.demo.scb.hotel; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.servicecomb.provider.rest.common.RestSchema; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@RestSchema(schemaId = "hotel") +@RequestMapping(path = "/") +public class HotelBookingController { + @Autowired + private HotelBookingService service; + + private final AtomicInteger id = new AtomicInteger(0); + + @CrossOrigin + @GetMapping(path = "/bookings") + public List<HotelBooking> getAll() { + return new ArrayList<>(service.getAllBookings()); + } + + @PostMapping(path = "/order/{name}/{rooms}") + public HotelBooking order(@PathVariable String name, @PathVariable Integer rooms) { + HotelBooking booking = new HotelBooking(); + booking.setId(id.incrementAndGet()); + booking.setName(name); + booking.setAmount(rooms); + service.order(booking); + return booking; + } + + @DeleteMapping(path = "/bookings") + public void clear() { + service.clearAllBookings(); + id.set(0); + } +} diff --git a/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/java/org/apache/servicecomb/saga/demo/scb/hotel/HotelBookingService.java b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/java/org/apache/servicecomb/saga/demo/scb/hotel/HotelBookingService.java new file mode 100644 index 0000000..4d4d550 --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/java/org/apache/servicecomb/saga/demo/scb/hotel/HotelBookingService.java @@ -0,0 +1,54 @@ +/* + * 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. + */ + +package org.apache.servicecomb.saga.demo.scb.hotel; + +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.servicecomb.saga.omega.transaction.annotations.Compensable; +import org.springframework.stereotype.Service; + +@Service +class HotelBookingService { + private Map<Integer, HotelBooking> bookings = new ConcurrentHashMap<>(); + + @Compensable(compensationMethod = "cancel") + void order(HotelBooking booking) { + if (booking.getAmount() > 2) { + throw new IllegalArgumentException("can not order the rooms large than two"); + } + booking.confirm(); + bookings.put(booking.getId(), booking); + } + + void cancel(HotelBooking booking) { + Integer id = booking.getId(); + if (bookings.containsKey(id)) { + bookings.get(id).cancel(); + } + } + + Collection<HotelBooking> getAllBookings() { + return bookings.values(); + } + + void clearAllBookings() { + bookings.clear(); + } +} diff --git a/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/resources/application.yaml b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/resources/application.yaml new file mode 100644 index 0000000..333f732 --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/resources/application.yaml @@ -0,0 +1,22 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +spring: + application: + name: hotel +alpha: + cluster: + address: 192.168.99.100:8080 diff --git a/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/resources/log4j2.xml b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/resources/log4j2.xml new file mode 100644 index 0000000..cae04cb --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/resources/log4j2.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> + +<Configuration status="WARN"> + <Appenders> + <Console name="Console" target="SYSTEM_OUT"> + <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> + </Console> + </Appenders> + <Loggers> + <AsyncRoot level="info"> + <AppenderRef ref="Console"/> + </AsyncRoot> + </Loggers> +</Configuration> diff --git a/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/resources/microservice.yaml b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/resources/microservice.yaml new file mode 100644 index 0000000..51d8133 --- /dev/null +++ b/saga-demo/saga-servicecomb-demo/scb-hotel/src/main/resources/microservice.yaml @@ -0,0 +1,46 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +#More details can be found : +# 1.http://servicecomb.incubator.apache.org/users/service-definition/ +# 2.http://servicecomb.incubator.apache.org/users/service-configurations/ +# 3.http://servicecomb.incubator.apache.org/users/communicate-protocol/ + +#Indicates an application name +APPLICATION_ID: demo +service_description: +#Indicates a microservice name +#The microservice name should be unique within an application. +#The name can contain digits, uppercase and lowercase letters, hyphens(-), underscores(_), and periods(.); and can neither start nor end with punctuations. +#The naming rule is as follows: ^[a-zA-Z0-9]+$|^[a-zA-Z0-9][a-zA-Z0-9_-.]*[a-zA-Z0-9]$. + name: hotel +#Indicates a service version + version: 1.0.0 +servicecomb: + service: + #Specifies the service center IP address. + registry: + address: http://127.0.0.1:30100 + #Specifies the rest transport listening IP address. + rest: + address: 127.0.0.1:38080 + + #Add Saga Handler + handler: + chain: + Provider: + default: saga-provider \ No newline at end of file