ES集群参数配置:
elasticsearch.clustername = im_es
elasticsearch.cluster.nodes = 10.**.**.**,10.**.**.**,10.**.**.**
elasticsearch.cluster.port = 9300
pom.xml:
org.elasticsearch
elasticsearch
2.4.0
org.springframework.data
spring-data-elasticsearch
2.0.4.RELEASE
@Component
@Configuration
@EnableElasticsearchRepositories
public class EsConfig {
@Value("${elasticsearch.cluster.nodes}")
private String esCluster;
@Value("${elasticsearch.clustername}")
private String esClusterName;
@Value("${elasticsearch.cluster.port}")
private int clusterPort;
private List list = new ArrayList<>();
@Bean
public Client client() throws Exception {
Settings esSettings = Settings.settingsBuilder().put("cluster.name", esClusterName)
.put("client.transport.sniff", true).build();
return TransportClient.builder().settings(esSettings).build()
.addTransportAddresses(getTransportAddresses());
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
return new ElasticsearchTemplate(client());
}
@Bean
public SimpleElasticsearchMappingContext elasticsearchMappingContext() {
return new SimpleElasticsearchMappingContext();
}
private InetSocketTransportAddress[] getTransportAddresses() throws UnknownHostException {
if (StringUtils.isNotBlank(esCluster)) {
String[] urllist = esCluster.split(",");
for(String str : urllist){
InetSocketTransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName(str), clusterPort);
list.add(transportAddress);
}
}
return list.toArray(new InetSocketTransportAddress[list.size()]);
}
}
消息实体:
public class Message implements Serializable{
private static final long serialVersionUID = 1L;
private String fromUid;
private String toUid;
private String session;
private String xml;
private String msgId;
private long msgTime;
private int source;
private String resource;
public Message() {
}
public Message(String id, String fromUid, String toUid, String session, String xml, String msgId, long msgTime,
int source, String resource) {
super();
this.fromUid = fromUid;
this.toUid = toUid;
this.session = session;
this.xml = xml;
this.msgId = msgId;
this.msgTime = msgTime;
this.source = source;
this.resource = resource;
}
public String getFromUid() {
return fromUid;
}
public void setFromUid(String fromUid) {
this.fromUid = fromUid;
}
public String getToUid() {
return toUid;
}
public void setToUid(String toUid) {
this.toUid = toUid;
}
public String getSession() {
return session;
}
public void setSession(String session) {
this.session = session;
}
public String getXml() {
return xml;
}
public void setXml(String xml) {
this.xml = xml;
}
public String getMsgId() {
return msgId;
}
public void setMsgId(String msgId) {
this.msgId = msgId;
}
public long getMsgTime() {
return msgTime;
}
public void setMsgTime(long msgTime) {
this.msgTime = msgTime;
}
public int getSource() {
return source;
}
public void setSource(int source) {
this.source = source;
}
public String getResource() {
return resource;
}
public void setResource(String resource) {
this.resource = resource;
}
@Override
public String toString() {
return "Message [fromUid=" + fromUid + ", toUid=" + toUid + ", session=" + session + ", xml="
+ xml + ", msgId=" + msgId + ", msgTime=" + msgTime + ", source=" + source + ", resource=" + resource
+ "]";
}
}
rabbitmq.vhost=robot
rabbitmq.vhost=robot
rabbitmq.host=192.168.***.***
rabbitmq.username=robot
rabbitmq.password=robot_test
rabbitmq.port=5672
rabbitmq.exchange=cm_customer_status
rabbitmq.routingKey=im.status
rabbitmq.wx.store.to.es.exchange=wx.store.to.es.exchange
rabbitmq.wx.store.to.es.routingkey=wx.store.to.es.routingkey
rabbitmq.wx.store.to.es.queue=wx.store.to.es.queue
@Component
public class WeChatMessageEsListener implements InitializingBean, DisposableBean, Consumer.StringMessageConsumer {
private static final Logger logger = LoggerFactory.getLogger(WeChatMessageEsListener.class);
@Value("${rabbitmq.host}")
private String host;
@Value("${rabbitmq.port}")
private int port;
@Value("${rabbitmq.vhost}")
private String vhost;
@Value("${rabbitmq.username}")
private String username;
@Value("${rabbitmq.password}")
private String password;
@Value("${rabbitmq.wx.store.to.es.queue}")
private String queue;
@Value("${rabbitmq.durable:true}")
private boolean durable = true;
@Value("${rabbitmq.wx.store.to.es.exchange}")
private String exchange;
@Value("${rabbitmq.wx.store.to.es.routingkey}")
private String routingKey;
@Value("${rabbitmq.consume.threads:10}")
private int consumeThreads = 5;
@Value("${rabbitmq.consume.basicqos:10}")
private int basicQos = 10;
private Consumer consumer;
private static final int threads = 5;
@Resource
private WeChatEsMessageHandler weChatEsMessageHandler;
@Override
public void consumer(String s) {
try {
weChatEsMessageHandler.handle(s);
} catch (Exception e) {
logger.error("consumer wechat message store to es error, the error is {}", e.getMessage(), e);
}
}
@Override
public void destroy() throws Exception {
consumer.shutdown();
}
@Override
public void afterPropertiesSet() throws Exception {
consumer = new RabbitmqConsumer.Builder()
.withHost(host)
.withPort(port)
.withVirtualhost(vhost)
.withUsername(username)
.withPassword(password)
.withDirectExchange(exchange, Arrays.asList(routingKey))
.withQueue(queue)
.withDurable(durable)
.withConsumeThreads(threads)
.withBasicQos(basicQos)
.build();
consumer.addConsumer(this);
consumer.start();
}
}
消息存放:
/**
* @author:
* @Date: 2018/8/6
* @Time: 下午3:09
* @Description: 微信消息存储到es
*/
@Component
public class WeChatEsMessageHandler implements InitializingBean, DisposableBean {
private ThreadPoolExecutor threadPool;
@Resource
private MessageESService messageESService;
@Override
public void destroy() throws Exception {
threadPool.shutdownNow();
}
@Override
public void afterPropertiesSet() throws Exception {
threadPool = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100),
new ThreadPoolExecutor.DiscardPolicy());
}
public void handle(String s) {
// msg converter
Message message = JSONObject.parseObject(s, Message.class);
if (message != null) {
messageESService.save(message);
}
}
}
@Service
public class MessageESServiceImpl implements MessageESService{
private static Logger logger = LoggerFactory.getLogger(MessageESServiceImpl.class);
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
public boolean save(Message msg) {
if(msg == null ){
return false;
}
List queries = new ArrayList();
IndexQuery indexQuery = null;
if(MesssageSource.WX_C_MSG == MesssageSource.getMsgType(msg.getSource())){
indexQuery = new IndexQueryBuilder().withIndexName(IndexAndTypeConstant.WX_C_INDEX)
.withType(IndexAndTypeConstant.WX_C_TYPE).withObject(msg).build();
}
queries.add(indexQuery);
elasticsearchTemplate.bulkIndex(queries);
return true;
}
public Message findOne(String id) {
// TODO Auto-generated method stub
return null;
}
}