SpringBoot2.0.4整合elasticsearch為5.6.10
阿新 • • 發佈:2018-12-25
網上找了很一些,很多跑不起來,可能是我的環境和介紹的環境不一樣,自己搞重新搞一下!
環境說明:
- spring boot 使用2.0.4
- elasticsearch為5.6.10
- 本地安裝ES叢集為 6.x版本
第一步
使用IDEA建立Spring boot web專案,使用spring boot 使用2.0.4版本, elasticsearch為5.6.10
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId >
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--引入ELasticSearch的依賴包,預設使用elasticsearch5.6.10版本-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--需要引入transport-netty3-client,否則會啟動報錯-->
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty3-client</artifactId >
<version>5.6.10</version>
</dependency>
第二步
配置程式碼,講ES CLient 元件加入到Spring 容器。
第一種配置方法
/**
* ES 配置類
* @author:dufyun
* @version:1.0.0
* @date 2018/8/19
* @update:[日期YYYY-MM-DD] [更改人姓名][變更描述]
*/
@Configuration
public class ElasticSearchConfig {
private static final Logger logger = LoggerFactory.getLogger(ElasticSearchConfig.class);
@Bean
public TransportClient transportClient() {
logger.info("初始化開始。。。。。");
TransportClient client = null;
try {
TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("localhost"),
Integer.valueOf(9300));
// 配置資訊
Settings esSetting = Settings.builder()
.put("cluster.name","myEsCluster")
.build();
//配置資訊Settings自定義
client = new PreBuiltTransportClient(esSetting);
client.addTransportAddresses(transportAddress);
} catch (Exception e) {
logger.error("elasticsearch TransportClient create error!!!", e);
}
return client;
}
}
第二種配置方法
也可以使用配置檔案!elasticsearch.properties
es.hostName=localhost
es.transport=9300
es.cluster.name=myEsCluster
@Configuration
@PropertySource(value={"classpath:elasticsearch.properties"}) // 載入 指定的配置,只支援properti
public class ElasticSearchConfig {
private static final Logger logger = LoggerFactory.getLogger(ElasticSearchConfig.class);
@Value("${es.hostName}")
private String hostName;
@Value("${es.transport}")
private Integer transport;
@Value("${es.cluster.name}")
private String clusterName;
@Bean
public TransportClient transportClient() {
LOGGER.info("初始化開始。。。。。");
TransportClient client = null;
try {
TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName(hostName),
Integer.valueOf(transport));
// 配置資訊
Settings esSetting = Settings.builder()
.put("cluster.name",clusterName)
.build();
//配置資訊Settings自定義,下面設定為EMPTY
client = new PreBuiltTransportClient(esSetting);
client.addTransportAddresses(transportAddress);
} catch (Exception e) {
logger.error("elasticsearch TransportClient create error!!!", e);
}
return client;
}
}
第三步
使用單元測試進行驗證
@Autowired
private TransportClient client;
@Test
public void contextLoads() {
System.out.println(client);
}
列印結果:
org.elasticsearch.transport.client.PreBuiltTransportClient@6c9b44bf
如果您覺得這篇博文對你有幫助,請點贊或者喜歡,讓更多的人看到,謝謝!
如果帥氣(美麗)、睿智(聰穎),和我一樣簡單善良的你看到本篇博文中存在問題,請指出,我虛心接受你讓我成長的批評,謝謝閱讀!
祝你今天開心愉快!
歡迎訪問我的csdn部落格,我們一同成長!
不管做什麼,只要堅持下去就會看到不一樣!在路上,不卑不亢!
© 每天都在變得更好的阿飛