-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: chengyouling <spadgerlin888@163.com>
- Loading branch information
1 parent
ead5ee2
commit 93ded5d
Showing
13 changed files
with
431 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
48 changes: 48 additions & 0 deletions
48
mq-gray-demo/rocketmq-gray-demo/rocketmq-gray-consumer-demo/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
38 changes: 38 additions & 0 deletions
38
...rc/main/java/io/sermant/demo/grayscale/rocketmq/consumer/RocketMqConsumerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...mo/src/main/java/io/sermant/demo/grayscale/rocketmq/consumer/RocketMqMessageListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...y-demo/rocketmq-gray-demo/rocketmq-gray-consumer-demo/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "*" |
48 changes: 48 additions & 0 deletions
48
mq-gray-demo/rocketmq-gray-demo/rocketmq-gray-producer-demo/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
67 changes: 67 additions & 0 deletions
67
...-demo/src/main/java/io/sermant/demo/grayscale/rocketmq/producer/MqProducerController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
Oops, something went wrong.