Appearance
Spring Cloud Bus 集成 RocketMQ
创建项目,引入依赖
xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo-bus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-bus</name>
<description>demo-bus</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.7.18</spring-boot.version>
<spring-cloud-alibaba.version>2021.0.6.1</spring-cloud-alibaba.version>
<spring-cloud.version>2021.0.9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-bus-rocketmq</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<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.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.demo.bus.DemoBusApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>local</id>
<properties>
<profiles.active>local</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
</profiles>
</project>
配置
yaml
spring:
profiles:
active: @profiles.active@
application:
name: @project.artifactId@
server:
port: 13002
yaml
spring:
cloud:
stream:
rocketmq:
binder:
enabled: true
name-server: 192.168.118.128:9876
# binders:
# rocketmq:
# type: rocketmq
# bindings:
# topic1-out-0:
# binder: rocketmq
# destination: topic2
# topic2-in-0:
# binder: rocketmq
# destination: topic2
# group: test-group
bus:
destination: topic2
id: ${spring.application.name}:${server.port}
enabled: true
nacos:
server-addr: ${NACOS_HOST:127.0.0.1}:8848
config:
namespace: local
group: DEFAULT_GROUP
file-extension: yml
server-addr: ${spring.cloud.nacos.server-addr}
discovery:
namespace: ${spring.cloud.nacos.config.namespace}
group: ${spring.cloud.nacos.config.group}
server-addr: ${spring.cloud.nacos.server-addr}
metadata:
Version: ${service.version}
ip: ${BUS_IP:127.0.0.1}
config:
import:
- optional:nacos:${spring.application.name}-@profiles.active@.${spring.cloud.nacos.config.file-extension}
创建自定义事件
java
package com.example.demo.bus.event;
import org.springframework.cloud.bus.jackson.RemoteApplicationEventScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@RemoteApplicationEventScan
public class BusEventConfig {
}
package com.example.demo.bus.event;
import org.springframework.cloud.bus.event.Destination;
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
public class MyEvent extends RemoteApplicationEvent {
public MyEvent() {
super();
}
public MyEvent(Object source, String originService, Destination destination) {
super(source, originService, destination);
}
public MyEvent(Object source, String originService, String destinationService) {
super(source, originService, DEFAULT_DESTINATION_FACTORY.getDestination(destinationService));
}
}
创建一个服务
java
package com.example.demo.bus.service;
import com.example.demo.bus.event.MyEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.bus.ServiceMatcher;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class PublisherService {
@Resource
private ApplicationEventPublisher applicationEventPublisher;
@Resource
protected ServiceMatcher serviceMatcher;
@Value("${spring.application.name}")
protected String applicationName;
public void publish(String message) {
// 发布事件
applicationEventPublisher.publishEvent(new MyEvent(message, serviceMatcher.getBusId(), applicationName + ":**"));
}
}
发布事件和监听事件
java
package com.example.demo.bus.controller;
import com.example.demo.bus.event.MyEvent;
import com.example.demo.bus.service.PublisherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.bus.event.AckRemoteApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@Slf4j
@RestController
@RequestMapping("/bus")
public class BusController {
@Resource
private PublisherService publisherService;
@EventListener
public void onEvent(MyEvent event) {
log.info("消息ID:{},消息内容:{},消息来源:{}", event.getId(), event.getSource(), event.getOriginService());
}
@GetMapping("/send")
public String send(){
// 发布事件
publisherService.publish("消息");
return "ok";
}
}