Spring整合Solr7.1:总是需要开拓的勇气
2017-11-17 19:24
423 查看
自我更新,需要对技术的热爱,对极致的追求
IDEA 2017.2.6
Spring 4.3.12
Solr 7.1.0
首先一个改变就是,在4.10的时候,要配置的是HttpSolrServer,而在7.1的时候,你需要配置的东西改名成HttpSolrClient了
这是solr4.10.3的HttpSolrServer的构造方法
这是solr4.10.3的applicationContextSolr.xml
这里是solr7.1的HttpSolrClient构造方法
可以看出,在solr最新版的源码中,已经移除了只有baseUrl的构造方法,但是加入了一个接收一个叫Builder的参数的构造方法,那么,Builder是什么?
ctrl+鼠标左键,看到了下面这段代码:
哈哈,可以看到,原来你需要用baseUrl先构造出来一个Builder对象,然后再用Builder去构造出我们需要的HttpSolrClient对象.
虽然,源码还没研究明白,但总算是解决了问题.
下面就是我们Spring整合Solr7所需的配置文件了
2017/11/17
Lucifer
一.环境说明
Windows 10 ver 1709IDEA 2017.2.6
Spring 4.3.12
Solr 7.1.0
二.开始吧
最近在做solr的升级工作,从4.10升级到最新的7.1,却发现困难难重重.因为相关可参考的资料太少了,能搜到的基本都是停留在solr4或5之类的,而再往上的却只有寥寥,让我这个菜鸟很是忧伤.没办法,只能一步步查源码,翻官方文档,来研究更新了什么.三.对比
在4.10.3的时候,配置是这样的,新建个applicationSolr.xml,作为solr的配置文件(个人喜好,也可以不单独出来)首先一个改变就是,在4.10的时候,要配置的是HttpSolrServer,而在7.1的时候,你需要配置的东西改名成HttpSolrClient了
这是solr4.10.3的HttpSolrServer的构造方法
public HttpSolrServer(String baseURL) { this(baseURL, (HttpClient)null, new BinaryResponseParser()); } public HttpSolrServer(String baseURL, HttpClient client) { this(baseURL, client, new BinaryResponseParser()); } public HttpSolrServer(String baseURL, HttpClient client, ResponseParser parser) { this.requestWriter = new RequestWriter(); this.followRedirects = false; this.maxRetries = 0; this.queryParams = Collections.emptySet(); this.baseUrl = baseURL; if (this.baseUrl.endsWith("/")) { this.baseUrl = this.baseUrl.substring(0, this.baseUrl.length() - 1); } if (this.baseUrl.indexOf(63) >= 0) { throw new RuntimeException("Invalid base url for solrj. The base URL must not contain parameters: " + this.baseUrl); } else { if (client != null) { this.httpClient = client; this.internalClient = false; } else { this.internalClient = true; ModifiableSolrParams params = new ModifiableSolrParams(); params.set("maxConnections", 128); params.set("maxConnectionsPerHost", 32); params.set("followRedirects", this.followRedirects); this.httpClient = HttpClientUtil.createClient(params); } this.parser = parser; } }
这是solr4.10.3的applicationContextSolr.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer"> <constructor-arg value="solrCore的远程地址"/> </bean> </beans>
这里是solr7.1的HttpSolrClient构造方法
** * @deprecated use {@link HttpSolrClient#HttpSolrClient(Builder)} instead, as it is a more extension/subclassing-friendly alternative */ @Deprecated protected HttpSolrClient(String baseURL, HttpClient client, ResponseParser parser, boolean allowCompression) { this(new Builder(baseURL) .withHttpClient(client) .withResponseParser(parser) .allowCompression(allowCompression)); } /** * The constructor. * * @param baseURL The base url to communicate with the Solr server * @param client Http client instance to use for communication * @param parser Response parser instance to use to decode response from Solr server * @param allowCompression Should compression be allowed ? * @param invariantParams The parameters which should be included with every request. * * @deprecated use {@link HttpSolrClient#HttpSolrClient(Builder)} instead, as it is a more extension/subclassing-friendly alternative */ @Deprecated protected HttpSolrClient(String baseURL, HttpClient client, ResponseParser parser, boolean allowCompression, ModifiableSolrParams invariantParams) { this(new Builder(baseURL) .withHttpClient(client) .withResponseParser(parser) .allowCompression(allowCompression) .withInvariantParams(invariantParams)); } protected HttpSolrClient(Builder builder) { this.baseUrl = builder.baseSolrUrl; if (baseUrl.endsWith("/")) { baseUrl = baseUrl.substring(0, baseUrl.length() - 1); } if (baseUrl.indexOf('?') >= 0) { throw new RuntimeException( "Invalid base url for solrj. The base URL must not contain parameters: " + baseUrl); } if (builder.httpClient != null) { this.httpClient = builder.httpClient; this.internalClient = false; } else { this.internalClient = true; ModifiableSolrParams params = new ModifiableSolrParams(); params.set(HttpClientUtil.PROP_FOLLOW_REDIRECTS, followRedirects); params.set(HttpClientUtil.PROP_ALLOW_COMPRESSION, builder.compression); httpClient = HttpClientUtil.createClient(params); } this.parser = builder.responseParser; this.invariantParams = builder.invariantParams; this.connectionTimeout = builder.connectionTimeoutMillis; this.soTimeout = builder.socketTimeoutMillis; }
可以看出,在solr最新版的源码中,已经移除了只有baseUrl的构造方法,但是加入了一个接收一个叫Builder的参数的构造方法,那么,Builder是什么?
ctrl+鼠标左键,看到了下面这段代码:
//无参构造 public Builder() { this.responseParser = new BinaryResponseParser(); } /** * Create a Builder object, based on the provided Solr URL. * * Two different paths can be specified as a part of this URL: * * 1) A path pointing directly at a particular core * <pre> * SolrClient client = new HttpSolrClient.Builder("http://my-solr-server:8983/solr/core1").build(); * QueryResponse resp = client.query(new SolrQuery("*:*")); * </pre> * Note that when a core is provided in the base URL, queries and other requests can be made without mentioning the * core explicitly. However, the client can only send requests to that core. * * 2) The path of the root Solr path ("/solr") * <pre> * SolrClient client = new HttpSolrClient.Builder("http://my-solr-server:8983/solr").build(); * QueryResponse resp = client.query("core1", new SolrQuery("*:*")); * </pre> * In this case the client is more flexible and can be used to send requests to any cores. This flexibility though * requires that the core be specified on all requests. * * By default, compression is not enabled on created HttpSolrClient objects. */ public Builder(String baseSolrUrl) { this.baseSolrUrl = baseSolrUrl; this.responseParser = new BinaryResponseParser(); }
哈哈,可以看到,原来你需要用baseUrl先构造出来一个Builder对象,然后再用Builder去构造出我们需要的HttpSolrClient对象.
虽然,源码还没研究明白,但总算是解决了问题.
下面就是我们Spring整合Solr7所需的配置文件了
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--版本更新说明: 1.从solr5.x开始,HttoSolrServer就变成了HttpSolrClient 2.查看源码发现,之前的构造方法已经修改,以前的注入方法也不再适用.主要由一个静态类builder来构造,而builder需要一个baseUrl, 所以差不多就是之前的单baseUrl的构造方法(源码179,830) --> <bean id="httpSolrClient" class="org.apache.solr.client.solrj.impl.HttpSolrClient"> <constructor-arg name="builder" value="solrCore的远程地址"/> </bean> </beans>
2017/11/17
Lucifer
相关文章推荐
- spring整合rabbitmq(需要的jar包,spring如何整合rabbitmq)
- maven项目下solr和spring的整合配置
- SpringBoot整合Solr 入门
- spring3.0.4+struts2.1.8+hibernate3.5.5 整合 所需要的jar文件
- AXIS2整合spring需要的jar,以及大部分缺少jar所报的异常
- spring2.5整合ibatis2.3事务管理时遇到的问题以及需要的jar包
- struts2和spring整合时需要注意的地方
- Spring 整合redis集群 实现 以及过程中需要注意的问题点
- maven整合spring与solr
- [增删改查] SpringBoot 整合 Solr 之 SolrClient 实现 CRUD、分页接口、高亮显示
- solr整合spring
- SpringBoot整合Spring Data Solr
- struts2.1.8 hibernate3.3.2 spring2.5 整合需要哪些jar包
- 经典Solr集群搭建、以及SolrCloud+Spring____整合
- solr系列三:solr和spring整合并实现增删改查功能
- Spring整合Hibernate需要引入的包
- myBatis3与spring整合之需要的jar包
- maven项目下solr和spring的整合配置
- cxf_rs_spring 整合所需要测试maven的pom配置文件
- Spring3.1+Hibernate3+Struts2的最新整合所需要的jar包