1. 程式人生 > >Spring-Boot 整合Solr客戶端

Spring-Boot 整合Solr客戶端


 
  

tips: IDEA的快速查詢類快捷鍵 (連續按兩次 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註解來掃描相應的類, 如果遇到無法讀取配置類屬性的情況, 首先考慮這個因素

3) 在resources下新建application.properties, 完成solr的基本配置
spring.data.solr.host=http://127.0.0.1:8983/solr

這個屬性配置的是solr伺服器的訪問地址, 因為本專案是作為客戶端來訪問solr伺服器, 所以不用做更多的配置
這個屬性是是通過@ConfigurationProperties("spring.data.solr")讀取出來的, 預設被讀取到 SolrProperties.class 中 詳情請使用類查詢器檢視該類

4) 新建一個Controller用來查詢Solr伺服器資料
@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();
    }
}

資料是我提前匯入的, 這裡使用ID查詢結果
SolrDocument{goodsId=[129831], id=fe7a5124-d75b-40b2-93fe-5555512ea6d2, _version_=1562570354094768128}

5) solr整合結束, 但問題來了

問題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());
    }

}

當我們引入

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-solr</artifactId>
    <version>${spring.data.solr.version}</version>
</dependency>

這個自動配置就會因為我們在AppMain.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"));
    }
}

啟動結果

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[email protected]160f0c04: startup date [Thu Mar 23 10:32:15 CST 2017]; root of context hierarchy

也就是自定義配置完成

我建議不使用自定義的配置方式, 因為它原有的自動裝配已經非常方便了. 並且可以根據是否配置zookeeper來判斷使用單機版或者叢集版.
現在能使用SolrClient了, 剩下的是需要封裝工具類, 完成客戶端的查詢和更新操作就OK了