win7 x64 基于spring boot+elasticsearch+Redis+mysql+mybatis进行搜索引擎web开发--爬取IThome热评(一)

因为工作需要,我准备在win7 x64系统上基于springboot +elasticsearch+redis搭建全文索引服务器。

1. elasticsearch安装比较方便,直接去官网下载了elasticsearch 6.2.4 的msi安装包,安装过程中配置集群信息,并将路径添加到环境变量,同时将elasticsearch添加到系统服务中。这个都是在安装过程中配置的。顺便还有data目录,conf目录,logs目录等。我把elasticsearch安装到C盘,而将data目录等3个目录放到了H盘。

为了保证能识别127.0.0.1是localhost,要保证hosts文件中有一行为:

127.0.0.1 localhost

下面直接贴配置信息:

# elasticsearch.yml
bootstrap.memory_lock: false
cluster.name: oldkingESCluster
http.port: 9200
network.host: 127.0.0.1
node.data: true
node.ingest: true
node.master: true
node.max_local_storage_nodes: 1
node.name: okT400
path.data: H:\ElasticSearch\data
path.logs: H:\ElasticSearch\logs
transport.tcp.port: 9300
#jvm.options
-XX:+UseConcMarkSweepGC
-XX:CMSInitiatingOccupancyFraction=75
-XX:+UseCMSInitiatingOccupancyOnly
-XX:+AlwaysPreTouch
-Xss1m
-Djava.awt.headless=true
-Dfile.encoding=UTF-8
-Djna.nosys=true
-XX:-OmitStackTraceInFastThrow
-Dio.netty.noUnsafe=true
-Dio.netty.noKeySetOptimization=true
-Dio.netty.recycler.maxCapacityPerThread=0
-Dlog4j.shutdownHookEnabled=false
-Dlog4j2.disable.jmx=true
-Djava.io.tmpdir=${ES_TMPDIR}
-XX:+HeapDumpOnOutOfMemoryError
-Xmx1024m
-Xms1024m

直接去安装路径启动elasticsearch即可。

至于elasticsearch-head插件,可以去elasticsearch-head的GitHub上看它的readme。可以直接从chrome的商店里面找到head插件,直接安装就可以啦。

启动elasticsearch后,点chrome上的head插件,发现elasticsearch启动了:


elasticsearch-head插件的作用基本就是elasticsearch的管理,平时也基本用不上。

注意:elasticsearch安装过程中会推荐一堆插件,看着挺有用的,但是你无法选中,选中就会出现无法联网的情况,所以我最后都没有安装它推荐的那些插件,包括x-package和其他的。

2. 安装redis

直接去GitHub上下载redis的msi版安装。


安装比较简单,一路就下去了,其余的就是配置。

(1)配置redis.window.conf

maxmemory 1024000000
requirepass redis
#一定要顶头写

(2)添加到系统服务中:redis-server.exe redis.windows.conf

(3)出错解决方法

Redis第一次启动报错

[2368] 21 Apr 02:57:05.611 # Creating Server TCP listening socket 127.0.0.1:6379: bind: No error

解决方法:在命令行中运行

redis-cli.exe

127.0.0.1:6379>shutdown

not connected>exit

然后重新运行redis-server.exe redis.windows.conf,启动成功!

添加系统服务错误:

[12820] 06 Sep 11:00:26.431 # HandleServiceCommands: system error caught. error code=1073, message = CreateService failed: unknown error

原因:系统服务中已经存在

解决办法
1)先卸载服务:
redis-server --service-uninstall
2)然后再安装:
redis-server--service-install redis.windows.conf

启停:
启动服务:redis-server --service-start
停止服务:redis-server --service-stop


3. 查看redis中的数据

下载青格软件的TreeNMS来管理redis,这里我给出csdn的下载链接。


登录后,点击右上角“参数配置”按钮,新增或修改连接参数,我们这里要添加密码(因为我们设置了redis的密码)测试连接成功后,保存参数并刷新页面即可。


4. 编译爬取IT之家的热评的代码

这个源码,我会给出csdn上的下载链接。

这个工程还依赖mysql,所以需要安装mysql并设置密码,这里设置的用户名密码是root和mysql。如果设置成其他的,可以在工程中修改。

除此之外,我们贴出配置文件,这里主要是elasticsearch和redis的配置:

#application.yml
#端口号
server:
  port: 8081

spring:
  data:
  ##elasticsearch配置
    elasticsearch:
      cluster-name: oldkingESCluster
      cluster-nodes: localhost:9300
  ##redis配置
  redis:
    database: 0
    host: localhost
    port: 6379
    password: redis
    pool:
      max-active: 15
      max-wait: 1
      max-idle: 0
    timeout: 0
  ##freemarker配置
  freemarker:
  ##是否允许属性覆盖
    allow-request-override: false
    allow-session-override: false
    cache: true
    check-template-location: true
    content-type: text/html
  ##暴露request属性
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    suffix: .ftl
    template-loader-path: classpath:/templates/
    request-context-attribute: request
    settings:
      classic_compatible: true
      locale: zh_CN
      date_format: yyyy-MM-dd
      time_format: HH:mm:ss
      datetime_format: yyyy-MM-dd HH:mm:ss


编译之后会报错:

(1)Spring连接Elasticsearch报错:org.elasticsearch.transport.NodeDisconnectedException: [][127.0.0.1:9300][cluster:monitor/nodes/liveness] disconnected

这个错误是由于elasticsearch版本过高造成的,查看你的Elasticsearch安装目录的logs下的日志.

java.lang.IllegalStateException: Received message from unsupported version: [2.0.0] minimal compatible version is: [5.6.0]

  从不支持的版本接收消息:[2.0.0]最小兼容版本是:[5.6.0]

低版本jar包的不支持高版本的Elasticsearch

这里是什么版本建议使用什么版本的Elasticsearch。

装上elasticsearch-2.1.0 解决。

我们曾尝试按照文章《spring boot集成elasticsearch6.1版本》和《spring boot elasticsearch 5.x/6.x版本整合详解升级org.springframework.boot的版本到2.0.1,但是编译出了更多的错误,这是因为里面调用的接口发生变化造成的。所以我们还是老老实实的安装低版本的Elasticsearch算了。

另外,https://blog.csdn.net/weixin_36380516/article/details/78482167也说明了Spring boot和spring-data-elasticsearch以及elasticsearch之间有严格的版本对应关系。

有的地方写着:Spring Boot Version (x) Spring Data Elasticsearch Version (y) Elasticsearch Version (z)
x <= 1.3.5 y <= 1.3.4 z <= 1.7.2* x >= 1.4.x 2.0.0 <=y < 5.0.0** 2.0.0 <= z < 5.0.0**
* - 只需要你修改下对应的 pom 文件版本号

我们这里是spring-boot-1.5.6.RELEASE和spring-data-elasticsearch-2.1.6.RELEASE,我们卸载安装的elasticsearch 6.2.4版本,试试安装2.4.6版本。这个是没有msi的哦。

5. 重新安装elasticsearch 2.4.6

配置2.4.6可以参考:https://www.cnblogs.com/ljhdo/p/4887557.html

下载2.4.6太费劲了。下载完解压缩,然后安装head插件,然后将elasticsearch添加到系统服务中。


安装完head插件后,可以通过网页管理ElasticSearch

启动elasticsearch服务,然后在本地浏览器中输入http://localhost:9200/_plugin/head/,如果看到以下截图,说明head插件安装成功。


6. 编译工程ithomecrawler

由于是下载别人的程序,原来IThome的评论页面都是json格式,而目前完全没了格式,所以没法使用jsoup。我们要保存的数据内容如下:

    private Long id;//热评编号
    private String commentId;
    private String user;//用户
    private String comment;//内容
    private int up;//支持数
    private int down;//反对数
    private String posandtime;//位置和时间
    private String mobile;//设备
    private String articleUrl;//源文章地址

如果采用json返回类型,那么解析出上面的数据结构相当简单,然而,下面给出一个评论页面的内容(只有两条评论):

{"html":"\u003cli class=\"entry\" cid=\"32767350\" nid=\"356518\"\u003e\u003cdiv class=\"adiv\"\u003e\u003cdiv\u003e\u003ca title=\"软媒通行证数字ID:1114977\" target=\"_blank\" href=\"http://quan.ithome.com/user/1114977\"\u003e\u003cimg class=\"headerimage\" onerror=\"this.src=\u0027//img.ithome.com/images/v2.1/noavatar.png\u0027\" src=\"//avatar.ithome.com/avatars/001/11/49/77_60.jpg\"/\u003e\u003c/a\u003e\u003c/div\u003e\u003cdiv class=\"level\"\u003e\u003cspan\u003eLv.24\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv\u003e\u003cdiv class=\"info rmp\"\u003e\u003cstrong class=\"p_floor\"\u003e72楼2#\u003c/strong\u003e\u003cdiv class=\"nmp\"\u003e\u003cspan class=\"nick\"\u003e\u003ca title=\"软媒通行证数字ID:1114977\" target=\"_blank\" href=\"http://quan.ithome.com/user/1114977\"\u003e在拾荒时捡到的\u003c/a\u003e\u003c/span\u003e\u003cspan class=\"mobile iphone\"\u003e\u003ca target=\"_blank\" title=\"App版本:v6.03\" href=\"//m.ithome.com/ithome/download/\"\u003eiPhone 6s 银\u003c/a\u003e\u003c/span\u003e\u003cspan class=\"posandtime\"\u003eIT之家甘肃兰州网友\u0026nbsp;2018-4-22 20:57:21\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cp\u003eov:告辞告辞,没想到是魅族技高一筹,甘拜下风,下一代机型见\u003c/p\u003e\u003cdiv class=\"zhiChi\"\u003e\u003cdiv class=\"l\"\u003e\u003cspan class=\"comm_reply\"\u003e\u003ca class=\"comment_co\"\u003e展开(21)\u003cimg src=\"//img.ithome.com/images/v2.3/arrow.png\"\u003e\u003c/a\u003e\u003c/span\u003e\u003c/div\u003e \u003cdiv class=\"r\"\u003e\u003cspan class=\"comm_reply\"\u003e\u003ca class=\"s\" id=\"hotagree32767852\" href=\"javascript:hotCommentVote(32767852,1)\"\u003e支持(72)\u003c/a\u003e\u003ca class=\"a\" id=\"hotagainst32767852\" href=\"javascript:hotCommentVote(32767852,2)\"\u003e反对(0)\u003c/a\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/li\u003e\u003cli class=\"entry\" cid=\"32767546\" nid=\"356518\"\u003e\u003cdiv class=\"adiv\"\u003e\u003cdiv\u003e\u003ca title=\"软媒通行证数字ID:1565223\" target=\"_blank\" href=\"http://quan.ithome.com/user/1565223\"\u003e\u003cimg class=\"headerimage\" onerror=\"this.src=\u0027//img.ithome.com/images/v2.1/noavatar.png\u0027\" src=\"//avatar.ithome.com/avatars/001/56/52/23_60.jpg\"/\u003e\u003c/a\u003e\u003c/div\u003e\u003cdiv class=\"level\"\u003e\u003cspan\u003eLv.18\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv\u003e\u003cdiv class=\"info rmp\"\u003e\u003cstrong class=\"p_floor\"\u003e99楼6#\u003c/strong\u003e\u003cdiv class=\"nmp\"\u003e\u003cspan class=\"nick\"\u003e\u003ca title=\"软媒通行证数字ID:1565223\" target=\"_blank\" href=\"http://quan.ithome.com/user/1565223\"\u003e死星\u003c/a\u003e\u003c/span\u003e\u003cspan class=\"mobile android\"\u003e\u003ca target=\"_blank\" title=\"App版本:v6.02\" href=\"//m.ithome.com/ithome/download/\"\u003e华为 Mate RS 保时捷设计\u003c/a\u003e\u003c/span\u003e\u003cspan class=\"posandtime\"\u003eIT之家四川成都网友\u0026nbsp;2018-4-22 20:57:14\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cp\u003e白色15我肯定要入一部的\u003cbr\u003e颜值高(仅限白色)\u003cbr\u003e线性马达\u003cbr\u003e手感好\u003cbr\u003e24w快充保障\u003cbr\u003e够用性能+够用拍照\u003cbr\u003e\u003cbr\u003e\u003c/p\u003e\u003cdiv class=\"zhiChi\"\u003e\u003cdiv class=\"l\"\u003e\u003cspan class=\"comm_reply\"\u003e\u003ca class=\"comment_co\"\u003e展开(130)\u003cimg src=\"//img.ithome.com/images/v2.3/arrow.png\"\u003e\u003c/a\u003e\u003c/span\u003e\u003c/div\u003e \u003cdiv class=\"r\"\u003e\u003cspan class=\"comm_reply\"\u003e\u003ca class=\"s\" id=\"hotagree32767844\" href=\"javascript:hotCommentVote(32767844,1)\"\u003e支持(54)\u003c/a\u003e\u003ca class=\"a\" id=\"hotagainst32767844\" href=\"javascript:hotCommentVote(32767844,2)\"\u003e反对(1)\u003c/a\u003e\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003c/li\u003e\u003cli class=\"entry\" cid=\"32767350\" nid=\"356518\"\u003e\u003cdiv class=\"adiv\"\u003e\u003cdiv\u003e\u003ca title=\"软媒通行证数字ID:1197300\" target=\"_blank\" href=\"http://quan.ithome.com/user/1197300\"\u003e\u003cimg class=\"headerimage\" onerror=\"this.src=\u0027//img.ithome.com/images/v2.1/noavatar.png\u0027\" src=\"//avatar.ithome.com/avatars/001/19/73/00_60.jpg\"/\u003e\u003c/a\u003e\u003c/div\u003e\u003cdiv class=\"level\"\u003e\u003cspan\u003eLv.28\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv\u003e\u003cdiv class=\"info rmp\"\u003e\u003cstrong class=\"p_floor\"\u003e72楼7#\u003c/strong\u003e\u003cdiv class=\"nmp\"\u003e\u003cspan class=\"nick\"\u003e\u003ca title=\"软媒通行证数字ID:1197300\" target=\"_blank\" href=\"http://quan.ithome.com/user/1197300\"\u003e雷檬猴啊\u003c/a\u003e\u003c/span\u003e\u003cspan class=\"mobile iphone\"\u003e\u003ca target=\"_blank\" title=\"App版本:v6.02\" href=\"//m.ithome.com/ithome/download/\"\u003eiPhone 7 Plus 亮黑\u003c/a\u003e\u003c/span\u003e\u003cspan class=\"posandtime\"\u003eIT之家湖北网友\u0026nbsp;2018-4-22 21:10:02\u003c/span\u003e\u003c/div\u003e\u003c/div\u003e\u003cp\u003e鸡王:扫视一圈友商把苹果能抄的都抄了个遍,就差带震动回馈的home键还没人抄了,这波我来捡个漏

猜你喜欢

转载自blog.csdn.net/jinking01/article/details/80066683
今日推荐