-
pom文件(这是我的所有依赖,非必选)
-
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.sample</groupId> <artifactId>serviceInterface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.sample</groupId> <artifactId>common-entity</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.sample</groupId> <artifactId>mapper</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.dangdang</groupId> <artifactId>sharding-jdbc-core</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <!--<artifactId>spring-boot-starter-redis</artifactId>--> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <!-- rabbitMQ --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
如果加入到自己工程后发现有的类找不到,一般会是jar包冲突导致,请检查依赖中是否有amqp-client和spring-rabbit的jar包,如果有者排除后使用spring-boot-starter-amqp就好,因为Springfarmework的ampq相当于对com.rabbitmq.amqp-client进行了封装,一定要确保两个版本匹配
-
启动类
@SpringBootApplication
public class ProviderApp {
public static void main(String[] args) throws Exception {
SpringApplication.run(ProviderApp.class, args);
}
}
-
application.yml
-
spring: rabbitmq: host: 192.168.119.10 port: 5672 username: admin password: 123456 virtual-host: /test
rabbitMQ的配置类
package com.sample.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 在此配置只是为了程序打开的时候不报错且能正常使用
* 申明队列、交换机以及队列和交换机绑定操作强烈建议到rabbitMQ的管理后台进行操作!!!这样可以实现系统间的彻底解藕
* http://192.168.119.10:15672/#/
* @author admin
*
*/
@Configuration
public class RabbitMQConf {
@Bean(name="message")
public Queue queueMessage() {
return new Queue("topic.message");
}
@Bean(name="messages")
public Queue queueMessages() {
return new Queue("topic.messages");
}
@Bean
public TopicExchange exchange() {
return new TopicExchange("testTopicExchange");
}
@Bean
public Binding bindingExchangeMessage(@Qualifier("message") Queue queueMessage, TopicExchange exchange) {
return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
}
@Bean
public Binding bindingExchangeMessages(@Qualifier("messages") Queue queueMessages, TopicExchange exchange) {
return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");//*表示一个词,#表示零个或多个词
}
}
-
发送消息
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Sender {
@Autowired
private AmqpTemplate template;
public String send(String exchange,String routingKey,String msg) {
template.convertAndSend(exchange,routingKey, msg);
return msg;
}
}
-
接收消息
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class Receive {
@RabbitListener(queues="topic.message")
public void receive1(String str) {
System.out.println("receive1:"+str);
}
@RabbitListener(queues="topic.messages")
public void receive2(String str) {
System.out.println("receive2:"+str);
}
}
-
controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sample.rabbitMQ.Sender;
@Controller
public class RabbitMQSenderController {
@Autowired
private Sender rabbitMQSender;
@ResponseBody
@RequestMapping("/rabbitMQSender/{routingKey}/{msg}")
public String test(@PathVariable String routingKey,@PathVariable String msg) {
String exchange="testTopicExchange";
String say = rabbitMQSender.send(exchange,routingKey, msg);
return say;
}
}
-
启动springboot并测试
http://localhost:8089/rabbitMQSender/topic.message.xyz/xx