This is an automated email from the ASF dual-hosted git repository. albumenj pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/dubbo-samples.git
The following commit(s) were added to refs/heads/master by this push: new 7518bb2d Rocketmq demo (#584) 7518bb2d is described below commit 7518bb2d4be90c54dd4df9c2b9fedbd8cd73fd22 Author: githublaohu <2372554...@qq.com> AuthorDate: Mon Nov 21 09:52:19 2022 +0800 Rocketmq demo (#584) --- dubbo-samples-rocketmq/README.md | 59 ++++++++++++++ dubbo-samples-rocketmq/pom.xml | 90 ++++++++++++++++++++++ .../dubbo/samples/rocketmq/RocketmqConsumer.java | 36 +++++++++ .../dubbo/samples/rocketmq/RocketmqProvider.java | 36 +++++++++ .../dubbo/samples/rocketmq/api/DemoService.java | 26 +++++++ .../samples/rocketmq/impl/DemoServiceImpl.java | 36 +++++++++ .../src/main/resources/log4j.properties | 25 ++++++ .../main/resources/spring/rocketmq-consumer.xml | 44 +++++++++++ .../main/resources/spring/rocketmq-provider.xml | 40 ++++++++++ pom.xml | 8 +- 10 files changed, 395 insertions(+), 5 deletions(-) diff --git a/dubbo-samples-rocketmq/README.md b/dubbo-samples-rocketmq/README.md new file mode 100644 index 00000000..225e0c0f --- /dev/null +++ b/dubbo-samples-rocketmq/README.md @@ -0,0 +1,59 @@ + +### 特性说明 + +Dubbo 自 2.7.5 版本开始支持 RocketMQ 协议。RocketMQ协议是基于RocketMQ-功能进行通信,适合需要通信信息记录或则金融场景。 + +1. [RokcetMQ基本兼容dubbo的原生dubbo协议与序列化协议[请点击]](https://dubbo.apache.org/zh/docs3-v2/java-sdk/reference-manual/protocol/dubbo/) +2. [RocketMQ官方安装教程[请点击]](https://rocketmq.apache.org/zh/docs/quickStart/02quickstart) + +### 协议配置 +最简单配置 +``` +dubbo.registry.address=nameservice://{nameservice-address} +dubbo.protocol.name=rocketmq +``` +#### 配置注册中心 + +因为RocketMQ的borker元数据都由RocketMQ管理组件name-service负责,所以RocketMQ协议的注册中心只能使用name-service +因自动创建topic方式,在分布式集群下可能有问题,所以有自动创建topic与手动创建配置两种模式 +``` +dubbo.registry.address=nameservice://localhost:9876 +dubbo.registry.parameters.route=true // 默认为true,自动创建topic,如果手动创建请设为false +``` + + +#### Topic的命令规则以及分组细节 + +topic创建规则有两种, + +- topic模式:就是以topic区别version与group + - topic命名规则是:"providers-{serviceName}-{version}-{group}-{CRC32}" +- select模式:使用RocketMQ的MessageSelector来区别version与group + - topic命名规则是:"providers-{serviceName}-{CRC32}" + - 注意:需要RocketMQ4.9.2以上版本才支持select模式 + +| 字段 | 说明 | +| --- | --- | +| serviceName | 接口名 | +| version | 版本号 | +| group | 分组名 | +| CRC32 | providers-{serviceName}-{version}-{group}或则providers-{serviceName} 的校验码,是用于防止在复杂情况下topic重复的问题 | + + +``` +dubbo.protocol.parameters.groupModel=topic // topic 是第一种模式,参数为select 为第二种模式。 +``` + +#### RocketMQ-Client配置 +生产端配置 +``` +dubbo.provider.parameters.enableMsgTrace=topic //Switch flag instance for message trace +dubbo.provider.parameters.namespace=topic //Namespace for this MQ Producer instance +dubbo.provider.parameters.customizedTraceTopic=topic //The name value of message trace topic.If you don't config,you can use the default trace topic name +``` +消费端配置 +``` +dubbo.consumer.parameters.enableMsgTrace=topic //Switch flag instance for message trace +dubbo.consumer.parameters.namespace=topic //Namespace for this MQ Producer instance +dubbo.consumer.parameters.customizedTraceTopic=topic //The name value of message trace topic.If you don't config,you can use the default trace topic name +``` \ No newline at end of file diff --git a/dubbo-samples-rocketmq/pom.xml b/dubbo-samples-rocketmq/pom.xml new file mode 100644 index 00000000..c9fd9ffe --- /dev/null +++ b/dubbo-samples-rocketmq/pom.xml @@ -0,0 +1,90 @@ +<?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: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" + xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-samples-rocketmq</artifactId> + <version>1.0-SNAPSHOT</version> + <name>dubbo-samples-rocketmq</name> + <properties> + <source.level>1.8</source.level> + <target.level>1.8</target.level> + <spring.version>4.3.16.RELEASE</spring.version> + <dubbo.version>3.0.7</dubbo.version> + <validation-api.version>1.1.0.Final</validation-api.version> + <hibernate-validator.version>4.2.0.Final</hibernate-validator.version> + <swagger.version>1.5.19</swagger.version> + <maven_checkstyle_version>3.0.0</maven_checkstyle_version> + <apache-rat-plugin.version>0.12</apache-rat-plugin.version> + <spring-boot.version>1.5.13.RELEASE</spring-boot.version> + </properties> + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-framework-bom</artifactId> + <version>${spring.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo-bom</artifactId> + <version>${dubbo.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + <dependencies> + <dependency> + <groupId>org.apache.dubbo</groupId> + <artifactId>dubbo</artifactId> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-web</artifactId> + </dependency> + <dependency> + <groupId>org.apache.dubbo.extensions</groupId> + <artifactId>dubbo-rpc-rocketmq</artifactId> + <version>1.0.3-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.dubbo.extensions</groupId> + <artifactId>dubbo-registry-nameservice</artifactId> + <version>1.0.3-SNAPSHOT</version> + </dependency> + + </dependencies> + + + <repositories> + <repository> + <id>mvnrepository</id> + <name>mvnrepository</name> + <url>https://repository.apache.org</url> + <layout>default</layout> + <releases> + <enabled>true</enabled> + </releases> + <snapshots> + <enabled>true</enabled> + </snapshots> + </repository> + </repositories> + + +</project> diff --git a/dubbo-samples-rocketmq/src/main/java/org/apache/dubbo/samples/rocketmq/RocketmqConsumer.java b/dubbo-samples-rocketmq/src/main/java/org/apache/dubbo/samples/rocketmq/RocketmqConsumer.java new file mode 100644 index 00000000..93f07860 --- /dev/null +++ b/dubbo-samples-rocketmq/src/main/java/org/apache/dubbo/samples/rocketmq/RocketmqConsumer.java @@ -0,0 +1,36 @@ +/* + * + * 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.dubbo.samples.rocketmq; + +import org.apache.dubbo.samples.rocketmq.api.DemoService; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class RocketmqConsumer { + + public static void main(String[] args) throws Exception { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/rocketmq-consumer.xml"); + context.start(); + + DemoService demoService = (DemoService) context.getBean("demoService"); + String result = demoService.sayHello("world"); + System.out.println(result); + } +} diff --git a/dubbo-samples-rocketmq/src/main/java/org/apache/dubbo/samples/rocketmq/RocketmqProvider.java b/dubbo-samples-rocketmq/src/main/java/org/apache/dubbo/samples/rocketmq/RocketmqProvider.java new file mode 100644 index 00000000..54f87405 --- /dev/null +++ b/dubbo-samples-rocketmq/src/main/java/org/apache/dubbo/samples/rocketmq/RocketmqProvider.java @@ -0,0 +1,36 @@ +/* + * + * 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.dubbo.samples.rocketmq; + +import java.util.concurrent.CountDownLatch; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class RocketmqProvider { + + public static void main(String[] args) throws Exception { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/rocketmq-provider.xml"); + context.start(); + + System.out.println("dubbo service started"); + new CountDownLatch(1).await(); + } + +} diff --git a/dubbo-samples-rocketmq/src/main/java/org/apache/dubbo/samples/rocketmq/api/DemoService.java b/dubbo-samples-rocketmq/src/main/java/org/apache/dubbo/samples/rocketmq/api/DemoService.java new file mode 100644 index 00000000..fbeb1dfe --- /dev/null +++ b/dubbo-samples-rocketmq/src/main/java/org/apache/dubbo/samples/rocketmq/api/DemoService.java @@ -0,0 +1,26 @@ +/* + * + * 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.dubbo.samples.rocketmq.api; + +public interface DemoService { + + String sayHello(String name); + +} diff --git a/dubbo-samples-rocketmq/src/main/java/org/apache/dubbo/samples/rocketmq/impl/DemoServiceImpl.java b/dubbo-samples-rocketmq/src/main/java/org/apache/dubbo/samples/rocketmq/impl/DemoServiceImpl.java new file mode 100644 index 00000000..3c9c88d9 --- /dev/null +++ b/dubbo-samples-rocketmq/src/main/java/org/apache/dubbo/samples/rocketmq/impl/DemoServiceImpl.java @@ -0,0 +1,36 @@ +/* + * + * 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.dubbo.samples.rocketmq.impl; + +import org.apache.dubbo.rpc.RpcContext; +import org.apache.dubbo.samples.rocketmq.api.DemoService; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DemoServiceImpl implements DemoService { + + public String sayHello(String name) { + System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); + return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress(); + } + +} diff --git a/dubbo-samples-rocketmq/src/main/resources/log4j.properties b/dubbo-samples-rocketmq/src/main/resources/log4j.properties new file mode 100644 index 00000000..e976f5c2 --- /dev/null +++ b/dubbo-samples-rocketmq/src/main/resources/log4j.properties @@ -0,0 +1,25 @@ +# +# +# 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. +# +# +###set log levels### +log4j.rootLogger=info, stdout +###output to the console### +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n \ No newline at end of file diff --git a/dubbo-samples-rocketmq/src/main/resources/spring/rocketmq-consumer.xml b/dubbo-samples-rocketmq/src/main/resources/spring/rocketmq-consumer.xml new file mode 100644 index 00000000..730510fe --- /dev/null +++ b/dubbo-samples-rocketmq/src/main/resources/spring/rocketmq-consumer.xml @@ -0,0 +1,44 @@ +<!-- + ~ + ~ 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. + ~ + --> + +<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" + xmlns:context="http://www.springframework.org/schema/context" + xmlns="http://www.springframework.org/schema/beans" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> + <context:property-placeholder/> + + <dubbo:application name="rocketmq-consumer"/> + + <dubbo:registry address="nameservice://localhost:9876"> + <dubbo:parameter key="route" value="falsee"/> + </dubbo:registry> + + <dubbo:protocol name="rocketmq" id="rocketmq"> + <dubbo:parameter key="groupModel" value="topic"/> + </dubbo:protocol> + + <dubbo:consumer> + <dubbo:parameter key="groupModel" value="topic"/> + </dubbo:consumer> + + <dubbo:reference id="demoService" + interface="org.apache.dubbo.samples.rocketmq.api.DemoService" protocol="rocketmq"/> +</beans> diff --git a/dubbo-samples-rocketmq/src/main/resources/spring/rocketmq-provider.xml b/dubbo-samples-rocketmq/src/main/resources/spring/rocketmq-provider.xml new file mode 100644 index 00000000..26030ba3 --- /dev/null +++ b/dubbo-samples-rocketmq/src/main/resources/spring/rocketmq-provider.xml @@ -0,0 +1,40 @@ +<!-- + ~ + ~ 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. + ~ + --> + +<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" + xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> + <context:property-placeholder/> + + <dubbo:application name="rocketmq-provider"/> + + <dubbo:registry address="nameservice://localhost:9876"> + <dubbo:parameter key="route" value="false"/> + </dubbo:registry> + + <dubbo:protocol name="rocketmq" id="rocketmq"> + <dubbo:parameter key="groupModel" value="topic"/> + </dubbo:protocol> + + <bean id="demoService" class="org.apache.dubbo.samples.rocketmq.impl.DemoServiceImpl"/> + + <dubbo:service interface="org.apache.dubbo.samples.rocketmq.api.DemoService" ref="demoService" protocol="rocketmq"/> +</beans> diff --git a/pom.xml b/pom.xml index 68cb4beb..6807b3cb 100644 --- a/pom.xml +++ b/pom.xml @@ -14,10 +14,7 @@ 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"> + --><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"> <modelVersion>4.0.0</modelVersion> <groupId>org.apache.dubbo</groupId> @@ -94,6 +91,7 @@ <module>dubbo-samples-xds</module> <module>dubbo-samples-spring-boot</module> <module>dubbo-samples-triple-reactor</module> + <module>dubbo-samples-rocketmq</module> <module>dubbo-samples-port-unification</module> </modules> @@ -112,4 +110,4 @@ </snapshots> </repository> </repositories> -</project> +</project> \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For additional commands, e-mail: notifications-h...@dubbo.apache.org