Skip to content

Commit

Permalink
新增rocketmq测试demo
Browse files Browse the repository at this point in the history
Signed-off-by: chengyouling <spadgerlin888@163.com>
  • Loading branch information
chengyouling authored and daizhenyu committed Jan 3, 2025
1 parent ead5ee2 commit 93ded5d
Show file tree
Hide file tree
Showing 13 changed files with 431 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,14 @@ jobs:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ${{ github.workspace }}/package/result/sermant-examples-xds-router-demo-${{ env.version }}.tar.gz
asset_name: sermant-examples-xds-router-demo-${{ env.version }}.tar.gz
asset_content_type: application/tar
- name: Upload Release mq-gray-demo # rocketmq-gray-demo release包
id: upload-release-mq-gray
uses: actions/upload-release-asset@v1.0.2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ${{ github.workspace }}/package/result/sermant-examples-mq-gray-demo-${{ env.version }}.tar.gz
asset_name: sermant-examples-mq-gray-demo-${{ env.version }}.tar.gz
asset_content_type: application/tar
25 changes: 25 additions & 0 deletions mq-gray-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>sermant-examples</artifactId>
<groupId>com.huaweicloud.sermant.examples</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>mq-gray-demo</artifactId>

<modules>
<module>rocketmq-gray-demo</module>
</modules>

<packaging>pom</packaging>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>
59 changes: 59 additions & 0 deletions mq-gray-demo/rocketmq-gray-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.huaweicloud.sermant.examples</groupId>
<artifactId>rocketmq-gray-demo</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-boot.version>2.7.15</spring-boot.version>
<spring-cloud.version>2021.0.9</spring-cloud.version>
<rocketmq-client.version>5.0.0</rocketmq-client.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>${rocketmq-client.version}</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-common</artifactId>
<version>${rocketmq-client.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<modules>
<module>rocketmq-gray-producer-demo</module>
<module>rocketmq-gray-consumer-demo</module>
</modules>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>rocketmq-gray-demo</artifactId>
<groupId>com.huaweicloud.sermant.examples</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>rocketmq-gray-consumer-demo</artifactId>
<version>1.0.0</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* 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 io.sermant.demo.grayscale.rocketmq.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* springboot starter
*
* @author chengyouling
* @since 2024-10-30
**/
@SpringBootApplication
public class RocketMqConsumerApplication {
/**
* main run
*
* @param args parameter
*/
public static void main(String[] args) {
SpringApplication.run(RocketMqConsumerApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* 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 io.sermant.demo.grayscale.rocketmq.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
* consumer message listener
*
* @author chengyouling
* @since 2024-10-30
**/
@Component
public class RocketMqMessageListener implements CommandLineRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(RocketMqMessageListener.class);

@Value("${rocketmq.address}")
private String nameServer;

@Value("${rocketmq.topic}")
private String topic;

@Override
public void run(String... args) throws Exception {
subscribeMessages();
}

private void subscribeMessages() throws MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("default");
consumer.setNamesrvAddr(nameServer);
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
consumer.subscribe(topic, "*");
consumer.registerMessageListener((MessageListenerOrderly) (messageExts, context) -> {
if (messageExts != null) {
for (MessageExt messageExt : messageExts) {
String message = new String(messageExt.getBody(), StandardCharsets.UTF_8);
LOGGER.info("sub message: " + message);
}
}
return ConsumeOrderlyStatus.SUCCESS;
});
consumer.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
server:
port: 9000
rocketmq:
address: 127.0.0.1:9876
topic: MESSAGE-GRAY
management:
endpoints:
web:
exposure:
include: "*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>rocketmq-gray-demo</artifactId>
<groupId>com.huaweicloud.sermant.examples</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>rocketmq-gray-producer-demo</artifactId>
<version>1.0.0</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* 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 io.sermant.demo.grayscale.rocketmq.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* producer controller
*
* @author chengyouling
* @since 2024-10-30
*/
@RestController
public class MqProducerController {
private static final Logger LOGGER = LoggerFactory.getLogger(MqProducerController.class);

@Value("${rocketmq.address}")
private String mqAddress;

@Value("${rocketmq.topic}")
private String mqTopic;

@Value("${service_meta_version:${SERVICE_META_VERSION:${service.meta.version:1.0.0}}}")
private String version;

private DefaultMQProducer defaultMQProducer;

@GetMapping("/sendMessage")
public String producerMessage(@RequestParam("message") String message) {
try {
if (defaultMQProducer == null) {
defaultMQProducer = new DefaultMQProducer("default");
defaultMQProducer.setNamesrvAddr(mqAddress);
defaultMQProducer.setSendMsgTimeout(60000);
defaultMQProducer.start();
}
String realMessage = "version " + version + " send message " + message;
Message mqMessage = new Message(mqTopic, realMessage.getBytes());
defaultMQProducer.send(mqMessage);
} catch (Exception e) {
LOGGER.error("send message error, address={}, message={}", mqAddress, message, e);
return "error";
}
return "success";
}
}
Loading

0 comments on commit 93ded5d

Please sign in to comment.