springboot整合mybatis和rabbitMQ的一個小例子
消息生產者服務搭建
springboot整合mybatis和rabbitMQ的框架搭建非常簡單,分三步,第一步使用idea工具創建一個springboot工程,第二步在application.yml文件中配置相關信息,第三步在springboot啓動類上面加註解。
創建工程好之後,將application.properties改爲application.yml(個人喜好yml文件,當然喜歡什麼用什麼),添加如下配置
- template:有關
AmqpTemplate
的配置- exchange:缺省的交換機名稱,此處配置後,發送消息如果不指定交換機就會使用這個
spring:
datasource:
#driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: 123456
rabbitmq:
host: 127.0.0.1
username: guest
password: guest
virtual-host: /
template:
exchange: LSL.EXCHANGE
mybatis:
type-aliases-package: com.lsl.rabbitmq.topic.pojo
server:
port: 8082
以上環境就搭建好了,我們可以把controller包,dao包,pojo包,service包都創建好
編寫各層代碼
創建user的pojo類
public class User implements Serializable {
private Integer id;
private String username;
private String password;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
'}';
}
編寫controller代碼
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/update/{id}")
public String updateUser(@PathVariable("id")Integer id){
userService.updateUser(id);
return "更新成功";
}
}
編寫service代碼
@Service
public class UserService {
@Autowired
private AmqpTemplate amqpTemplate;
@Autowired
private UserDao userDao;
public void updateUser(Integer id){
User user=userDao.queryUserById(id);
user.setUsername("秦雁回");
user.setPassword("1234556");
user.setName("阿盧");
userDao.updateUser(user);
sendMsg("update",user);
}
private void sendMsg(String type, User user){
try {
amqpTemplate.convertAndSend("user."+type, JSON.toJSONString(user));
}catch (Exception e){
e.printStackTrace();
}
}
}
這裏沒有指定交換機,因此默認發送到了配置中的:LSL.EXCHANGE
注意:這裏要把所有異常都try起來,不能讓消息的發送影響到正常的業務邏輯
我們要明白消息什麼時候需要發,當對user進行寫操作:增、刪、改的時候,需要發送一條消息,通知其它服務。
發送什麼內容?對user的增刪改時其它服務可能需要新的user數據,但是如果消息內容中包含全部user信息,數據量可能太大,而且並不是每個服務都需要全部的信息。因此我們可以只發送user的id,其它服務可以根據id查詢自己需要的信息。但是後面我創建的服務不涉及對數據庫的操作,我這裏就發送了user的全部信息。使用JSON對象需要引入依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
編寫dao代碼
public interface UserDao {
@Delete("delete from user where id=#{id}")
void deleteUserById(Integer id);
@Update("update user set username=#{username},password=#{password},name=#{name} where id=#{id}")
void updateUser(User user);
@Select("select * from user where id=#{id}")
User queryUserById(Integer id);
}
如上我們就完成了一個消息生產者的服務接口的編寫
消息消費者服務搭建
消息消費者項目同樣使用idea工具創建一個springboot工程,引入的依賴爲web和rabbitMQ就可以了;
在application.yml文件中添加如下配置
spring:
rabbitmq:
host: 127.0.0.1
username: guest
password: guest
virtual-host: /
創建一個UserListener類和User的pojo類
UserListener.class代碼
@Component
public class UserListener {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "LSL.USER.UPDATE.QUEUE",durable = "true"),
exchange = @Exchange(value = "LSL.EXCHANGE",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
key = "user.update"
))
public void updateUserListener(String userJson){
if (userJson==null){
return;
}
User user=JSON.parseObject(userJson, new TypeReference<User>(){});
System.out.println(user);
}
}
要使用JSON對象需要引入fastjson依賴,上面有座標
user類與消息生產者中的user類一致,可以直接拷貝到消息消費者中
如上我們就完成了消息消費者項目的搭建與代碼編寫,當我們啓動兩個服務,啓動rabbitMQ服務之後,在瀏覽器輸入http://localhost:8082/user/update/1後,就會去數據庫更新user表中id爲1的user信息,當消息生產者服務中id爲1的user信息變化之後,消息消費者服務中會拿到更新後的id爲1的user信息,即完成了數據同步。
啓動服務
啓動rabbitMQ服務
啓動兩個服務
更新前id爲1的user信息
在瀏覽器輸入http://localhost:8082/user/update/1
消息消費者拿到更新後的id爲1的user信息
數據庫中id爲1的user信息
博主第一次寫博客,懷着緊張的心情寫完整篇,我知道其中的內容還有很多不夠詳細,歡迎各位大佬批評指正