springboot篇】二十一. 基于springboot电商项目 四
程序员文章站
2022-07-14 08:18:20
...
springboot项目
- **中国加油,武汉加油!**
- 篇幅较长,配合目录观看
- 案例准备
- 1. 商品同步到索引库中
- 1.1 shop-service-api编写Service接口
- 1.2 shop-service-iml新建search-service(module-springboot)
- 1.3 导相关依赖
- 1.4 编写ServiceImpl
- 1.5 编写yml
- 1.6 修改程序入口
- 1.7 shop-web下的shop-search导包
- 1.8 修改yml
- 1.9 修改Controller
- 1.10 修改程序入口
- 1.11 启动程序入口
- 1.12 访问
- 1.13 search-service测试同步
- 1.14 shop-service-iml的GoodsServiceImpl重写insert方法
- 1.14 shop-service-api定义方法
- 1.16 search-service重写方法
中国加油,武汉加油!
篇幅较长,配合目录观看
案例准备
1. 商品同步到索引库中
1.1 shop-service-api编写Service接口
package com.wpj.service;
import com.wpj.entity.Goods;
import java.util.List;
public interface ISearchService {
public List<Goods> searchGoodsListByKey(String key);
}
1.2 shop-service-iml新建search-service(module-springboot)
- spring boot devtools
- spring data for apache solr
1.3 导相关依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.wpj</groupId>
<artifactId>shop-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
1.4 编写ServiceImpl
package com.wpj.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.wpj.entity.Goods;
import com.wpj.service.ISearchService;
import org.apache.solr.client.solrj.SolrClient;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@Service
public class SearchServiceImpl implements ISearchService {
@Autowired
private SolrClient solrClient;
@Override
public List<Goods> searchGoodsListByKey(String key) {
System.out.println("SearchServiceImpl.searchGoodsListByKey" + key);
return null;
}
}
1.5 编写yml
dubbo:
registry:
address: zookeeper://192.168.1.114:2181
application:
name: search-service
protocol:
port: -1
spring:
data:
solr:
host: http://192.168.1.114:8983/solr/mycollection
1.6 修改程序入口
package com.wpj;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@DubboComponentScan(basePackages = "com.wpj.service")
public class SearchServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SearchServiceApplication.class, args);
}
}
1.7 shop-web下的shop-search导包
<dependency>
<groupId>com.wpj</groupId>
<artifactId>search-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
1.8 修改yml
server:
port: 8082
dubbo:
registry:
address: zookeeper://192.168.1.114:2181
application:
name: shop-service
protocol:
port: -1
consumer:
check: false
retries: 3
1.9 修改Controller
package com.wpj.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.wpj.entity.Goods;
import com.wpj.service.ISearchService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/search")
public class SearchController {
@Reference
private ISearchService searchService;
@RequestMapping("/searchGoods")
@ResponseBody
public String searchGoods(String key) {
System.out.println("SearchController.searchGoods" + key);
List<Goods> goodsList = searchService.searchGoodsListByKey(key);
return "ok";
}
}
1.10 修改程序入口
package com.wpj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(scanBasePackages = "com.wpj", exclude = DataSourceAutoConfiguration.class)
public class ShopSearchApplication {
public static void main(String[] args) {
SpringApplication.run(ShopSearchApplication.class, args);
}
}
1.11 启动程序入口
1.12 访问
1.13 search-service测试同步
package com.wpj;
import com.alibaba.dubbo.config.annotation.Reference;
import com.wpj.entity.Goods;
import com.wpj.service.IGoodService;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.common.SolrInputDocument;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
class SearchServiceApplicationTests {
@Autowired
private SolrClient solrClient;
@Reference
private IGoodService goodService;
@Test
public void testSyncGoods() throws Exception{
// 查询所有的商品
List<Goods> list = goodService.getList();
for (Goods goods : list) {
SolrInputDocument document = new SolrInputDocument();
// 把商品添加到索引库
document.addField("gname",goods.getGname());
document.addField("id",goods.getId());
document.addField("gdesc",goods.getGdesc());
document.addField("gpic",goods.getGpic());
document.addField("gprice",goods.getGprice().toString());
solrClient.add(document);
}
solrClient.commit();
}
}
1.14 shop-service-iml的GoodsServiceImpl重写insert方法
@Reference
private ISearchService searchService;
@Override
public int insert(Goods goods) {
int insert = super.insert(goods);
searchService.addGoods(goods);
return insert;
}
1.14 shop-service-api定义方法
package com.wpj.service;
import com.wpj.entity.Goods;
import java.util.List;
public interface ISearchService {
public List<Goods> searchGoodsListByKey(String key);
public void addGoods(Goods goods);
}
1.16 search-service重写方法
@Override
public void addGoods(Goods goods) {
SolrInputDocument document = new SolrInputDocument();
document.addField("id",goods.getId());
document.setField("gname", goods.getGname());
document.setField("gdesc", goods.getGdesc());
document.setField("gpic", goods.getGpic());
document.setField("gprice", goods.getGprice().toString());
try {
solrClient.add(document);
solrClient.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}