webmagic配置RedisScheduler
导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
这里因为jedispool新版本中returnresource方法被弃用,所以将jedis包排除用回旧的版本。
配置RedisConfig
package com.example.demo.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis;
@Bean
public JedisPool redisPoolFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
return new JedisPool(jedisPoolConfig,host,port,timeout,password);
}
}
配置文件
spring:
redis:
host: {主机号}
port: {端口}
password: {密码}
timeout: 30000
jedis:
pool:
max-idle: 100
max-wait: 1000
server:
port: 8081
spider代码
先注入我们在配置类中写好的JedisPool bean
@Autowired
private JedisPool jedisPool;
Spider.create(new Processor())
.setUUID("123456")
.setScheduler(new RedisScheduler(jedisPool))
.addUrl(url)
.run();
setUUID只有在分布式的时候是必须的,当你在不同的项目中使用同样的uuid,他们就会使用redis中的同一个队列。所以setUUID必须放在setScheduler上面,不然获取不到uuid。
博客主要介绍了WebMagic配置RedisScheduler的相关内容。因JedisPool新版本中方法被弃用,排除jedis包用回旧版本。还涉及RedisConfig配置、配置文件设置以及Spider代码编写,强调注入JedisPool bean,且setUUID要放在setScheduler上面。
21万+

被折叠的 条评论
为什么被折叠?



