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

也就是自定义配置完成

我建议不使用自定义的配置方式, 因为它原有的自动装配已经非常方便了. 并且可以根据是否配置zookeeper来判断使用单机版或者集群版.
现在能使用SolrClient了, 剩下的是需要封装工具类, 完成客户端的查询和更新操作就OK了



作者:小雨转暴雨
链接:http://www.jianshu.com/p/e21fe5f3bd8c
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

转载于:https://www.cnblogs.com/zxf160/p/7211454.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值