From 42bdb9844de2b1e61aba1d78aff60c3eb26ac80f Mon Sep 17 00:00:00 2001 From: Freeman Lau Date: Wed, 26 Jan 2022 14:48:02 +0800 Subject: [PATCH] Add rocketmq example --- spring-cloud-alibaba-examples/pom.xml | 1 + .../rocketmq-comprehensive-example/pom.xml | 48 ++++++++++++ .../RocketMQComprehensiveApplication.java | 73 +++++++++++++++++++ .../java/com/alibaba/cloud/examples/User.java | 58 +++++++++++++++ .../src/main/resources/application.yml | 28 +++++++ 5 files changed, 208 insertions(+) create mode 100644 spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/pom.xml create mode 100644 spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/src/main/java/com/alibaba/cloud/examples/RocketMQComprehensiveApplication.java create mode 100644 spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/src/main/java/com/alibaba/cloud/examples/User.java create mode 100644 spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/src/main/resources/application.yml diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index 3c3c379d5..82ffda792 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -36,6 +36,7 @@ seata-example/account-service rocketmq-example/rocketmq-consume-example rocketmq-example/rocketmq-produce-example + rocketmq-example/rocketmq-comprehensive-example spring-cloud-bus-rocketmq-example spring-cloud-alibaba-dubbo-examples spring-cloud-alibaba-sidecar-examples/spring-cloud-alibaba-sidecar-nacos-example diff --git a/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/pom.xml b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/pom.xml new file mode 100644 index 000000000..fae04aee5 --- /dev/null +++ b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/pom.xml @@ -0,0 +1,48 @@ + + + + + com.alibaba.cloud + spring-cloud-alibaba-examples + ${revision} + ../../pom.xml + + 4.0.0 + + + rocketmq-comprehensive-example + Spring Cloud Starter Stream Alibaba RocketMQ Comprehensive Example + Example demonstrating how to use rocketmq to produce, process and consume + jar + + + + + com.alibaba.cloud + spring-cloud-starter-stream-rocketmq + + + org.springframework.boot + spring-boot-starter-json + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + true + + + + + + diff --git a/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/src/main/java/com/alibaba/cloud/examples/RocketMQComprehensiveApplication.java b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/src/main/java/com/alibaba/cloud/examples/RocketMQComprehensiveApplication.java new file mode 100644 index 000000000..000cf2522 --- /dev/null +++ b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/src/main/java/com/alibaba/cloud/examples/RocketMQComprehensiveApplication.java @@ -0,0 +1,73 @@ +/* + * Copyright 2013-2022 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 java.time.Duration; +import java.util.Arrays; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import reactor.core.publisher.Flux; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.integration.support.StringObjectMapBuilder; + +/** + * @author freeman + */ +@SpringBootApplication +public class RocketMQComprehensiveApplication { + private static final Logger log = LoggerFactory + .getLogger(RocketMQComprehensiveApplication.class); + + public static void main(String[] args) { + SpringApplication.run(RocketMQComprehensiveApplication.class, args); + } + + @Bean + public Supplier> producer() { + return () -> Flux.interval(Duration.ofSeconds(2)).map(id -> { + User user = new User(); + user.setId(id.toString()); + user.setName("freeman"); + user.setMeta(new StringObjectMapBuilder() + .put("hobbies", Arrays.asList("movies", "songs")).put("age", 21) + .get()); + return user; + }).log(); + } + + @Bean + public Function, Flux> processor() { + return flux -> flux.map(user -> { + user.setId(String.valueOf( + Long.parseLong(user.getId()) * Long.parseLong(user.getId()))); + return user; + }); + } + + @Bean + public Consumer consumer() { + return num -> log.info(num.toString()); + } + +} diff --git a/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/src/main/java/com/alibaba/cloud/examples/User.java b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/src/main/java/com/alibaba/cloud/examples/User.java new file mode 100644 index 000000000..83d0c8a12 --- /dev/null +++ b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/src/main/java/com/alibaba/cloud/examples/User.java @@ -0,0 +1,58 @@ +/* + * Copyright 2013-2022 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 java.util.Map; + +/** + * @author freeman + */ +public class User { + private String id; + private String name; + private Map meta; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Map getMeta() { + return meta; + } + + public void setMeta(Map meta) { + this.meta = meta; + } + + @Override + public String toString() { + return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", meta=" + meta + + '}'; + } +} diff --git a/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/src/main/resources/application.yml b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/src/main/resources/application.yml new file mode 100644 index 000000000..be17fad2e --- /dev/null +++ b/spring-cloud-alibaba-examples/rocketmq-example/rocketmq-comprehensive-example/src/main/resources/application.yml @@ -0,0 +1,28 @@ +server: + port: 28082 +spring: + application: + name: rocketmq-comprehensive-example + cloud: + stream: + rocketmq: + binder: + name-server: 127.0.0.1:9876 + function: + definition: producer;processor;consumer + bindings: + producer-out-0: + destination: num + group: producer_group + processor-in-0: + destination: num + group: processor_group + processor-out-0: + destination: square + group: processor_group + consumer-in-0: + destination: square + group: consumer_group +logging: + level: + org.apache.rocketmq: debug \ No newline at end of file