前言
項目運行前需安裝好RocketMQ與環境變量配置,對Rocket相關知識不熟悉的可按照以下文章順序後再閱讀該文:
註冊中心(eureka)模塊
-
依賴配置:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
-
application.yml(正常情況建議eureka放到bootstrap.yml中):
server: port: 8000 eureka: client: service-url: defaultZone: http://localhost:${server.port}/eureka fetch-registry: false register-with-eureka: false spring: application: name: registry-center
-
EurekaApplication
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class); } }
配置中心模塊
-
依賴配置:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
-
application.yml
server: port: 8001 spring: application: name: config-center
-
bootstrap.yml
spring: cloud: config: server: git: uri: https://gitee.com/Wilson-He/spring-boot-series.git username: Wilson-He force-pull: true password: # 配置文件在uri下的目錄 search-paths: /config-server-properties eureka: client: service-url: defaultZone: http://localhost:8000/eureka
-
ConfigServerApplication
@SpringBootApplication @EnableDiscoveryClient @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
-
配置文件
在{spring.cloud.git.uri去.git}/{spring.cloud.git.search-paths}下添加配置文件,如上例添加的配置文件目錄路徑爲https://gitee.com/Wilson-He/spring-boot-series下的config-server-properties目錄,在項目中添加配置文件rocketmq-test.properties:rocketmq.topic=spring-boot-topic rocketmq.name-server=127.0.0.1:9876 rocketmq.topic.user=spring-cloud-user-topic rocketmq.topic.order=spring-cloud-order-topic rocketmq.consumer-group.user=spring-cloud-user-group rocketmq.consumer-group.order=spring-cloud-order-group
生產者模塊
-
依賴配置
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-spring-boot-starter</artifactId> </dependency> </dependencies>
-
application.yml
server: port: 9990 spring: application: name: spring-cloud-producer rocketmq: producer: group: spring-cloud-producer name-server: 127.0.0.1:9876
-
bootstrap.yml
eureka: client: service-url: defaultZone: http://localhost:8000/eureka spring: cloud: config: discovery: enabled: true service-id: config-center # 加載配置中心目錄下的資源文件rocketmq-test.properties name: rocketmq profile: test
-
CloudProducerApplication
@SpringBootApplication @EnableDiscoveryClient public class CloudProducerApplication { public static void main(String[] args) { SpringApplication.run(CloudProducerApplication.class, args); } }
-
MessageSenderController
@RestController @RequestMapping("/send") public class MessageSenderController { @Resource private RocketMQTemplate rocketMQTemplate; @Value("${rocketmq.topic.user}") private String userTopic; @Value("${rocketmq.topic.order}") private String orderTopic; @GetMapping("/user") public String sendUser(@RequestParam String msg) { rocketMQTemplate.send(userTopic, MessageBuilder.withPayload(msg).build()); return "send user success"; } @GetMapping("/order") public String sendOrder(@RequestParam String msg) { rocketMQTemplate.send(orderTopic, MessageBuilder.withPayload(msg).build()); return "send order success"; }
消費者模塊
-
application.yml
server: port: 10000 spring: application: name: spring-cloud-consumer
由於配置了註冊中心,
@RocketMQMessageListener
註解的name-server
默認配置值爲${rocketmq.name-server:}
,所以只要配置中心含rocketmq.name-server
的配置即可省略consumer的name-server配置,前文的rocketmq-test.properties已配置了該屬性。 -
bootstrap.yml文件同Producer
-
消息監聽器
-
OrderRocketMQListener
@Slf4j @RocketMQMessageListener(consumerGroup = "${rocketmq.consumer-group.order}", topic = "${rocketmq.topic.order}") @Service public class OrderRocketMQListener implements RocketMQListener<MessageExt> { @Override public void onMessage(MessageExt message) { log.info("order msg id: {}, payload: {}", message.getMsgId(), new String(message.getBody())); } }
-
UserRocketMQListener
@Slf4j @RocketMQMessageListener(consumerGroup = "${rocketmq.consumer-group.user}", topic = "${rocketmq.topic.user}") @Service public class UserRocketMQListener implements RocketMQListener<MessageExt> { @Override public void onMessage(MessageExt message) { log.info("user msg id: {}, payload: {}", message.getMsgId(), new String(message.getBody())); } }
-
CloudConsumerApplication
@SpringBootApplication @EnableDiscoveryClient public class CloudConsumerApplication { public static void main(String[] args) { SpringApplication.run(CloudConsumerApplication.class, args); } }
-
運行測試
- 運行NameServer:
mqnamesrv
- 運行Broker:
mqbroker -n 127.0.0.1:9876
- 運行EurekaApplication
- 運行ConfigServerApplication
- 運行CloudConsumerApplication
- 運行CloudProducerApplication
- 打開
http://localhost:8000
查看應用服務是否已成功註冊到註冊中心
- 調用CloudProducerApplication信息發送接口進行測試
http://localhost:9990/send/user?msg=user msg
http://localhost:9990/send/order?msg=order msg
可以看到CloudConsumerApplication控制檯打印出Listener接收到的消息:
附
項目地址:spring-rocketmq