CombinedMq Spring Boot可以帮助您将CombinedMq集成到Spring Boot中,通过application.yml配置和使用注解的方式,能够非常方便的完成集成工作。
使用注解的方式来使用CombinedMq,会涉及到四个注解:
- @Queue - 标注在一个接口类型上
- @Producer - 标注在类的成员变量上,该变量应该是一个由@Queue标注的接口,用来发送消息
- @Consumer - 标注在一个由@Queue标注的接口的实现类上,用来接收消息
- @EnableCombinedMq - 指定basePackages,开启对@Queue注解进行扫描的功能
<dependency>
<groupId>com.github.combinedmq</groupId>
<artifactId>combinedmq-spring-boot-starter</artifactId>
<version>1.0.2</version>
</dependency>
该接口的所有方法返回类型都只能是void类型:
@Queue(name = "x.y.z")
public interface GreetingService {
void sayHi(String name);
}
- application.yml配置
combinedmq:
rabbitmq:
host: 10.1.7.22
port: 5672
username: xiaoyu
password: xiaoyu
virtualHost: /
consumer-listener:
concurrency: 5
- 实现GreetingService接口
@Slf4j
@Service
@Consumer
public class GreetingServiceImpl implements GreetingService {
@Override
public void sayHi(String name) {
log.info("接收消息: {}", name);
}
}
- 启动Spring Boot Consumer程序
@EnableCombinedMq({"combinedmq"})
@SpringBootApplication
public class ConsumerMain {
public static void main(String[] args) {
new SpringApplicationBuilder(ConsumerMain.class).web(false).run(args);
}
}
- application.yml配置
combinedmq:
rabbitmq:
host: 10.1.7.22
port: 5672
username: xiaoyu
password: xiaoyu
virtualHost: /
producer-pool:
maxTotal: 100
maxIdle: 20
minIdle: 10
maxWaitMillis: 30000
minEvictableIdleTimeMillis: 60000
timeBetweenEvictionRunsMillis: 30000
testOnBorrow: false
testOnReturn: false
testWhileIdle: true
- 使用@Producer注入GreetingService
@Slf4j
@RestController
public class GreetingController {
@Producer(delayMillis = 5000)
private GreetingService greetingService;
@RequestMapping("sayHi/{name}")
public String sayHi(@PathVariable String name) {
log.info("准备发送消息");
greetingService.sayHi(name);
log.info("消息发送成功: {}", name);
return "发送成功";
}
}
- 启动Spring Boot Producer程序
@EnableCombinedMq({"combinedmq"})
@SpringBootApplication
public class ProducerMain {
public static void main(String[] args) {
SpringApplication.run(ProducerMain.class, args);
}
}
CombinedMq Spring Boot 由以下几个子模块组成: