微服务--商品上架并保存到ElasticSearch流程

本文介绍了ElasticSearch的基本概念,以及如何在SpringCloud项目中整合ElasticSearch,包括添加依赖、定义文档模型、创建索引和映射,以及使用FeignClient进行远程调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一丶ElasticSearch是什么?

1.ES是一个分布式的全文搜索引擎,为了解决原生Lucene使用的不足,优化Lucene的调用方式,并实现了高可用的分布式集群的搜索方案,ES的索引库管理支持依然是基于Apache Lucene(TM)的开源搜索引擎。

2.ES也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的 RESTful API来隐Lucene的复杂性,从而让全文搜索变得简单。

3.总的来说ElasticSearch简化了全文检索lucene的使用,同时增加了分布式的特性,使得构建大规模分布式全文检索变得非常容易。

4.Lucene是apache下的一个开源的全文检索引擎工具包(一堆jar包)。它为软件开发人员提供一个简单易用的工具包(类库),以方便的在小型目标系统中实现全文检索的功能。Lucene适用于中小型项目 ,ES适用于中大型项目(它底层是基于lucene实现的)

二丶SpringCloud项目整合ElasticSearch

1.整一个ElasticSearch模块(名字任取)

2.导入依赖

    <-- elasticsearch包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
    <!--测试类-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

3.编写一个Doc,这里以课程类(course)为例

@Document(indexName = "course" , type = "_doc")
@Data
public class CourseDoc {
    // ID
    @Id
    private Long id;
    // 课程名
    @Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart") // 指定为 分词
    private String name;
    // 价格
    @Field(type = FieldType.Double)
    private BigDecimal price;
    // 原价
    @Field(type = FieldType.Double)
    private BigDecimal priceOld;
    // 课程类型
    @Field(type = FieldType.Long)
    private Long courseTypeId;
    // 课程等级
    @Field(type = FieldType.Keyword)
    private String gradeName;
    // 课程封面
    @Field(type = FieldType.Keyword)
    private String pic;
    // 课程上线时间
    @Field(type = FieldType.Date)
    private Date onlineTime;
    // 课程老师名字
    @Field(type = FieldType.Keyword)
    private String teacherName;
    // 课程是否收费 - 收费/免费
    @Field(type = FieldType.Keyword)
    private String chargeName;
    // 课程销量
    @Field(type = FieldType.Integer)
    private Integer saleCount;
    // 课程浏览量
    @Field(type = FieldType.Integer)
    private Integer viewCount;
}

4.编写repository,并继承ElasticsearchRepository类


@Repository
public interface CourseElasticsearchRepository  extends ElasticsearchRepository<CourseDoc,Long> {

}

这里的泛型第一个参数类型是编写的Doc类,第二个参数类型是Doc中id的类型

5.初始化索引库和文档映射

ElasticsearchTemplate 是一个ES的工具类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SearchStart.class)
public class ESTest {

    @Autowired
    private ElasticsearchRestTemplate template;

    @Test
    public void testCreateIndex() {
        //创建索引
        template.createIndex(CourseDoc.class);
        //做文档映射
        template.putMapping(CourseDoc.class);
    }

6.编写controller接口类

@RestController
@RequestMapping("/es/course")
public class SearchController {

    @Autowired
    private ISearchService searchService;

    @PostMapping("/search")
    public JSONResult search(@RequestBody CourseDoc doc){
        searchService.search(doc);
        return JSONResult.success();
    }
}

7.编写service实现类

@Service
public class SearchServiceImpl implements ISearchService {
    @Autowired
    private CourseElasticsearchRepository courseElasticsearchRepository;

    @Override
    public void search(CourseDoc doc) {
        courseElasticsearchRepository.save(doc);
    }
}

8.抽取feiqn接口

@FeignClient(value = "service-search",fallbackFactory = SearchFallbackFactory.class)
public interface SearchClient {

    @PostMapping("/es/course/search")
    JSONResult search(@RequestBody CourseDoc doc);
}

9.编写降级类

@Component
public class SearchFallbackFactory implements FallbackFactory<SearchClient> {
    @Override
    public SearchClient create(Throwable throwable) {
        return new SearchClient() {
            @Override
            public JSONResult search(CourseDoc doc) {
                throwable.printStackTrace();
                return JSONResult.error("调用服务失败");
            }
        };
    }
}

10.调用feiqn接口

调用之前,调用的模块需要开启feiqn支持(@EnableFeignClients),然后配置feiqn的降级

feign:
  sentinel:
    enabled: true

然后注入feiqn接口即可使用方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值