整体es微服务结构
feign是微服务调用 common存储的doc对象
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代码端