Fork me on GitHub

八.宜立方商城——Solr集群和商品信息同步

一.课程计划

1
2
3
4
5
6
7
1、solr集群搭建
2、使用solrj管理solr集群
3、把搜索功能切换到集群版
4、添加商品同步到索引库
a: Activemq
b:发送消息
c:接受消息

二.什么是SolrCloud?

SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud。当一个系统的索引数据量少的时候是不需要使用SolrCloud的,当索引量很大,搜索请求并发很高,这时需要使用SolrCloud来满足这些需求。

SolrCloud是基于Solr和Zookeeper的分布式搜索方案,它的主要思想是使用Zookeeper作为集群的配置信息中心。

它有几个特色功能:

  1. 集中式的配置信息
  2. 自动容错
  3. 近实时搜索
  4. 查询时自动负载均衡

三.Solr集群的系统架构

image

3.1物理结构

三个Solr实例( 每个实例包括两个Core),组成一个SolrCloud。

3.2逻辑结构

索引集合包括两个Shard(shard1和shard2),shard1和shard2分别由三个Core组成,其中一个Leader两个Replication,Leader是由zookeeper选举产生,zookeeper控制每个shard上三个Core的索引数据一致,解决高可用问题。

用户发起索引请求分别从shard1和shard2上获取,解决高并发问题。

3.2.1 collection

Collection在SolrCloud集群中是一个逻辑意义上的完整的索引结构。它常常被划分为一个或多个Shard(分片),它们使用相同的配置信息。

比如:针对商品信息搜索可以创建一个collection。
collection=shard1+shard2+….+shardX

3.2.2 Core

每个Core是Solr中一个独立运行单位,提供 索引和搜索服务。一个shard需要由一个Core或多个Core组成。由于collection由多个shard组成所以collection一般由多个core组成。

3.2.3 Master或Slave

Master是master-slave结构中的主结点(通常说主服务器),Slave是master-slave结构中的从结点(通常说从服务器或备服务器)。同一个Shard下master和slave存储的数据是一致的,这是为了达到高可用目的。

3.2.4 Shard

Collection的逻辑分片。每个Shard被化成一个或者多个replication,通过选举确定哪个是Leader。

3.3 需要实现的solr集群架构

image

Zookeeper作为集群的管理工具


1、集群管理:容错、负载均衡。

2、配置文件的集中管理

3、集群的入口

需要实现zookeeper 高可用。需要搭建集群。建议是奇数节点。需要三个zookeeper服务器。(因为zookeeper也有一个投票选举的机制,超过半数以上才能算好,跟redis集群不同,redis那叫容错)

搭建solr集群需要7台服务器。

搭建伪分布式:
需要三个zookeeper节点
需要四个tomcat节点。

建议虚拟机的内容1G以上。

四.环境准备

CentOS-6.5-i386-bin-DVD1.iso

jdk-7u72-linux-i586.tar.gz

apache-tomcat-7.0.47.tar.gz

zookeeper-3.4.6.tar.gz

solr-4.10.3.tgz

五.安装步骤

5.1 Zookeeper集群搭建

第一步:需要安装jdk环境。

第二步:把zookeeper的压缩包上传到服务器。
image

image

第三步:解压缩。

image

第四步:把zookeeper复制三份。

image

1
2
3
4
[root@localhost ~]# mkdir /usr/local/solr-cloud
[root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper01
[root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper02
[root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper03

第五步:在每个zookeeper目录下创建一个data目录。

image

第六步:在data目录下创建一个myid文件,文件名就叫做“myid”。内容就是每个实
例的id。例如1、2、3

image

image

image

1
2
3
4
5
6
[root@localhost data]# echo 1 >> myid
[root@localhost data]# ll
total 4
-rw-r--r--. 1 root root 2 Apr 7 18:23 myid
[root@localhost data]# cat myid
1

第七步:修改配置文件。把conf目录下的zoo_sample.cfg文件改名为zoo.cfg

image

image

1
2
3
server.1=192.168.25.154:2881:3881
server.2=192.168.25.154:2882:3882
server.3=192.168.25.154:2883:3883

image

image

image

image

image

image

image

第八步:启动每个zookeeper实例。
启动bin/zkServer.sh start

image

查看zookeeper的状态:
bin/zkServer.sh status

image

5.2 Solr集群的搭建

第一步:创建四个tomcat实例。每个tomcat运行在不同的端口。8180、8280、8380、8480

image

image

image

image

第二步:部署solr的war包。把单机版的solr工程复制到集群中的tomcat中。

image

image

image

image

image

image

image

image

第三步:为每个solr实例创建一个对应的solrhome。使用单机版的solrhome复制四份。

image

image

第四步:需要修改solr的web.xml文件。把solrhome关联起来。

image

image

image

第五步:配置solrCloud相关的配置。每个solrhome下都有一个solr.xml,把其中的ip及端口号配置好。

image

image

image

第六步:修改tomcat/bin目录下的catalina.sh 文件,关联solr和zookeeper。
把此配置添加到配置文件中:

1
JAVA_OPTS="-DzkHost=192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"

image

image

image

image

image

image

image

image

image

第七步:让zookeeper统一管理配置文件。需要把solrhome/collection1/conf目录上传到zookeeper。上传任意solrhome中的配置文件即可。

使用工具上传配置文件:/root/solr-4.10.3/example/scripts/cloud-scripts/zkcli.sh

1
./zkcli.sh -zkhost 192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183 -cmd upconfig -confdir /usr/local/solr-cloud/solrhome01/collection1/conf -confname myconf

image

image

image

image

查看zookeeper上的配置文件:

使用zookeeper目录下的bin/zkCli.sh命令查看zookeeper上的配置文件:

1
2
使用以下命令连接指定的zookeeper服务:
./zkCli.sh -server 192.168.25.154:2183

image

image

第八步:启动每个tomcat实例。要包装zookeeper集群是启动状态。

image

image

image

第九步:访问集群

image

第十步:创建新的Collection进行分片处理。

1
http://192.168.25.154:8180/solr/admin/collections?action=CREATE&name=collection2&numShards=2&replicationFactor=2

image

image

第十一步:删除不用的Collection。

1
http://192.168.25.154:8180/solr/admin/collections?action=DELETE&name=collection1

image

六.使用solrJ管理集群

6.1 添加文档

1
2
3
4
5
6
7
8
使用步骤:
第一步:把solrJ相关的jar包添加到工程中。
第二步:创建一个SolrServer对象,需要使用CloudSolrServer子类。构造方法的参数是zookeeper的地址列表。
第三步:需要设置DefaultCollection属性。
第四步:创建一SolrInputDocument对象。
第五步:向文档对象中添加域
第六步:把文档对象写入索引库。
第七步:提交。

image

image

image

image

image

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Test
public void testAddDocument() throws Exception{
//创建一个集群的连接,应该使用CloudSolrServer创建
//zkHost:zookeeper的地址列表
CloudSolrServer cloudSolrServer = new CloudSolrServer("192.168.25.128:2182,192.168.25.128:2183,192.168.25.128:2184");

//设置一个defaultCollection属性
cloudSolrServer.setDefaultCollection("collection2");

//创建一个文档对象
SolrInputDocument document= new SolrInputDocument();

//向文档中添加域
document.setField("id","solrcloud01");
document.setField("item_title","测试商品01");
document.setField("item_price",123);

//把文件写入索引库
cloudSolrServer.add(document);

//提交
cloudSolrServer.commit();
}

image

image

6.2查询文档

创建一个CloudSolrServer对象,其他处理和单机版一致。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Test
public void testQueryDocument() throws Exception{
//创建一个集群的连接,应该使用CloudSolrServer创建
//zkHost:zookeeper的地址列表
CloudSolrServer cloudSolrServer = new CloudSolrServer("192.168.25.128:2182,192.168.25.128:2183,192.168.25.128:2184");

//设置一个defaultCollection属性
cloudSolrServer.setDefaultCollection("collection2");

//创建一个查询条件
SolrQuery solrQuery = new SolrQuery();

//设置查询条件
solrQuery.setQuery("*:*");

//执行查询
QueryResponse query = cloudSolrServer.query(solrQuery);

//取查询结果
SolrDocumentList results = query.getResults();
System.out.println("总记录数:"+results.getNumFound());

//打印
for(SolrDocument solrDocument:results){
System.out.println(solrDocument.get("id"));
System.out.println(solrDocument.get("title"));
System.out.println(solrDocument.get("item_title"));
System.out.println(solrDocument.get("item_price"));
}
}

image

七.把搜索功能切换到集群版

image

image

image

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util4.2.xsd">

<!-- 单机版solr服务配置 -->
<!-- <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg name="baseURL" value="http://192.168.25.128:8080/solr"></constructor-arg>
</bean> -->
<!-- 集群版solr服务 -->
<bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
<constructor-arg name="zkHost" value="192.168.25.123:2182,192.168.25.128:2183,192.168.25.128:2184"></constructor-arg>
<property name="defaultCollection" value="collection2"></property>
</bean>
</beans>

八.同步索引库分析

方案一:在e3-manager中,添加商品的业务逻辑中,添加一个同步索引库的业务逻辑。

就是添加一次商品,就紧接着调用一次索引库的导入

缺点:业务逻辑耦合度高,业务拆分不明确

方案二:业务逻辑在e3-search中实现,调用服务在e3-manager实现。业务逻辑分开。
缺点:服务之间的耦合度变高。服务的启动有先后顺序。

方案三:使用消息队列。MQ是一个消息中间件。

image

MQ是一个消息中间件,ActiveMQ、RabbitMQ、kafka

九.ActiveMQ

9.1 什么是ActiveMQ

ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。

JMS:JAVA Message ……,JavaEE改名之前就叫J2EE

主要特点:

  1. 多种语言和协议编写客户端。语言: Java, C, C++, C#, Ruby, Perl, Python, PHP。应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
  2. 完全支持JMS1.1和J2EE 1.4规范 (持久化,XA消息,事务)
  3. 对Spring的支持,ActiveMQ可以很容易内嵌到使用Spring的系统里面去,而且也支持Spring2.0的特性
  4. 通过了常见J2EE服务器(如 Geronimo,JBoss 4, GlassFish,WebLogic)的测试,其中通过JCA 1.5 resource adaptors的配置,可以让ActiveMQ可以自动的部署到任何兼容J2EE 1.4 商业服务器上
  5. 支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
  6. 支持通过JDBC和journal提供高速的消息持久化
  7. 从设计上保证了高性能的集群,客户端-服务器,点对点
  8. 支持Ajax
  9. 支持与Axis的整合
  10. 可以很容易得调用内嵌JMS provider,进行测试

9.2 ActiveMQ的消息形式

对于消息的传递有两种类型:

一种是点对点的,即一个生产者和一个消费者一一对应;

另一种是发布/订阅模式,即一个生产者产生消息并进行发送后,可以由多个消费者进行接收。

JMS定义了五种不同的消息正文格式,以及调用的消息类型,允许你发送并接收以一些不同形式的数据,提供现有消息格式的一些级别的兼容性。

  • StreamMessage – Java原始值的数据流
  • MapMessage–一套名称-值对
  • TextMessage–一个字符串对象
  • ObjectMessage–一个序列化的 Java对象
  • BytesMessage–一个字节的数据流

十.ActiveMQ的安装

进入http://activemq.apache.org/下载ActiveMQ

使用的版本是5.12.0

10.1 安装环境:

  1. 需要jdk
  2. 安装Linux系统。生产环境都是Linux系统。

10.2 安装步骤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
第一步: 把ActiveMQ 的压缩包上传到Linux系统。
第二步:解压缩。
第三步:启动。
使用bin目录下的activemq命令启动:
[root@localhost bin]# ./activemq start
关闭:
[root@localhost bin]# ./activemq stop
查看状态:
[root@localhost bin]# ./activemq status

注意:如果ActiveMQ整合spring使用不要使用activemq-all-5.12.0.jar包。建议使用5.11.2

进入管理后台:
http://192.168.25.168:8161/admin
用户名:admin
密码:admin

image

image

image

image

image

image

image

image

image

image

十一ActiveMQ的使用方法

image

一种是点对点,一种是广播的方式

11.1 Queue

11.1.1 Producer

生产者:生产消息,发送端。
把jar包添加到工程中。使用5.11.2版本的jar包。

image

1
2
3
4
5
6
7
8
9
第一步:创建ConnectionFactory对象,需要指定服务端ip及端口号。
第二步:使用ConnectionFactory对象创建一个Connection对象。
第三步:开启连接,调用Connection对象的start方法。
第四步:使用Connection对象创建一个Session对象。
第五步:使用Session对象创建一个Destination对象(topic、queue),此处创建一个Queue对象。
第六步:使用Session对象创建一个Producer对象。
第七步:创建一个Message对象,创建一个TextMessage对象。
第八步:使用Producer对象发送消息。
第九步:关闭资源。

image

image

image

image

image

image

image

image

image

image

image

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 点到点形式发送
* @throws Exception
*/
@Test
public void testQueueProducer() throws Exception{
//1.创建一个连接工厂对象,需要指定服务的ip及端口
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.128:61616");

//2.使用工厂对象创建一个Connection对象
Connection connection = connectionFactory.createConnection();

//3.开启连接,调用Connection对象的start方法。
connection.start();

//4.创建一个Session对象
//第一个参数:是否开启事务。如果true开启事务,第二个参数将没有意义。一般不开旗事务
//第二个参数:应答模式。自动应答或者手动应答。一般自动应答。
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

//5.使用Session对象创建一个Destination对象。两种形式queue、topic,现在应该使用queue
Queue queue = session.createQueue("test-queue");

//6.使用Session对象创建一个Producer对象。
MessageProducer producer = session.createProducer(queue);

//7.创建一个Message对象,可以使用TextMessage
/*TextMessage textMessage = new ActiveMQTextMessage();
textMessage.setText("hello Activemq");*/

TextMessage textMessage = session.createTextMessage("hello activemq");

//8.发送消息
producer.send(textMessage);

//9.关闭资源
producer.close();
session.close();
connection.close();
}

11.1.2 Consumer

1
2
3
4
5
6
7
8
9
10
消费者:接收消息。
第一步:创建一个ConnectionFactory对象。
第二步:从ConnectionFactory对象中获得一个Connection对象。
第三步:开启连接。调用Connection对象的start方法。
第四步:使用Connection对象创建一个Session对象。
第五步:使用Session对象创建一个Destination对象。和发送端保持一致queue,并且队列的名称一致。
第六步:使用Session对象创建一个Consumer对象。
第七步:接收消息。
第八步:打印消息。
第九步:关闭资源

image

image

image

image

image

image

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@Test
public void testQueueConsumer() throws Exception{
//创建一个ConnectionFactory对象连接MQ服务器
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.128:61616");

//创建一个连接对象
Connection connection = connectionFactory.createConnection();

//开启连接
connection.start();

//使用Connection对象创建一个Session对象
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

//创建一个Destination对象。queue对象
Queue queue = session.createQueue("test-queue");

//使用Session对象创建一个消费者对象
MessageConsumer consumer = session.createConsumer(queue);

//接受消息
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
//打印结果
TextMessage textMessage = (TextMessage) message;
try {
String text = textMessage.getText();
System.out.println(text);
} catch (JMSException e) {
e.printStackTrace();
}
}
});
//等待接受消息
System.in.read();

//关闭资源
consumer.close();
session.close();
connection.close();
}

11.2 Topic

11.2.1 Producer

1
2
3
4
5
6
7
8
9
10
使用步骤:
第一步:创建ConnectionFactory对象,需要指定服务端ip及端口号。
第二步:使用ConnectionFactory对象创建一个Connection对象。
第三步:开启连接,调用Connection对象的start方法。
第四步:使用Connection对象创建一个Session对象。
第五步:使用Session对象创建一个Destination对象(topic、queue),此处创建一个Topic对象。
第六步:使用Session对象创建一个Producer对象。
第七步:创建一个Message对象,创建一个TextMessage对象。
第八步:使用Producer对象发送消息。
第九步:关闭资源。

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@Test
public void testTopicProducer() throws Exception{
//1.创建一个连接工厂对象,需要指定服务的ip及端口
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.128:61616");

//2.使用工厂对象创建一个Connection对象
Connection connection = connectionFactory.createConnection();

//3.开启连接,调用Connection对象的start方法。
connection.start();

//4.创建一个Session对象
//第一个参数:是否开启事务。如果true开启事务,第二个参数将没有意义。一般不开旗事务
//第二个参数:应答模式。自动应答或者手动应答。一般自动应答。
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

//5.使用Session对象创建一个Destination对象。两种形式queue、topic,现在应该使用queue
/*Queue queue = session.createQueue("test-queue");*/
Topic topic = session.createTopic("test-topic");

//6.使用Session对象创建一个Producer对象。
/*MessageProducer producer = session.createProducer(queue);*/
MessageProducer producer = session.createProducer(topic);

//7.创建一个Message对象,可以使用TextMessage
/*TextMessage textMessage = new ActiveMQTextMessage();
textMessage.setText("hello Activemq");*/

TextMessage textMessage = session.createTextMessage("topic message");

//8.发送消息
producer.send(textMessage);

//9.关闭资源
producer.close();
session.close();
connection.close();
}

image

image

image

广播消息,发了之后,就不管了,如果没有人接受,那就直接没了,有人会说,这在队列里啊,这不是队列啊。别理解错了。它只管发送,不管有没有人接,没人接就没了。

11.2.2 Consumer

1
2
3
4
5
6
7
8
9
10
消费者:接收消息。
第一步:创建一个ConnectionFactory对象。
第二步:从ConnectionFactory对象中获得一个Connection对象。
第三步:开启连接。调用Connection对象的start方法。
第四步:使用Connection对象创建一个Session对象。
第五步:使用Session对象创建一个Destination对象。和发送端保持一致topic,并且话题的名称一致。
第六步:使用Session对象创建一个Consumer对象。
第七步:接收消息。
第八步:打印消息。
第九步:关闭资源

image

image

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@Test
public void testTopicConsumer() throws Exception{
//创建一个ConnectionFactory对象连接MQ服务器
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.128:61616");

//创建一个连接对象
Connection connection = connectionFactory.createConnection();

//开启连接
connection.start();

//使用Connection对象创建一个Session对象
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

//创建一个Destination对象。queue对象
/*Queue queue = session.createQueue("test-queue");*/
Topic topic = session.createTopic("test-topic");

//使用Session对象创建一个消费者对象
MessageConsumer consumer = session.createConsumer(topic);

//接受消息
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
//打印结果
TextMessage textMessage = (TextMessage) message;
try {
String text = textMessage.getText();
System.out.println(text);
} catch (JMSException e) {
e.printStackTrace();
}
}
});

System.out.println("topic消费者1已经启动");

//等待接受消息
System.in.read();

//关闭资源
consumer.close();
session.close();
connection.close();
}

十二.全局异常处理

image

为什么配置全局异常处理?

image

image

image

12.1 处理思路

12.2 创建全局异常处理器

image

image

image

image

image

image

image

image

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* @author RickYinPeng
* @ClassName GlobalExceptionResolver
* @Description 全局异常处理
* @date 2018/11/28/11:47
*/
public class GlobalExceptionResolver implements HandlerExceptionResolver{

private static Logger logger = LoggerFactory.getLogger(GlobalExceptionResolver.class);

@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

//打印控制台
e.printStackTrace();
//写日志
logger.debug("测试输出的日志.......");
logger.info("系统发生异常了......");
logger.error("系统发生异常",e);

//发邮件、发短信
//使用jmail工具包。发短信使用第三方的WebService。

//显示错误页面
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("error/exception");

return modelAndView;
}


}

12.3 Springmvc中配置异常处理器

image