springcloud集成ES(elasticsearch)将es作为一个微服务处理

整体es微服务结构
feign是微服务调用 common存储的doc对象
这个是整个es微服务的整体结构

1.首先创建doc对象

1.这个对象主要作用就是保存我们所需要存储的字段
type 类型,可以是
Text
Integer,
Long,
Date,
Float,
Double,
Boolean,
Object,
Auto,
Nested,
Ip,
Attachment,
Keyword;
@Field注解主要根据字段的数据类型来确定其中text表示需要分词analyzer 表示分词器searchAnalyzer表示按照这个分词规则来进行搜索
Keyword不进行分词 text分词 index索引(跟数据库的索引相似)

doc对象

//应该有什么字段,展示页的  所有条件 ,所有排序 ,所有显示的字段
//课程的文档对象
@Document(indexName = "hrm-course",type = "course")
@Data //<-get and set
public class CourseDoc {

    @Id
    private Long id;

    @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_smart")
    private String name;

    @Field(type = FieldType.Keyword)
    private String forUser;

    @Field(type = FieldType.Long)
    private Long courseTypeId;

    @Field(type = FieldType.Keyword)
    private String gradeName;

    @Field(type = FieldType.Long)
    private Long gradeId;

    @Field(type = FieldType.Text,analyzer = "ik_smart",searchAnalyzer = "ik_smart")
    private String tenantName;

    @Field(type = FieldType.Keyword,index=false)
    private String pic;

    @Field(type = FieldType.Integer,index=false)
    private Integer saleCount;

    @Field(type = FieldType.Integer,index=false)
    private Integer viewCount;

    @Field(type = FieldType.Integer,index=false)
    private Integer commentCount;

    @Field(type = FieldType.Date,index=false)
    private Date onlineDate;

    @Field(type = FieldType.Integer)
    private Integer charge;

    @Field(type = FieldType.Keyword)
    private String chargeName;

    @Field(type = FieldType.Float)
    private Float price;

    @Field(type = FieldType.Float)
    private Float priceOld;
}

2.导入maven依赖

maven依赖

    <dependencies>
        <!--eureka客户端的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--web环境包,不可少-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--这个包是我自己的包书写其他业务逻辑的 -->
                <dependency>
            <groupId>cn.zhangqiang</groupId>
            <artifactId>hrm-basic-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--集成swagger  不集成就不要-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
  <!--下面这个包里面有我自己的doc对象 doc对象就是存入elasticsearch的一个对象(个人理解)-->
        <dependency>
            <groupId>cn.zhangqiang</groupId>
            <artifactId>hrm-es-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

3.写一个接口去继承对es进行增删改查的一个接口

主要是因为这个接口不能被调用,所以我们需要去继承一下,并交给spring管理

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

4.写一个controller对接口进行注入,并调用方法

//ES接口
@RestController
public class ESController {

    @Autowired
    private CourseElasticsearchRepository courseElasticsearchRepository;

    //添加
    @RequestMapping(value = "/es/save",method = RequestMethod.POST)
    public AjaxResult save(@RequestBody CourseDoc couresDoc){
        courseElasticsearchRepository.save(couresDoc);
        return AjaxResult.me();
    }
}

5.书写启动类

@SpringBootApplication
@EnableEurekaClient
public class EsServerApplication1070{
    public static void main( String[] args ){
        SpringApplication.run(EsServerApplication1070.class);
    }
}

6.配置yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:1010/eureka/   # 这个是我的注册中心
    registry-fetch-interval-seconds: 5
  instance:
    instance-id: search-server:1070 #在仪表盘显示的实例ID
    prefer-ip-address: true  #使用IP注册到Eureka
server:
  port: 1070  #暴露的端口
spring:
  application:
    name: search-server
  data:    #配置Es的
     elasticsearch:
       cluster-name: elasticsearch
       cluster-nodes: 127.0.0.1:9300 #9200是图形界面端,9300代码端

然后需要这个服务的可以创建一个feign接口去调用

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值