欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ELASTICSEARCH的java客户端和springboot整合(二)

程序员文章站 2022-07-01 16:04:12
...

                      SpringBoot整合TransportClient

目录

                      SpringBoot整合TransportClient

一.1redis的整合

1.启动一个redis的结点(单节点)

2.配置单节点的内容

3.配置类中读取属性生成返回的JedisShardedPool

一.2rabbitmq的整合

1.直接利用springboot自动整合

2.生产端注入RabbitTemplate

3.消费端异步监听的声明式注解@RabbitListener

一.3es的整合

3.配置类读取属性

4.配置类初始化TransportClient对象

6.创建一个main类

7.创建ESController类

1.先添加一些数据

2.运行代码测试

1.redis的整合


一.1redis的整合

1.启动一个redis的结点(单节点)

2.配置单节点的内容

  • redis.nodes
  • redis.maxTotal
  • redis.minIdle
  • redis.maxIdle

3.配置类中读取属性生成返回的JedisShardedPool

一.2rabbitmq的整合

1.直接利用springboot自动整合

2.生产端注入RabbitTemplate

3.消费端异步监听的声明式注解@RabbitListener

一.3es的整合

  • springboot版本1.5.2.RELEASE-->2.X的es版本
  • es的版本历史:随着es的发展形成了一个庞大的家族
  • springboot版本2.1.X.RELEASE-->5.x以上的es

手动整合:TransportClient交给框架维护,注入使用

1.Pom.xml依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.tedu</groupId>
  <artifactId>springboot-elasticsearch</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springboot-elasticsearch</name>
  <url>http://maven.apache.org</url>
  <parent>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-parent</artifactId>
  	<version>1.5.2.RELEASE</version>
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    	<groupId>org.elasticsearch</groupId>
    	<artifactId>elasticsearch</artifactId>
    	<version>5.5.2</version>
    </dependency>
    <!-- es的java客户端 TransportClient  -->
    <dependency>
    	<groupId>org.elasticsearch.client</groupId>
    	<artifactId>transport</artifactId>
    	<version>5.5.2</version>
    </dependency>
    <!-- Java对象转换json字符串 -->
    <dependency>
    	<groupId>com.fasterxml.jackson.core</groupId>
    	<artifactId>jackson-databind</artifactId>
    	<version>2.8.7</version>
    </dependency>
  </dependencies>
</project>

2.准备配置properties

ELASTICSEARCH的java客户端和springboot整合(二)

需要多个可以追加

3.配置读取属性

@ConfigurationProperties(prefix="es")

private List<String> nodes;

ELASTICSEARCH的java客户端和springboot整合(二)

4.配置类初始化TransportClient对象

	//初始化连接对象
	@Bean
	public TransportClient initEsClient() throws Exception{
		//新建一个client对象,setting对象(配置集群名称)
		TransportClient client=
				new PreBuiltTransportClient(Settings.EMPTY);
		//添加负载均衡器的结点信息,如果是集群,可以多次添加多个节点信息
		
		for (String node : nodes) {//10.9.9.9:9300
			String host=node.split(":")[0];
			int port=Integer.parseInt(node.split(":")[1]);
			client.addTransportAddress(new InetSocketTransportAddress(
					InetAddress.getByName(host), port));
		}
		return client;
	}
	public List<String> getNodes() {
		return nodes;
	}

	public void setNodes(List<String> nodes) {
		this.nodes = nodes;
	}
	

6.创建一个main类

package com.jt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StarterES {
	public static void main(String[] args) {
		SpringApplication.run(StarterES.class, args);
	}
}

7.创建ESController类

@RestController
public class ESController {
	@Autowired
	private TransportClient client;
	@RequestMapping("search")
	public String searchArticle
	(String name,String text,Integer page,Integer rows){
		MatchQueryBuilder query1=QueryBuilders.matchQuery(name, text);//进行默认的分词器解析
		//java 编,程,思,想
		//搜索api获取搜索结果
		SearchResponse response1 = client.prepareSearch("index06")
		.setFrom((page-1)*rows).setSize(rows).setQuery(query1).get();
		long total = response1.getHits().totalHits;
		System.out.println(total);
		SearchHit[] hits = response1.getHits().getHits();
		String reMsg="";
		for (SearchHit hit : hits) {
			reMsg=reMsg+hit.getSourceAsString();
		}
		return reMsg;
	}
}

1.先添加一些数据

curl -XPUT -d '{"id":1,"title":"es简介1","content":"es好用好用真好用1"}' http://10.42.60.249:9200/index06/article/1
curl -XPUT -d '{"id":2,"title":"es简介2","content":"es好用好用真好用2"}' http://10.42.60.249:9200/index06/article/2
curl -XPUT -d '{"id":3,"title":"es简介3","content":"es好用好用真好用3"}' http://10.42.60.249:9200/index06/article/3
curl -XPUT -d '{"id":4,"title":"es简介4","content":"es好用好用真好用4"}' http://10.42.60.249:9200/index06/article/4
curl -XPUT -d '{"id":5,"title":"es简介5","content":"es好用好用真好用5"}' http://10.42.60.249:9200/index06/article/5
curl -XPUT -d '{"id":6,"title":"es简介6","content":"es好用好用真好用6"}' http://10.42.60.249:9200/index06/article/6
curl -XPUT -d '{"id":7,"title":"es简介7","content":"es好用好用真好用7"}' http://10.42.60.249:9200/index06/article/7
curl -XPUT -d '{"id":8,"title":"es简介8","content":"es好用好用真好用8"}' http://10.42.60.249:9200/index06/article/8
curl -XPUT -d '{"id":9,"title":"es简介9","content":"es好用好用真好用9"}' http://10.42.60.249:9200/index06/article/9
curl -XPUT -d '{"id":10,"title":"es简介10","content":"es好用好用真好用10"}' http://10.42.60.249:9200/index06/article/10
curl -XPUT -d '{"id":11,"title":"es简介11","content":"es好用好用真好用11"}' http://10.42.60.249:9200/index06/article/11

2.运行代码测试

ELASTICSEARCH的java客户端和springboot整合(二)

实现通过代码获取ES中的数据

ELASTICSEARCH的java客户端和springboot整合(二)