直接上代码
参考文档:阿里云rocketmq
配置文件:
rocketmq:
access-key: // AccessKeyId 阿里云身份验证,在阿里云用户信息管理控制台获取。
secret-key: // AccessKeySecret 阿里云身份验证,在阿里云用户信息管理控制台获取。
consume-thread-nums: //消费者线程数
wangfa-name-srv-addr: // 设置TCP接入域名,进入消息队列RocketMQ版控制台实例详情页面的接入点区域查看。
wangfa-group-id: // 您在控制台创建的Group ID。
wangfa-topic: // 普通消息所属的Topic,切勿使用普通消息的Topic来收发其他类型的消息。
wangfa-tag: // Message Tag可理解为Gmail中的标签,对消息进行再归类,方便Consumer指定过滤条件在消息队列RocketMQ版的服务器过滤。
// Tag的具体格式和设置方法,请参见Topic与Tag最佳实践。
参数工具类
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Properties;
@Data
@Configuration
public class RocketmqProperties {
@Value("${rocketmq.access-key}")
private String accessKey;
@Value("${rocketmq.secret-key}")
private String secretKey;
@Value("${rocketmq.consume-thread-nums}")
private String consumeThreadNums = "20";
public Properties newMqProperties() {
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.AccessKey, this.accessKey);
properties.setProperty(PropertyKeyConst.SecretKey, this.secretKey);
properties.setProperty(PropertyKeyConst.ConsumeThreadNums, this.consumeThreadNums);
return properties;
}
}
订阅消息:
import com.aliyun.openservices.ons.api.