Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webmagic
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
沈俊林
webmagic
Commits
55d80129
Commit
55d80129
authored
Jul 25, 2013
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
invite redis for distribution
parent
619aab43
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
98 additions
and
6 deletions
+98
-6
pom.xml
webmagic-plugin/pom.xml
+5
-0
RedisScheduler.java
.../java/us/codecraft/webmagic/scheduler/RedisScheduler.java
+45
-0
RedisSchedulerTest.java
...a/us/codecraft/webmagic/scheduler/RedisSchedulerTest.java
+41
-0
GlobalProcessor.java
...n/java/us/codecraft/webmagic/samples/GlobalProcessor.java
+7
-6
No files found.
webmagic-plugin/pom.xml
View file @
55d80129
...
@@ -25,6 +25,11 @@
...
@@ -25,6 +25,11 @@
<artifactId>
freemarker
</artifactId>
<artifactId>
freemarker
</artifactId>
<version>
2.3.15
</version>
<version>
2.3.15
</version>
</dependency>
</dependency>
<dependency>
<groupId>
redis.clients
</groupId>
<artifactId>
jedis
</artifactId>
<version>
2.0.0
</version>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
webmagic-plugin/src/main/java/us/codecraft/webmagic/scheduler/RedisScheduler.java
0 → 100644
View file @
55d80129
package
us
.
codecraft
.
webmagic
.
scheduler
;
import
redis.clients.jedis.Jedis
;
import
redis.clients.jedis.JedisPool
;
import
redis.clients.jedis.JedisPoolConfig
;
import
us.codecraft.webmagic.Request
;
import
us.codecraft.webmagic.Task
;
import
us.codecraft.webmagic.schedular.Scheduler
;
/**
* 使用redis管理url,构建一个分布式的爬虫。<br>
* @author yihua.huang@dianping.com <br>
* @date: 13-7-25 <br>
* Time: 上午7:07 <br>
*/
public
class
RedisScheduler
implements
Scheduler
{
private
JedisPool
pool
;
private
static
final
String
QUEUE_PREFIX
=
"queue_"
;
private
static
final
String
SET_PREFIX
=
"set_"
;
public
RedisScheduler
(
String
host
){
pool
=
new
JedisPool
(
new
JedisPoolConfig
(),
host
);
}
@Override
public
synchronized
void
push
(
Request
request
,
Task
task
)
{
Jedis
jedis
=
pool
.
getResource
();
if
(
jedis
.
zrank
(
SET_PREFIX
+
task
.
getUUID
(),
request
.
getUrl
())==
null
){
jedis
.
rpush
(
QUEUE_PREFIX
+
task
.
getUUID
(),
request
.
getUrl
());
jedis
.
zadd
(
SET_PREFIX
+
task
.
getUUID
(),
System
.
currentTimeMillis
(),
request
.
getUrl
());
}
pool
.
returnResource
(
jedis
);
}
@Override
public
synchronized
Request
poll
(
Task
task
)
{
Jedis
jedis
=
pool
.
getResource
();
String
url
=
jedis
.
lpop
(
QUEUE_PREFIX
+
task
.
getUUID
());
pool
.
returnResource
(
jedis
);
return
new
Request
(
url
);
}
}
webmagic-plugin/src/test/java/us/codecraft/webmagic/scheduler/RedisSchedulerTest.java
0 → 100644
View file @
55d80129
package
us
.
codecraft
.
webmagic
.
scheduler
;
import
org.junit.Before
;
import
org.junit.Test
;
import
us.codecraft.webmagic.Request
;
import
us.codecraft.webmagic.Site
;
import
us.codecraft.webmagic.Task
;
/**
* @author yihua.huang@dianping.com <br>
* @date: 13-7-25 <br>
* Time: 上午7:51 <br>
*/
public
class
RedisSchedulerTest
{
private
RedisScheduler
redisScheduler
;
@Before
public
void
setUp
()
{
redisScheduler
=
new
RedisScheduler
(
"localhost"
);
}
@Test
public
void
test
()
{
Task
task
=
new
Task
()
{
@Override
public
String
getUUID
()
{
return
"1"
;
}
@Override
public
Site
getSite
()
{
return
null
;
}
};
redisScheduler
.
push
(
new
Request
(
"http://www.ibm.com/developerworks/cn/java/j-javadev2-22/"
),
task
);
Request
poll
=
redisScheduler
.
poll
(
task
);
System
.
out
.
println
(
poll
.
getUrl
());
}
}
webmagic-samples/src/main/java/us/codecraft/webmagic/samples/GlobalProcessor.java
View file @
55d80129
...
@@ -3,11 +3,9 @@ package us.codecraft.webmagic.samples;
...
@@ -3,11 +3,9 @@ package us.codecraft.webmagic.samples;
import
us.codecraft.webmagic.Page
;
import
us.codecraft.webmagic.Page
;
import
us.codecraft.webmagic.Site
;
import
us.codecraft.webmagic.Site
;
import
us.codecraft.webmagic.Spider
;
import
us.codecraft.webmagic.Spider
;
import
us.codecraft.webmagic.downloader.FileDownloader
;
import
us.codecraft.webmagic.downloader.HttpClientDownloader
;
import
us.codecraft.webmagic.pipeline.FilePipeline
;
import
us.codecraft.webmagic.pipeline.FilePipeline
;
import
us.codecraft.webmagic.processor.PageProcessor
;
import
us.codecraft.webmagic.processor.PageProcessor
;
import
us.codecraft.webmagic.schedul
ar.FileCacheQueue
Scheduler
;
import
us.codecraft.webmagic.schedul
er.Redis
Scheduler
;
import
java.util.List
;
import
java.util.List
;
...
@@ -40,9 +38,12 @@ public class GlobalProcessor implements PageProcessor {
...
@@ -40,9 +38,12 @@ public class GlobalProcessor implements PageProcessor {
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
Spider
.
create
(
new
GlobalProcessor
()).
thread
(
10
)
Spider
.
create
(
new
GlobalProcessor
()).
thread
(
10
)
.
scheduler
(
new
FileCacheQueueScheduler
(
"/data/webmagic/test"
))
.
scheduler
(
new
RedisScheduler
(
"localhost"
))
.
downloader
(
new
FileDownloader
(
"/data/webmagic/test"
,
new
HttpClientDownloader
()))
.
pipeline
(
new
FilePipeline
(
"/data/webmagic/test/"
))
.
pipeline
(
new
FilePipeline
(
"/data/webmagic/test"
))
.
runAsync
();
Spider
.
create
(
new
GlobalProcessor
()).
thread
(
10
)
.
scheduler
(
new
RedisScheduler
(
"localhost"
))
.
pipeline
(
new
FilePipeline
(
"/data/webmagic/test/"
))
.
run
();
.
run
();
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment