Spring-Boot 集成Solr客戶端的詳細(xì)步驟
Solr 是基于 Lucene 的全文檢索服務(wù)器,可配置、可擴(kuò)展,并對索引和搜索性能進(jìn)行了優(yōu)化。Solr 多用于電子商務(wù)網(wǎng)站、門戶、論壇這類網(wǎng)站的站內(nèi)搜索。Solr 可以獨(dú)立運(yùn)行在 Jetty、Tomcat 等這些 Servlet 容器中。Solr 索引的實(shí)現(xiàn)非常簡單,用 POST 方法去向 Solr服務(wù)器發(fā)送一個描述 Field 及其內(nèi)容的 JSON 文檔,Solr 根據(jù) JSON 文件增刪改索引。Solr 搜索只需要發(fā)送 HTTP GET 請求,然后對 Solr 返回 JSON 格式的查詢結(jié)果進(jìn)行解析,組織頁面布局。Solr 不提供構(gòu)建 UI 的功能,Solr提供了一個管理界面,通過管理界面可以查詢 Solr 的配置和運(yùn)行情況。
tips: IDEA的快速查找類快捷鍵 (連續(xù)按兩次 shift 鍵)
1) maven配置:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<spring.data.solr.version>2.1.1.RELEASE</spring.data.solr.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>${spring.data.solr.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--添加Web依賴, 使項目變成web項目-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
2) SpringBoot 快速啟動
@SpringBootApplication
@EnableAutoConfiguration
public class AppMain {
public static void main(String[] args) {
SpringApplication.run(AppMain.class, args);
}
}
可能遇見的問題:
1. 一般spring-boot項目的啟動類都放在項目的根路徑下, 這樣可以不用配置@ComponentScan注解來掃描相應(yīng)的類, 如果遇到無法讀取配置類屬性的情況, 首先考慮這個因素
3) 在resources下新建application.properties, 完成solr的基本配置
spring.data.solr.host=http://127.0.0.1:8983/solr
這個屬性配置的是solr服務(wù)器的訪問地址, 因?yàn)楸卷椖渴亲鳛榭蛻舳藖碓L問solr服務(wù)器, 所以不用做更多的配置
這個屬性是是通過@ConfigurationProperties("spring.data.solr")讀取出來的, 默認(rèn)被讀取到 SolrProperties.class 中 詳情請使用類查找器查看該類
4) 新建一個Controller用來查詢Solr服務(wù)器數(shù)據(jù)
@RestController
public class SolrController {
@Autowired
private SolrClient client;
@RequestMapping("/")
public String testSolr() throws IOException, SolrServerException {
SolrDocument document = client.getById("test", "fe7a5124-d75b-40b2-93fe-5555512ea6d2");
System.out.println(document);
return document.toString();
}
}
數(shù)據(jù)是我提前導(dǎo)入的, 這里使用ID查詢結(jié)果
SolrDocument{goodsId=[129831], id=fe7a5124-d75b-40b2-93fe-5555512ea6d2, _version_=1562570354094768128}
5) solr集成結(jié)束, 但問題來了
問題1: 為什么沒有配置SolrClient, 但卻自動注入成功了呢?
問題2: 它是在什么地方被注入到BeanFactory的? (@Autowired能夠注入成功說明該類存在于其中)
問題3: 能不能自己配置?
問題4: 有沒有必要自己配置?
6) 解決問題1, 和問題2:
SolrAutoConfiguration 是Solr自動配置的類, 在Spring-Boot啟動的時候會自動加載屬性, 注入SolrClient類
@Configuration
@ConditionalOnClass({ HttpSolrClient.class, CloudSolrClient.class })
@EnableConfigurationProperties(SolrProperties.class)
public class SolrAutoConfiguration {
private final SolrProperties properties;
private SolrClient solrClient;
public SolrAutoConfiguration(SolrProperties properties) {
this.properties = properties;
}
@Bean
@ConditionalOnMissingBean
public SolrClient solrClient() {
this.solrClient = createSolrClient();
return this.solrClient;
}
private SolrClient createSolrClient() {
if (StringUtils.hasText(this.properties.getZkHost())) {
return new CloudSolrClient(this.properties.getZkHost());
}
return new HttpSolrClient(this.properties.getHost());
}
}
當(dāng)我們引入
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>${spring.data.solr.version}</version>
</dependency>
這個自動配置就會因?yàn)槲覀冊贏ppMain.java上配置的@EnableAutoConfiguration注解生效, 這樣就會自動注入Bean了
7) 解決問題3, 問題4
肯定是可以自己配置的, 配置方式也非常簡單
只需要自己編寫一個類, 使用@Configuration注解, 并且配置一個@Bean, 返回SolrClient就可以了
@Configuration
public class SolrClientConfiguration {
@Autowired
private Environment environment;
@Bean
public SolrClient solrClient() {
System.out.println("自定義配置SolrClient");
return new HttpSolrClient(environment.getRequiredProperty("spring.data.solr.host"));
}
}
啟動結(jié)果
2017-03-23 10:32:17.414 INFO 10359 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 自定義配置SolrClient 2017-03-23 10:32:18.178 INFO 10359 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@160f0c04: startup date [Thu Mar 23 10:32:15 CST 2017]; root of context hierarchy
也就是自定義配置完成
我建議不使用自定義的配置方式, 因?yàn)樗械淖詣友b配已經(jīng)非常方便了. 并且可以根據(jù)是否配置zookeeper來判斷使用單機(jī)版或者集群版.
現(xiàn)在能使用SolrClient了, 剩下的是需要封裝工具類, 完成客戶端的查詢和更新操作就OK了
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java基于深度優(yōu)先遍歷的隨機(jī)迷宮生成算法
今天小編就為大家分享一篇關(guān)于Java基于深度優(yōu)先遍歷的隨機(jī)迷宮生成算法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02
通過使用Byte?Buddy便捷創(chuàng)建Java?Agent
這篇文章主要為大家介紹了如何通過使用Byte?Buddy便捷創(chuàng)建Java?Agent的使用說明,有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
Java線程重復(fù)執(zhí)行以及操作共享變量的代碼示例
這篇文章主要介紹了Java中對線程重復(fù)執(zhí)行以及操作共享變量的代碼示例,來自于Java面試題目的練習(xí)整理,需要的朋友可以參考下2015-12-12
SpringBoot創(chuàng)建多模塊項目的全過程記錄
這篇文章主要給大家介紹了關(guān)于SpringBoot創(chuàng)建多模塊項目的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
SpringBoot整合PageHelper實(shí)現(xiàn)分頁查詢功能詳解
PageHelper是mybatis框架的一個插件,用于支持在mybatis執(zhí)行分頁操作。本文將通過SpringBoot整合PageHelper實(shí)現(xiàn)分頁查詢功能,需要的可以參考一下2022-03-03
Spring Boot示例分析講解自動化裝配機(jī)制核心注解
這篇文章主要分析了Spring Boot 自動化裝配機(jī)制核心注解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-07-07

