diff --git a/beacon-common/src/main/java/com/mashibing/common/enums/ExceptionEnums.java b/beacon-common/src/main/java/com/mashibing/common/enums/ExceptionEnums.java index 699273a..809105f 100644 --- a/beacon-common/src/main/java/com/mashibing/common/enums/ExceptionEnums.java +++ b/beacon-common/src/main/java/com/mashibing/common/enums/ExceptionEnums.java @@ -27,8 +27,7 @@ public enum ExceptionEnums { LIMIT_MINUTE(-16, "达到分钟限流阈值!!"), LIMIT_HOUR(-17, "达到小时限流阈值!!"), NO_CHANNEL(-18, "没有可用通道!!!"), - - ; + SEARCH_INDEX_ERROR(-20, "es添加一行文档失败!!!"); private final int code; diff --git a/beacon-common/src/main/java/com/mashibing/common/exception/SearchException.java b/beacon-common/src/main/java/com/mashibing/common/exception/SearchException.java new file mode 100644 index 0000000..8ca0a7a --- /dev/null +++ b/beacon-common/src/main/java/com/mashibing/common/exception/SearchException.java @@ -0,0 +1,30 @@ +package com.mashibing.common.exception; + +import com.mashibing.common.enums.ExceptionEnums; +import lombok.Getter; + +/** + * @author heqijun + * @ClassName: SearchException + * @Description: 搜索模块自定义异常 + * @date 2025/6/12 18:13 + */ + +@Getter +public class SearchException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + private final Integer code; + + public SearchException(Integer code, String message) { + super(message); + this.code = code; + } + + public SearchException(ExceptionEnums exceptionEnums) { + super(exceptionEnums.getMsg()); + this.code = exceptionEnums.getCode(); + } + +} diff --git a/beacon-search/src/main/java/com/mashibing/search/config/RestHighLevelClientConfig.java b/beacon-search/src/main/java/com/mashibing/search/config/RestHighLevelClientConfig.java index 5577527..71e21ed 100644 --- a/beacon-search/src/main/java/com/mashibing/search/config/RestHighLevelClientConfig.java +++ b/beacon-search/src/main/java/com/mashibing/search/config/RestHighLevelClientConfig.java @@ -1,6 +1,10 @@ package com.mashibing.search.config; import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.impl.client.BasicCredentialsProvider; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; @@ -21,6 +25,12 @@ public class RestHighLevelClientConfig { @Value("${elasticsearch.hostAndPorts}") private String[] hostAndPorts; + @Value("${elasticsearch.username:elastic}") + private String username; + + @Value("${elasticsearch.password}") + private String password; + @Bean public RestHighLevelClient restHighLevelClient() { @@ -30,8 +40,15 @@ public class RestHighLevelClientConfig { httpHost[i] = new HttpHost(hostAndPort[0], Integer.parseInt(hostAndPort[1])); } + // 设置认证信息 + CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); + + // 设置连接信息 RestClientBuilder restClientBuilder = RestClient.builder(httpHost); + restClientBuilder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider)); + // 使用RestClientBuilder构建RestHighLevelClient对象 RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); return restHighLevelClient; diff --git a/beacon-search/src/main/java/com/mashibing/search/service/SearchService.java b/beacon-search/src/main/java/com/mashibing/search/service/SearchService.java new file mode 100644 index 0000000..8658a11 --- /dev/null +++ b/beacon-search/src/main/java/com/mashibing/search/service/SearchService.java @@ -0,0 +1,22 @@ +package com.mashibing.search.service; + +import java.io.IOException; + +/** + * @author heqijun + * @ClassName: SearchService + * @Description: 搜索模块增删改查service接口 + * @date 2025/6/12 18:00 + */ + +public interface SearchService { + + /** + * 向es中添加一行文档 + * + * @param index 索引信息 + * @param id 文档id + * @param json 文档内容 + */ + void index(String index, String id, String json) throws IOException; +} diff --git a/beacon-search/src/main/java/com/mashibing/search/service/impl/ElasticSearchServiceImpl.java b/beacon-search/src/main/java/com/mashibing/search/service/impl/ElasticSearchServiceImpl.java new file mode 100644 index 0000000..99e0520 --- /dev/null +++ b/beacon-search/src/main/java/com/mashibing/search/service/impl/ElasticSearchServiceImpl.java @@ -0,0 +1,52 @@ +package com.mashibing.search.service.impl; + +import com.mashibing.common.enums.ExceptionEnums; +import com.mashibing.common.exception.SearchException; +import com.mashibing.search.service.SearchService; +import lombok.extern.slf4j.Slf4j; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.common.xcontent.XContentType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.IOException; + +/** + * @author heqijun + * @ClassName: ElasticSearchServiceImpl + * @Description: 搜索模块增删改查service实现类 + * @date 2025/6/12 18:03 + */ + +@Slf4j +@Service +public class ElasticSearchServiceImpl implements SearchService { + + private static String CREATED = "created"; + + @Autowired + RestHighLevelClient client; + + @Override + public void index(String index, String id, String json) throws IOException { + + //构建request + IndexRequest request = new IndexRequest(); + request.index(index).id(id).source(json, XContentType.JSON); + + //发送插入请求 + IndexResponse response = client.index(request, RequestOptions.DEFAULT); + + String result = response.getResult().getLowercase(); + if (!CREATED.equals(result)) { + //插入失败 + log.error("【搜索模块-写入数据】失败!!!index = {},id = {},json = {},result = {}", index, id, json, result); + throw new SearchException(ExceptionEnums.SEARCH_INDEX_ERROR); + } + log.info("【搜索模块-写入数据】成功!!!index = {},id = {},json = {},result = {}", index, id, json, result); + + } +} diff --git a/beacon-search/src/test/java/com/mashibing/search/service/impl/ElasticSearchServiceImplTest.java b/beacon-search/src/test/java/com/mashibing/search/service/impl/ElasticSearchServiceImplTest.java new file mode 100644 index 0000000..38da8be --- /dev/null +++ b/beacon-search/src/test/java/com/mashibing/search/service/impl/ElasticSearchServiceImplTest.java @@ -0,0 +1,28 @@ +package com.mashibing.search.service.impl; + +import com.mashibing.search.service.SearchService; +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.io.IOException; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +@RunWith(SpringRunner.class) +class ElasticSearchServiceImplTest { + + @Autowired + private SearchService service; + + @Test + void index() throws IOException { + String index = "sms_submit_log_2023"; + String id = "1"; + String json = "{\"clientId\": 1}"; + service.index(index, id, json); + } +} \ No newline at end of file