比尔云BierYun--阿里云最新优惠活动
阿里云优惠码丨阿里云代金券

通过springboot构建solr查询实例

通过springboot构建solr查询实例http://www.bieryun.com/1681.html

曾经使用spring我们需要各种各样的配置web.xml,配置springapplication.xml。真的很繁琐,要建一个项目框架出来,确实不太容易。而现在spring社区推出spring boot。完全不需要去管那些xml的配置了,springboot采用约定优于配置的思想,通过你项目所需的dependencies来自动配置框架。
springboot的特性:
1.自动配置文件
2.直接内置tomcat或jetty web服务器不需要打war包直接通过mvn spring-boot:run就可以运行。
3.只需要提供几个简单依赖就可以提供像springmvc,mybatis,jpa等框架支持。

springboot实例

该实例使用maven构建项目,以下是maven依赖。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <guava.version>18.0</guava.version>
    <commons.lang3.version>3.3.2</commons.lang3.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>4.0.0</version>
    </dependency>
    <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
<dependency>
        <groupId>org.apache.solr</groupId>
        <artifactId>solr-solrj</artifactId>
        <version>5.3.0</version>
    </dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
</dependencies>

application.properties

spring.solr.host=http://192.168.219.134:8080/solr
spring.solr.zkHost=192.168.219.134:2181,192.168.219.140:2181
spring.solr.defaultCollection=demo

通过一个bean接收zk服务器相关属性

import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="spring.solr")
public class SolrConfig {

private String host;
private String zkHost;
private String defaultCollection;


public String getDefaultCollection() {
    return defaultCollection;
}
public void setDefaultCollection(String defaultCollection) {
    this.defaultCollection = defaultCollection;
}
public String getHost() {
    return host;
}
public void setHost(String host) {
    this.host = host;
}
public String getZkHost() {
    return zkHost;
}
public void setZkHost(String zkHost) {
    this.zkHost = zkHost;
}

}

配置solrserver

@Configuration
@EnableConfigurationProperties(SolrConfig.class)
public class solrClientConfig {

@Autowired
private SolrConfig solrConfig;



private CloudSolrServer solrServer;

@PreDestroy
public void close() {
    if (this.solrServer != null) {
        try {
            this.solrServer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
@Bean 
public CloudSolrServer SolrServer(){
    if (StringUtils.hasText(this.solrConfig.getZkHost())) {
        //solrClient = new CloudSolrClient(this.solrConfig.getZkHost());
        solrServer = new CloudSolrServer(this.solrConfig.getZkHost());
        solrServer.setDefaultCollection(this.solrConfig.getDefaultCollection());
    }
    return this.solrServer;
}

}

接下来测试solr查询

@RestController
public class HelloController {

@Autowired
private CloudSolrServer solrserver;


public String hello(){
    return"say hello";
}
@RequestMapping("/test")
public void test(){

    ModifiableSolrParams params =new ModifiableSolrParams();
    params.add("q","demo:父亲");
    params.add("ws","json");
    params.add("start","0");
    params.add("rows","10");
    QueryResponse response=null;
    
    try{
        response=solrserver.query(params);
        SolrDocumentList results = response.getResults();
        for (SolrDocument document:results) {
        System.out.println( document.getFieldValue("demo"));
        System.out.println(document.getFieldValue("id"));
        }
    }catch(Exception e){
        e.getStackTrace();
    }
    System.out.println(response.toString());
}

}

未经允许不得转载:比尔云 » 通过springboot构建solr查询实例
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

强烈推荐

高性能SSD云服务器ECS抗攻击,高可用云数据库RDS