使用RabbitMq提高吞吐量
一直知道消息队列可以提高吞吐量,那具体可以提高多少呢?
下面来看看!
创建项目
消费者
BookController
@RestController
public class BookController {
@Autowired
private BookMapper bookMapper;
@GetMapping("/add")
public String addBook(){
bookMapper.insert(new Book(null,"h","hhh"));
return "success";
}
}
BookQueue
@Component
@RabbitListener(queues = "book")
public class BookQueue {
@Autowired
private BookMapper bookMapper;
@RabbitHandler
public void addBook(Book book){
bookMapper.insert(book);
}
}
ConsumerApplication
@SpringBootApplication
@EnableRabbit // 开启监听器需要该注解
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
application.yaml
server:
port: 8082
spring:
profiles:
include: mapper
rabbitmq:
host: 192.168.92.100
port: 5672
username: guest
password: guest
virtual-host: /
mapper(持久层模块)
Book
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book implements Serializable {
private Integer id;
private String name;
private String src;
}
BookMapper
@Mapper
public interface BookMapper {
void insert(Book book);
}
BookMapper.xml
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liu.mapper.BookMapper">
<insert id="insert" parameterType="com.liu.entity.Book">
insert into book (id, name, src) values (null, #{name}, #{src});
</insert>
</mapper>
application-mapper.yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/python
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
生产者
BookController
@RestController
public class BookController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/add")
public String addBook() {
Book book = new Book(null, "hh", "hh");
rabbitTemplate.convertAndSend("","book", book);
return "success";
}
}
ProducerApplication
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
application.yaml
spring:
profiles:
include: mapper # 导入mapper模块
rabbitmq:
host: 192.168.92.100
port: 5672
username: guest
password: guest
virtual-host: /
server:
port: 8081