Spring-Boot 集成Solr客户端的详细步骤
Solr是基于Lucene的全文检索服务器,可配置、可扩展,并对索引和搜索性能进行了优化。Solr多用于电子商务网站、门户、论坛这类网站的站内搜索。Solr可以独立运行在Jetty、Tomcat等这些Servlet容器中。Solr索引的实现非常简单,用POST方法去向Solr服务器发送一个描述Field及其内容的JSON文档,Solr根据JSON文件增删改索引。Solr搜索只需要发送HTTPGET请求,然后对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 publicclassAppMain{ publicstaticvoidmain(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 publicclassSolrController{ @Autowired privateSolrClientclient; @RequestMapping("/") publicStringtestSolr()throwsIOException,SolrServerException{ SolrDocumentdocument=client.getById("test","fe7a5124-d75b-40b2-93fe-5555512ea6d2"); System.out.println(document); returndocument.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) publicclassSolrAutoConfiguration{ privatefinalSolrPropertiesproperties; privateSolrClientsolrClient; publicSolrAutoConfiguration(SolrPropertiesproperties){ this.properties=properties; } @Bean @ConditionalOnMissingBean publicSolrClientsolrClient(){ this.solrClient=createSolrClient(); returnthis.solrClient; } privateSolrClientcreateSolrClient(){ if(StringUtils.hasText(this.properties.getZkHost())){ returnnewCloudSolrClient(this.properties.getZkHost()); } returnnewHttpSolrClient(this.properties.getHost()); } }
当我们引入
org.springframework.data spring-data-solr ${spring.data.solr.version}
这个自动配置就会因为我们在AppMain.java上配置的@EnableAutoConfiguration注解生效,这样就会自动注入Bean了
7)解决问题3,问题4
肯定是可以自己配置的,配置方式也非常简单
只需要自己编写一个类,使用@Configuration注解,并且配置一个@Bean,返回SolrClient就可以了
@Configuration publicclassSolrClientConfiguration{ @Autowired privateEnvironmentenvironment; @Bean publicSolrClientsolrClient(){ System.out.println("自定义配置SolrClient"); returnnewHttpSolrClient(environment.getRequiredProperty("spring.data.solr.host")); } }
启动结果
2017-03-2310:32:17.414INFO10359---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'requestContextFilter'to:[/*] 自定义配置SolrClient 2017-03-2310:32:18.178INFO10359---[main]s.w.s.m.m.a.RequestMappingHandlerAdapter:Lookingfor@ControllerAdvice:org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@160f0c04:startupdate[ThuMar2310:32:15CST2017];rootofcontexthierarchy
也就是自定义配置完成
我建议不使用自定义的配置方式,因为它原有的自动装配已经非常方便了.并且可以根据是否配置zookeeper来判断使用单机版或者集群版.
现在能使用SolrClient了,剩下的是需要封装工具类,完成客户端的查询和更新操作就OK了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。