ES数据同步\Aop实现发送消息到MQ

基础实现:在CRUD时,数据库的数据会发生更改,但是我们es的文档数据并未同步修改,所以,在我们修改数据库数据时我们使用aop存个id到mq, 然后消费者拿到消息获取id,再用这个id新增es文档。

生产者和消费者的交换机和队列名称常量

public class MqConstants {
    /**
     * 交换机
     */
    public final static String HOTEL_EXCHANGE = "hotel.topic";
    /**
     * 监听新增和修改的队列
     */
    public final static String HOTEL_INSERT_QUEUE = "hotel.insert.queue";
    /**
     * 监听删除的队列
     */
    public final static String HOTEL_DELETE_QUEUE = "hotel.delete.queue";
    /**
     * 新增或修改的RoutingKey
     */
    public final static String HOTEL_INSERT_KEY = "hotel.insert";
    /**
     * 删除的RoutingKey
     */
    public final static String HOTEL_DELETE_KEY = "hotel.delete";
}

1.生产者

1.1导入aop和mq依赖

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

1.2application.yml配置rabbitmq

server:
  port: 8099
spring:
  datasource:
    url: 
    username: 
    password: 
    driver-class-name: com.mysql.jdbc.Driver
  rabbitmq:
    host: localhost # 主机名
    port: 5672 # 端口
    virtual-host: / # 虚拟主机
    username: guest # 用户名
    password: guest # 密码

1.3在启动类或者写一个配置类配置json转换器的bean,使用@EnableAspectJAutoProxy注解开启aop。

@MapperScan(basePackages = "cn.itcast.hotel.mapper")
@SpringBootApplication
@EnableAspectJAutoProxy
public class HotelAdminApplication {

    public static void main(String[] args) {
        SpringApplication.run(HotelAdminApplication.class, args);
    }
    
    //mq序列化
    @Bean
    public MessageConverter jsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}

1.4MQ配置类,用来声明交换机和队列并进行绑定。

@Configuration
public class MqConfig {
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(MqConstants.HOTEL_EXCHANGE, true, false);
    }

    @Bean
    public Queue insertQueue(){
        return new Queue(MqConstants.HOTEL_INSERT_QUEUE, true);
    }

    @Bean
    public Queue deleteQueue(){
        return new Queue(MqConstants.HOTEL_DELETE_QUEUE, true);
    }

    @Bean
    public Binding insertQueueBinding(){
        return BindingBuilder.bind(insertQueue()).to(topicExchange()).with(MqConstants.HOTEL_INSERT_KEY);
    }

    @Bean
    public Binding deleteQueueBinding(){
        return BindingBuilder.bind(deleteQueue()).to(topicExchange()).with(MqConstants.HOTEL_DELETE_KEY);
    }
}

1.5写一个注解,根据注解的value来判断es是增还是删

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SendMQ {
    String value();
}

1.6切面类,增强功能。

@Aspect
@Component
@Slf4j
public class SendMQAspect {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Pointcut("@annotation(cn.itcast.hotel.annotation.SendMQ)")
    public void pc1() {
    }

    @Around("pc1() && @annotation(sendMQ)")
    public void around(ProceedingJoinPoint pjp, SendMQ sendMQ) throws Throwable {
        //System.out.println("进来了");
        pjp.proceed();
        String value = sendMQ.value();
        Object args = pjp.getArgs()[0];
        if ("delete".equals(value)) {
            rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_DELETE_KEY, args);
        }
        if ("insert".equals(value)){
        	//获取实体类的getId方法
            Method getId = args.getClass().getMethod("getId");
            //执行方法获取id
            Long id = (Long) getId.invoke(args);
            //将id当做消息发送到mq
            rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE, MqConstants.HOTEL_INSERT_KEY, id);
        }
    }
}

1.7在CRUD业务上增加自定义注解

我这里使用的mybatisplus,所以在controller层添加。

@RestController
@RequestMapping("hotel")
public class HotelController {

    @Autowired
    private IHotelService hotelService;

    @PostMapping
    @SendMQ(value = "insert")
    public void saveHotel(@RequestBody Hotel hotel){
        hotelService.save(hotel);
    }

    @SendMQ(value = "insert")
    @PutMapping()
    public void updateById(@RequestBody Hotel hotel){
        if (hotel.getId() == null) {
            throw new InvalidParameterException("id不能为空");
        }
        hotelService.updateById(hotel);
    }

    @DeleteMapping("/{id}")
    @SendMQ(value = "delete")
    public void deleteById(@PathVariable("id") Long id) {
        hotelService.removeById(id);
    }
}

2.消费者

2.1导入es和mq依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2.2消费者配置rabbitmq地址

和生产者第二步一样

2.3 启动类更改

和生产者第三步一样,如果这里没配置,会报一个ListenerExecutionFailedException异常。还需要注册es连接的bean。

@MapperScan("cn.itcast.hotel.mapper")
@SpringBootApplication
public class HotelDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(HotelDemoApplication.class, args);
    }

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        return new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://127.0.0.1:9200")
        ));
    }

    //mq序列化
    @Bean
    public MessageConverter jsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }

}

ListenerExecutionFailedException异常原因

Listener method 'public void com.config.mq.MsgReceiver.process(java.lang.String) throw

MQ监听消息时遇到的错误,项目启动就会持续跳出来,报错会刷屏。其实仔细可以明白其意思,就是有一个公有化的监听方法参数是String类型,所以抛出异常,我传的long,但是我发送时序列化了,所以接收也要序列化。

2.4es增删

service层

public interface IHotelService extends IService<Hotel> {

    void deleteById(Long id);

    void insertById(Long id);
}

实现类

@Service
public class HotelService extends ServiceImpl<HotelMapper, Hotel> implements IHotelService {

    @Autowired
    private RestHighLevelClient client;

    @Override
    public void deleteById(Long id) {
        try {
            // 1.准备Request
            DeleteRequest request = new DeleteRequest("hotel", id.toString());
            // 2.发送请求
            client.delete(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insertById(Long id) {
        try {
            // 0.根据id查询酒店数据
            Hotel hotel = getById(id);
            // 转换为文档类型
            HotelDoc hotelDoc = new HotelDoc(hotel);

            // 1.准备Request对象
            IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
            // 2.准备Json文档
            request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
            // 3.发送请求
            client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

2.5监听消息

@Component
public class HotelListener {

    @Autowired
    private IHotelService hotelService;

    /**
     * 监听酒店新增或修改的业务
     * @param id 酒店id
     */
    @RabbitListener(queues = MqConstants.HOTEL_INSERT_QUEUE)
    public void listenHotelInsertOrUpdate(Long id){
        hotelService.insertById(id);
    }

    /**
     * 监听酒店删除的业务
     * @param id 酒店id
     */
    @RabbitListener(queues = MqConstants.HOTEL_DELETE_QUEUE)
    public void listenHotelDelete(Long id){
        hotelService.deleteById(id);
    }
}

到这里就基本实现了功能,启动mq服务,去http://localhost:15672访问,可以看到交换机和队列也可以看到消息的生产和消费。
启动服务:
1.找到RabbitMQ安装所在文件夹,然后找到sbin目录。
2.使用cmd打开,输入rabbitmq-server.bat,启动RabbitMQ服务。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值