一聚教程网:一个值得你收藏的教程网站

热门教程

Spring-Boot 集成Solr客户端的详细步骤

时间:2022-06-29 01:05:50 编辑:袖梨 来源:一聚教程网

Solr 是基于 Lucene 的全文检索服务器,可配置、可扩展,并对索引和搜索性能进行了优化。Solr 多用于电子商务网站、门户、论坛这类网站的站内搜索。Solr 可以独立运行在 Jetty、Tomcat 等这些 Servlet 容器中。Solr 索引的实现非常简单,用 POST 方法去向 Solr服务器发送一个描述 Field 及其内容的 JSON 文档,Solr 根据 JSON 文件增删改索引。Solr 搜索只需要发送 HTTP GET 请求,然后对 Solr 返回 JSON 格式的查询结果进行解析,组织页面布局。Solr 不提供构建 UI 的功能,Solr提供了一个管理界面,通过管理界面可以查询 Solr 的配置和运行情况。

tips: IDEA的快速查找类快捷键 (连续按两次 shift 键)

1) maven配置:

  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.2.RELEASE
  

  
    2.1.1.RELEASE
  

  
    
      
        org.springframework.data
        spring-data-solr
        ${spring.data.solr.version}
      
    
  

  
    
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.springframework.data
      spring-data-solr
    

    
    
      org.springframework.boot
      spring-boot-starter-test
    
  

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

}

当我们引入


  org.springframework.data
  spring-data-solr
  ${spring.data.solr.version}

这个自动配置就会因为我们在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.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@160f0c04: startup date [Thu Mar 23 10:32:15 CST 2017]; root of context hierarchy

也就是自定义配置完成

我建议不使用自定义的配置方式, 因为它原有的自动装配已经非常方便了. 并且可以根据是否配置zookeeper来判断使用单机版或者集群版.

现在能使用SolrClient了, 剩下的是需要封装工具类, 完成客户端的查询和更新操作就OK了

热门栏目