Commit 8abd95d0 authored by Quxl's avatar Quxl

修改搜索引擎代码,修改Redis配置

parent 90d5f431
package com.egolm.shop.config; package com.egolm.shop.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
...@@ -15,7 +16,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -15,7 +16,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
public class RedisConfig { public class RedisConfig {
@Bean @Bean
public RedisTemplate<String, ?> getRedisTemplate(RedisConnectionFactory connectionFactory) { @Qualifier("redisTemplate")
public RedisTemplate<String, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, ?> template = new RedisTemplate<>(); RedisTemplate<String, ?> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory); template.setConnectionFactory(connectionFactory);
Jackson2JsonRedisSerializer<?> valueSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class); Jackson2JsonRedisSerializer<?> valueSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
......
package com.egolm.shop.config;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SolrConfig {
@Value("solr.baseURL")
private String baseURL;
@Bean
public HttpSolrClient httpSolrClient() {
return new HttpSolrClient(baseURL);
}
}
package com.egolm.shop.controller; package com.egolm.shop.controller;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
...@@ -13,6 +22,7 @@ import io.swagger.annotations.Api; ...@@ -13,6 +22,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import redis.clients.jedis.JedisCommands;
@Api @Api
@Controller @Controller
...@@ -22,8 +32,12 @@ public class SolrGoodsController { ...@@ -22,8 +32,12 @@ public class SolrGoodsController {
@Autowired @Autowired
private SolrGoodsService service; private SolrGoodsService service;
@Autowired
@Qualifier("redisTemplate")
private RedisTemplate<String, ?> redisTemplate;
@ResponseBody @ResponseBody
@RequestMapping("query") @PostMapping("query")
@ApiOperation("搜索引擎商品搜索") @ApiOperation("搜索引擎商品搜索")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(dataType="long", allowMultiple=false, name="index", value="分页编号"), @ApiImplicitParam(dataType="long", allowMultiple=false, name="index", value="分页编号"),
...@@ -40,15 +54,7 @@ public class SolrGoodsController { ...@@ -40,15 +54,7 @@ public class SolrGoodsController {
} }
@ResponseBody @ResponseBody
@RequestMapping("update") @PostMapping("updateByDistrictID")
@ApiOperation("搜索引擎更新全部商品索引")
public Object update() {
service.update();
return Rjx.jsonOk();
}
@ResponseBody
@RequestMapping("updateByDistrictID")
@ApiOperation("搜索引擎按区域编号更新商品索引") @ApiOperation("搜索引擎按区域编号更新商品索引")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(dataType="string", name="sDistrictID", value="区域编号"), @ApiImplicitParam(dataType="string", name="sDistrictID", value="区域编号"),
...@@ -58,4 +64,51 @@ public class SolrGoodsController { ...@@ -58,4 +64,51 @@ public class SolrGoodsController {
return Rjx.jsonOk(); return Rjx.jsonOk();
} }
@ResponseBody
@PostMapping("update")
@Scheduled(cron="0 0 4 * * ?")
@ApiOperation("搜索引擎更新全部商品索引")
public Object update() {
if(this.getLock().getCode() == 200) {
try {
service.update(null);
} finally {
this.releaseLock();
}
} else {
return Rjx.jsonErr().setMessage("该任务正在运行,请不要重复执行");
}
return Rjx.jsonOk();
}
private static final String SCHEDULE_LOCK_KEY = "SCHEDULE_LOCK_KEY";
private static final long SCHEDULE_LOCK_EXPIRE = 1000*60;
@ResponseBody
@PostMapping("getLock")
@ApiOperation("设置搜索引擎更新索引定时任务分布式互斥锁")
public Rjx getLock() {
String result = redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
JedisCommands commands = (JedisCommands) connection.getNativeConnection();
String uuid = UUID.randomUUID().toString();
return commands.set(SCHEDULE_LOCK_KEY, uuid, "NX", "PX", SCHEDULE_LOCK_EXPIRE);
}
});
if(result != null && result.equals("OK")) {
return Rjx.jsonOk();
} else {
return Rjx.jsonErr();
}
}
@ResponseBody
@PostMapping("releaseLock")
@ApiOperation("删除搜索引擎更新索引定时任务分布式互斥锁")
public Rjx releaseLock() {
redisTemplate.delete(SCHEDULE_LOCK_KEY);
return Rjx.jsonOk();
}
} }
...@@ -7,8 +7,6 @@ public interface SolrGoodsService { ...@@ -7,8 +7,6 @@ public interface SolrGoodsService {
public Rjx query(Page page, String queryKey, String[] keywords, String[] facetFields, String[] facetPivotFields); public Rjx query(Page page, String queryKey, String[] keywords, String[] facetFields, String[] facetPivotFields);
public void update();
public void update(String sDistrictID); public void update(String sDistrictID);
public void deleteByQuery(String queryKey); public void deleteByQuery(String queryKey);
......
...@@ -23,7 +23,9 @@ import org.apache.solr.common.SolrDocument; ...@@ -23,7 +23,9 @@ import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList; import org.apache.solr.common.util.NamedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.egolm.common.DateUtil; import com.egolm.common.DateUtil;
import com.egolm.common.StringUtil; import com.egolm.common.StringUtil;
...@@ -34,11 +36,15 @@ import com.egolm.common.jdbc.Page; ...@@ -34,11 +36,15 @@ import com.egolm.common.jdbc.Page;
import com.egolm.shop.config.XException; import com.egolm.shop.config.XException;
import com.egolm.shop.service.SolrGoodsService; import com.egolm.shop.service.SolrGoodsService;
@Service
public class SolrGoodsServiceImpl implements SolrGoodsService { public class SolrGoodsServiceImpl implements SolrGoodsService {
private static final Logger logger = Logger.getLogger(SolrGoodsService.class); private static final Logger logger = Logger.getLogger(SolrGoodsService.class);
@Autowired
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
@Autowired
private HttpSolrClient solrClient; private HttpSolrClient solrClient;
@Override @Override
...@@ -160,11 +166,6 @@ public class SolrGoodsServiceImpl implements SolrGoodsService { ...@@ -160,11 +166,6 @@ public class SolrGoodsServiceImpl implements SolrGoodsService {
} }
} }
@Async
public void update() {
this.update(null);
}
@Async @Async
public void update(String sDistrictID) { public void update(String sDistrictID) {
List<String> strs = new ArrayList<String>(); List<String> strs = new ArrayList<String>();
......
...@@ -32,6 +32,8 @@ spring.redis.pool.max-wait=2000 ...@@ -32,6 +32,8 @@ spring.redis.pool.max-wait=2000
spring.redis.pool.max-idle=5 spring.redis.pool.max-idle=5
spring.redis.pool.min-idle=0 spring.redis.pool.min-idle=0
solr.baseURL=http://10.10.0.84:8983/solr/goods
### 游客 用户生成用户的前缀 ### 游客 用户生成用户的前缀
redis.guest.key=B2B_Guest redis.guest.key=B2B_Guest
#### 用户验证码存储的前缀 #### 用户验证码存储的前缀
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment