涉及微服务:
changgou_web_search:存放静态资源,实现页面跳转。
changgou_service_search:负责从ES查询数据。
changgou_service_search_api:放实体对象及feign接口。
(1)创建changgou_web_search工程
pom的引入(前端使用的是thymeleaf模板引擎)
<dependencies>
<dependency>
<groupId>com.changgou</groupId>
<artifactId>changgou_common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.changgou</groupId>
<artifactId>changgou_service_search_api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
存放静态资源创建类:
Snipaste_2019-08-21_11-20-04.png
这里注意static与templates文件夹名必须这样起,不然需要配置,麻烦!
application.yml的配置:
server:
port: 9011
spring:
application:
name: webSearch
main:
allow-bean-definition-overriding: true #当遇到同样名字的时候,是否允许覆盖注册
thymeleaf:
cache: false #thymeleaf数据缓存关闭
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:6868/eureka
instance:
prefer-ip-address: true
feign:
hystrix:
enabled: true
client:
config:
default: #配置全局的feign的调用超时时间 如果 有指定的服务配置 默认的配置不会生效
connectTimeout: 60000 # 指定的是 消费者 连接服务提供者的连接超时时间 是否能连接 单位是毫秒
readTimeout: 80000 # 指定的是调用服务提供者的 服务 的超时时间() 单位是毫秒
#hystrix 配置
hystrix:
command:
default:
execution:
timeout:
#如果enabled设置为false,则请求超时交给ribbon控制
enabled: true
isolation:
strategy: SEMAPHORE
thread:
# 熔断器超时时间,默认:1000/毫秒
timeoutInMilliseconds: 80000
websearchController:
package com.changgou.web.search.controller;
import com.changgou.entity.Page;
import com.changgou.web.search.service.WebSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;
import java.util.Set;
/**
* @author :gzy
* @date :Created in 2019/8/20
* @description :
* @version: 1.0
*/
@Controller
@RequestMapping("/wsearch")
public class WebSearchController {
@Autowired
private WebSearchService webSearchService;
@GetMapping
public String searchGoods(Model model,@RequestParam Map<String,String> searchmap){
Map map = webSearchService.searchSkuInfoByEs(searchmap);
model.addAttribute("result",map);
model.addAttribute("searchMap",searchmap);
model.addAttribute("page",new Page((int)map.get("total"),Integer.valueOf(map.get("pageNum").toString()),Page.pageSize));
//http://search.changgou.com:9011/wsearch?keywords=手机
StringBuilder url = new StringBuilder();
url.append("http://search.changgou.com:9011/wsearch");
if(null!=searchmap&&searchmap.size()>0){
url.append("?");
Set<Map.Entry<String, String>> entries = searchmap.entrySet();
for (Map.Entry<String, String> entry : entries) {
if(!"pageNum".equals(entry.getKey())){
url.append("&").append(entry.getKey()).append("=").append(entry.getValue());
}
}
}
model.addAttribute("url",url.toString());
return "search";
}
}
WebSearchService 实现类及接口省略,实现类主要通过Feign调用changgou_service_search微服务的查询功能。
(2)changgou_service_search工程
搜索功能实现类:
/**
* @author :gzy
* @date :Created in 2019/8/19
* @description :
* @version: 1.0
*/
@Service
public class SearchServiceImpl implements SearchService {
@Autowired
private EsMapper esMapper;
@Autowired
private GoodsFeign goodsFeign;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
//从 数据库导入数据到Es
@Override
public void importData() {
//创建索引
elasticsearchTemplate.createIndex(SkuInfo.class);
//设置映射
elasticsearchTemplate.putMapping(SkuInfo.class);
List<Sku> byStatus = goodsFeign.findByStatus("1");
String string = JSON.toJSONString(byStatus);
List<SkuInfo> skuInfos = JSON.parseArray(string, SkuInfo.class);
for (SkuInfo skuInfo : skuInfos) {
skuInfo.setSpecMap(JSON.parseObject(skuInfo.getSpec(),Map.class));
}
esMapper.saveAll(skuInfos);
}
//搜索功能
@Override
public Map searchSkuInfoByEs(Map<String, String> searchmap) {
Map map=new HashMap<>();
//条件对象 目标:为了合并 搜索条件及排序及分页及高亮
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//组合条件对象
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//本次查询关键词
if(null!=searchmap.get("keywords") && !"".equals(searchmap.get("keywords").trim())){
boolQueryBuilder.must(QueryBuilders.matchQuery("name",searchmap.get("keywords")));
}
else {
//默认搜索条件
}
//分页
String pageNum=searchmap.get("pageNum");
if(StringUtils.isEmpty(pageNum)){
pageNum="1";
}
nativeSearchQueryBuilder.withPageable(PageRequest.of(Integer.valueOf(pageNum)-1,Page.pageSize));
nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
AggregatedPage<SkuInfo> page = elasticsearchTemplate.queryForPage(nativeSearchQueryBuilder.build(), SkuInfo.class);
List<SkuInfo> content = page.getContent();
//查询到的数据
map.put("rows",content);
//当前页
map.put("pageNum",pageNum);
//数据总条数
map.put("total",page.getTotalElements());
return map;
}
}
searcController:
package com.changgou.search.controller;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.security.cert.TrustAnchor;
import java.util.Map;
/**
* @author :gzy
* @date :Created in 2019/8/19
* @description :
* @version: 1.0
*/
@RestController
@CrossOrigin
@RequestMapping("/search")
public class SearchController {
@Autowired
private SearchService searchService;
/*
搜索,被远程调用
*/
@GetMapping("/list")
public Map search(@RequestParam Map<String,String> searchmap){
return searchService.searchSkuInfoByEs(searchmap);
}
//导入数据
@GetMapping("/importData")
public Result importData(){
searchService.saveall();
return new Result(true,StatusCode.OK,"导入成功");
}
}
(3)changgou_service_search_api
暴露的feign接口:
package com.changgou.search.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;
/**
* @author :gzy
* @date :Created in 2019/8/20
* @description :
* @version: 1.0
*/
@FeignClient(name = "search")
@RequestMapping("/search")
public interface SearchFeign {
@GetMapping("/list")
Map search(@RequestParam(name = "searchmap") Map<String,String> searchmap);
}
网友评论