# springboot-ES-MSQL-REDIS **Repository Path**: neal_zero/springboot-es-msql-redis ## Basic Information - **Project Name**: springboot-ES-MSQL-REDIS - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-05-15 - **Last Updated**: 2021-05-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ![image-20210515125343732](readme.assets/image-20210515125343732.png) > 项目主要分为2个模块customer和search模块 > > * customer 模块为外部平台对接的总的接口模块他的作用是是直接操作MYSQL和对外暴露接口返回JSON数据给前端页面 > * 项目结构 > * config包 :配置restTemplate > * serarch 模块为ES操作模块,为customer所调用提供API接口给客户模块使用 > 增加MQ操作 ![image-20210516095138929](readme.assets/image-20210516095138929.png) >客户模块操作ES模块的时候可以使用MQ, > >客户模块操作成功后发布一个消息给MQ ~~~shell 1.客户模块导入MQ依赖 ~~~ ~~~yaml spring: # rabbitMQ rabbitmq: host: 192.168.203.142 prot: 5672 username: test password: test virtual-host: /test ~~~ ~~~sh 2.客户模块编写MQ配置类创建路由,队列,并且进行绑定 ~~~ ~~~java package com.sunzheng.customer.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.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMqConfig { //创建exchange-topic @Bean public TopicExchange getTopExchange() { return new TopicExchange("customer-api-exchange", true, false); } //创建queues @Bean public Queue getQueue(){ return new Queue("customer-queue",true,false,false,null); } //绑定que和exchange @Bean public Binding getBingd(TopicExchange topicExchange, Queue queue){ return BindingBuilder.bind(queue).to(topicExchange).with("customer.api.*"); } } ~~~ ~~~ 3.客户模块对MySql操作成功后,发布一个消息给MQ ~~~ ~~~ @Autowired private RabbitTemplate rabbitTemplate; //保存客户 @Override @Transactional public void add(Customer customer) throws IOException { //1.调用mybatis来增加customer Integer count = customerMapper.insertCustomer(customer); //2 如果没有问题开始调用ES来增加数据 log.info("数据:{}", customer); //判读失败 if (count != 1) { log.error("插入数据报错"); throw new RuntimeException("数据插入失败"); } else { //-----------------------使用MQ发送消息模式------- try{ rabbitTemplate.convertAndSend("customer-api-exchange","customer.api.save",objectMapper.writeValueAsString(customer)); } catch (Exception e){ log.error("消息发送失败",e.getMessage()); throw new RuntimeException("消息发送失败"); } } ~~~ > ES 模块 进行消息的消费,如果有相应的消息,进行相应的操作 ~~~sh 1. 导入依赖, ~~~ ~~~xml org.springframework.boot spring-boot-starter-amqp ~~~ ~~~sh 2.配置文件,增加手动ACK ~~~ ~~~yaml spring: rabbitmq: host: 192.168.203.142 prot: 5672 username: test password: test virtual-host: /test listener: simple: acknowledge-mode: manual ~~~ ~~~sh 3.编写监听组件 ~~~ ~~~java package com.sunzheng.search.listen; import com.fasterxml.jackson.databind.ObjectMapper; import com.sunzheng.search.entity.Customer; import com.sunzheng.search.server.CustomerServer; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import com.rabbitmq.client.Channel; import org.springframework.stereotype.Component; import java.io.IOException; @Component public class Customerlinster { @Autowired CustomerServer customerServer; private ObjectMapper objectMapper=new ObjectMapper(); @RabbitListener(queues = "customer-queue") public void getMessage(String data, Channel channel, Message message) throws IOException { MessageProperties messageProperties = message.getMessageProperties(); //获取送routingkey String receivedRoutingKey = messageProperties.getReceivedRoutingKey(); //根据rotingkey判断 switch (receivedRoutingKey){ case "customer.api.save": //调用server中的保存 customerServer.saveCustomer(objectMapper.readValue(data, Customer.class)); break; } channel.basicAck(message.getMessageProperties().getDeliveryTag(),false); } } ~~~