暫時只介紹Work Queue球拦。
其他的工作模式其實都差不多? 可以試著自己往里面加.?
這只是個簡單架子译蒂。?
使用springboot 2.x 版本.
pom文件.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version></properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<encoding>UTF-8</encoding>
<configLocation>xml/google_checks.xml</configLocation> </configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
配置文件 application.properties?
spring.rabbitmq.host=
spring.rabbitmq.port=
spring.rabbitmq.username=
spring.rabbitmq.password=
spring.rabbitmq.virtual-host=
spring.rabbitmq.connection-timeout=
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-returns=true
spring.rabbitmq.listener.simple.acknowledge-mode=manual? ?//實際使用必須 手動Ack 原因自查.
寫入一個java類用來配置rabbitmq
import org.springframework.amqp.core.*;? ?
import org.springframework.amqp.rabbit.core.RabbitTemplate;?
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;?
import org.springframework.beans.factory.annotation.Autowired;?
import org.springframework.context.annotation.Bean;?
import org.springframework.context.annotation.Configuration;?
?import java.text.SimpleDateFormat;?
import java.util.Date;
/**
*rabbitmq 配置文件
*/
@Configuration
public class RabbitConfig {
//自動注入RabbitTemplate模板類
? ? @Autowired
? ? private RabbitTemplaterabbitTemplate;
/**
? ? * 模版類定義
? ? * Jackson消息轉換器
? ? * ConfirmCallback接口用于實現(xiàn)消息發(fā)送到RabbitMQ交換器后接收ack回調? 即消息發(fā)送到exchange? ack
? ? * ReturnCallback接口用于實現(xiàn)消息發(fā)送到RabbitMQ交換器俏讹,但無相應隊列與交換器綁定時的回調? 即消息發(fā)送不到任何一個隊列中? ack
? ? * @return? amqp template
*/
? ? @Bean
? ? public AmqpTemplate amqpTemplate() {
// 使用jackson 消息轉換器
? ? ? ? rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
rabbitTemplate.setEncoding("UTF-8");
// 開啟returncallback? ? properties 需要 配置publisher-returns: true
? ? ? ? rabbitTemplate.setMandatory(true);
rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
String correlationId = message.getMessageProperties().getCorrelationId();
});
//? 消息確認? properties 需要配置publisher-returns: true
? ? ? ? rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
if (ack) {
SimpleDateFormat df =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
// 輸出字符串
? ? ? ? ? ? ? ? System.out.println("時間"+df.format(new Date()));
System.out.println("消息發(fā)送到exchange成功,id: "+correlationData.getId());
}else {
System.out.println("消息發(fā)送到exchange失敗,原因: "+ cause);
}
});
return rabbitTemplate;
}
/**
? ? * 聲明Direct交換機 支持持久化.
*
? ? * @return the exchange
*/
? ? @Bean
? ? public Exchange directExchange() {
return ExchangeBuilder.directExchange("exchange-1").durable(true).build();
}
/**
? ? * 聲明一個隊列 支持持久化.
? ? * @return the queue
*/
? ? @Bean
? ? public Queue directQueue() {
return QueueBuilder.durable("queue-1").build();
}
/**
? ? * 通過綁定鍵 將指定隊列綁定到一個指定的交換機 .
? ? * @param queue? ? the queue
? ? * @param exchange the exchange
? ? * @return the binding
*/
? ? @Bean
? ? public Binding directBindingA( Queue queue, Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("send").noargs();
}
}
生產(chǎn)者代碼(Controller)
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class RabbitSender {
//自動注入RabbitTemplate模板類
? @Autowired
? private RabbitTemplaterabbitTemplate;
@GetMapping("/send")
public void send()throws Exception {
String s ="123456";
//id + 時間戳 全局唯一
? ? ? CorrelationData correlationData =new CorrelationData("1234567890");
rabbitTemplate.convertAndSend("exchange-1","send", s, correlationData);
}
}
將此注解@EnableRabbit?加在@SpringBootApplication 后!
本文僅限本人小白學習參考袍镀,不足之處請大佬指正聚谁。