设为首页 收藏本站
查看: 1118|回复: 0

[经验分享] Spring-Boot 集成Solr客户端

[复制链接]

尚未签到

发表于 2017-12-19 06:23:07 | 显示全部楼层 |阅读模式
Spring-Boot 集成Solr的学习

  tips:>
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>  

  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>  

  @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],>

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>  

  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>  

  @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了

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-425559-1-1.html 上篇帖子: solr之~模糊查询【转】 下篇帖子: Lily HBase Indexer同步HBase二级索引到Solr丢失数据的问题分析
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表