Skip to content

Commit 883985f

Browse files
committed
更新 ES 实战代码
1 parent 00116c8 commit 883985f

File tree

12 files changed

+90
-18
lines changed

12 files changed

+90
-18
lines changed

passjava-common/src/main/java/com/jackson0714/common/exception/BizCodeEnum.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
***/
1717
public enum BizCodeEnum {
1818
UNKNOWN_EXCEPTION(10000, "系统未知异常"),
19-
VALID_EXCEPTION(10001, "参数格式校验失败");
19+
VALID_EXCEPTION(10001, "参数格式校验失败"),
20+
QUESTION_SAVE_EXCEPTION(12001, "题目保存异常");
2021

2122
private int code;
2223
private String msg;

passjava-common/src/main/java/com/jackson0714/common/to/es/QuestionEsModel.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@
2929
* */
3030
@Data
3131
public class QuestionEsModel {
32-
3332
private Long id;
3433
private String title;
3534
private String answer;
3635
private String typeName;
37-
3836
}

passjava-question/src/main/java/com/jackson0714/passjava/question/PassjavaQuestionApplication.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import org.springframework.boot.SpringApplication;
55
import org.springframework.boot.autoconfigure.SpringBootApplication;
66
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7+
import org.springframework.cloud.openfeign.EnableFeignClients;
8+
import org.springframework.context.annotation.ComponentScan;
79

10+
@EnableFeignClients(basePackages = "com.jackson0714.passjava.question.feign")
811
@EnableDiscoveryClient
912
@MapperScan("com.jackson0714.passjava.question.dao")
1013
@SpringBootApplication
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.jackson0714.passjava.question.feign;
2+
3+
import com.jackson0714.common.to.es.QuestionEsModel;
4+
import com.jackson0714.common.utils.R;
5+
import org.apache.ibatis.annotations.Mapper;
6+
import org.springframework.cloud.openfeign.FeignClient;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
10+
/**
11+
* @Author: 公众号 | 悟空聊架构
12+
* @Date: 2021/4/6 15:14
13+
* @Site: www.passjava.cn
14+
* @Github: https://github.com/Jackson0714/PassJava-Platform
15+
*/
16+
@Mapper
17+
@FeignClient("passjava-search")
18+
public interface SearchFeignService {
19+
20+
@PostMapping("search/question/save")
21+
R saveQuestion(@RequestBody QuestionEsModel questionEsModel);
22+
}

passjava-question/src/main/java/com/jackson0714/passjava/question/service/IQuestionService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
*
1111
*
12-
* @author jackson0714
12+
* @author 公众号 | 悟空聊架构
1313
* @email jackson0585@163.com
1414
* @date 2020-04-25 22:34:04
1515
*/

passjava-question/src/main/java/com/jackson0714/passjava/question/service/impl/QuestionServiceImpl.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.jackson0714.passjava.question.service.impl;
22

33
import com.jackson0714.common.to.es.QuestionEsModel;
4+
import com.jackson0714.common.utils.R;
45
import com.jackson0714.passjava.question.entity.TypeEntity;
6+
import com.jackson0714.passjava.question.feign.SearchFeignService;
57
import com.jackson0714.passjava.question.service.ITypeService;
68
import org.apache.commons.lang.StringUtils;
79
import org.springframework.beans.BeanUtils;
@@ -23,7 +25,10 @@
2325
public class QuestionServiceImpl extends ServiceImpl<QuestionDao, QuestionEntity> implements IQuestionService {
2426

2527
@Autowired
26-
ITypeService ITypeService;
28+
ITypeService typeService;
29+
30+
@Autowired
31+
SearchFeignService searchFeignService;
2732

2833
@Override
2934
public PageUtils queryPage(Map<String, Object> params) {
@@ -58,7 +63,7 @@ public boolean saveQuestion(QuestionEntity question) {
5863
public boolean updateQuestion(QuestionEntity question) {
5964
updateById(question);
6065
saveEs(question);
61-
return false;
66+
return true;
6267
}
6368

6469
private boolean saveEs(QuestionEntity question) {
@@ -68,14 +73,22 @@ private boolean saveEs(QuestionEntity question) {
6873
// 2.1 复制属性
6974
BeanUtils.copyProperties(question, esModel);
7075
// 2.2 获取“题目类型”的名称
71-
TypeEntity typeEntity = ITypeService.getById(question.getType());
76+
TypeEntity typeEntity = typeService.getById(question.getType());
7277
String typeName = typeEntity.getType();
7378
// 2.3 给 ES model 的“类型”字段赋值
7479
esModel.setTypeName(typeName);
75-
// 3.
7680
System.out.println("-----------------esModel:" + esModel);
7781

78-
return false;
82+
// 3. 调用 passjava-search 服务,将数据发送到 ES 中保存。
83+
R r = searchFeignService.saveQuestion(esModel);
84+
85+
System.out.println("r:" + r);
86+
87+
if (r.get("code") != 0) {
88+
return false;
89+
}
90+
91+
return true;
7992
}
8093

8194
}

passjava-search/src/main/java/com/jackson0714/passjava/search/PassjavaSearchApplication.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
66
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7+
import org.springframework.context.annotation.ComponentScan;
78

89
@EnableDiscoveryClient
910
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

passjava-search/src/main/java/com/jackson0714/passjava/search/config/PassJavaElasticsearchConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @Author: 公众号 | 悟空聊架构
1212
* @Date: 2020/10/8 17:02
13-
* @Site: www.passjava.club
13+
* @Site: www.passjava.cn
1414
* @Github: https://github.com/Jackson0714/PassJava-Platform
1515
*/
1616
@Configuration

passjava-search/src/main/java/com/jackson0714/passjava/search/controller/QuestionController.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jackson0714.passjava.search.controller;
22

3+
import com.jackson0714.common.exception.BizCodeEnum;
34
import com.jackson0714.common.to.es.QuestionEsModel;
45
import com.jackson0714.common.utils.R;
56
import com.jackson0714.passjava.search.service.IQuestionService;
@@ -21,13 +22,21 @@
2122
public class QuestionController {
2223

2324
@Autowired
24-
IQuestionService IQuestionService;
25+
IQuestionService questionService;
2526

2627
// 保存题目到 ES。
2728
@PostMapping("/question/save")
2829
public R saveQuestion(@RequestBody QuestionEsModel questionEsModel) {
30+
boolean result =false;
31+
try {
32+
result = questionService.save(questionEsModel);
33+
} catch (Exception e) {
34+
result =false;
35+
}
2936

30-
IQuestionService.save(questionEsModel);
37+
if (!result) {
38+
return R.error(BizCodeEnum.QUESTION_SAVE_EXCEPTION.getCode(), BizCodeEnum.QUESTION_SAVE_EXCEPTION.getMsg());
39+
}
3140

3241
return R.ok();
3342
}
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package com.jackson0714.passjava.search.service;
22

33
import com.jackson0714.common.to.es.QuestionEsModel;
4+
import org.apache.ibatis.annotations.Mapper;
5+
import org.springframework.stereotype.Component;
6+
7+
import java.io.IOException;
48

59
/**
610
* @Author: 公众号 | 悟空聊架构
711
* @Date: 2021/3/31 17:56
8-
* @Site: www.jayh.club
12+
* @Site: www.passjava.cn
913
* @Github: https://github.com/Jackson0714/PassJava-Platform
1014
*/
15+
@Mapper
16+
@Component
1117
public interface IQuestionService {
12-
void save(QuestionEsModel questionEsModel);
18+
boolean save(QuestionEsModel questionEsModel) throws IOException;
1319
}

0 commit comments

Comments
 (0)