搭建模块
创建二个项目
gmall-list-service的appliction.properties:
server.port=8073 spring.datasource.url=jdbc:mysql://localhost:3306/gmall?characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=root mybatis.configuration.map-underscore-to-camel-case=true mybatis.mapper-locations=classpath:mapper/*Mapper.xml spring.dubbo.registry.protocol=zookeeper spring.dubbo.registry.address=192.168.0.100:2181 spring.dubbo.application.name=gmall-list-service spring.dubbo.protocol.name=dubbo spring.dubbo.base-package=com.javawxid spring.elasticsearch.jest.uris=http://192.168.0.100:9200 logging.level.root=info
gmall-list-service的pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>com.javawxid.gmall</groupId> <artifactId>gmall-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.javawxid.gmall</groupId> <artifactId>gmall-list-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gmall-list-service</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.javawxid.gmall</groupId> <artifactId>gmall-service-util</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.javawxid.gmall</groupId> <artifactId>gmall-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <!-- 导入Jest依赖 --> <!-- https://mvnrepository.com/artifact/io.searchbox/jest --> <dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> <version>5.3.3</version> </dependency> <!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna --> <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> <version>4.5.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
其中jest和jna请将版本号,部分纳入gmall-parent中管理。spring-boot-starter-data-elasticsearch不用管理版本号,其版本跟随springboot的1.5.10大版本号。
gmall-list-web的appliction.properties:
server.port=8083 spring.dubbo.registry.address=192.168.0.100:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.application.name=gmall-list-web spring.dubbo.base-package=com.javawxid spring.dubbo.consumer.timeout=600000 spring.dubbo.consumer.check=false spring.thymeleaf.mode=LEGACYHTML5 spring.thymeleaf.cache=false logging.level.root=info
gmall-list-web的pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>com.javawxid.gmall</groupId> <artifactId>gmall-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.javawxid.gmall</groupId> <artifactId>gmall-list-web</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gmall-list-web</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.javawxid.gmall</groupId> <artifactId>gmall-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.javawxid.gmall</groupId> <artifactId>gmall-web-util</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、 关于es 的java 客户端的选择
目前市面上有两类客户端
一类是TransportClient 为代表的ES原生客户端,不能执行原生dsl语句必须使用它的Java api方法。
另外一种是以Rest Api为主的missing client,最典型的就是jest。 这种客户端可以直接使用dsl语句拼成的字符串,直接传给服务端,然后返回json字符串再解析。
两种方式各有优劣,但是最近elasticsearch官网,宣布计划在7.0以后的版本中废除TransportClient。以RestClient为主。
所以在官方的RestClient 基础上,进行了简单包装的Jest客户端,就成了首选,而且该客户端也与springboot完美集成。
3、插入数据
PUT /movie_chn/movie/1 { "id":1, "name":"红海行动", "doubanScore":8.5, "actorList":[ {"id":1,"name":"张译"}, {"id":2,"name":"海清"}, {"id":3,"name":"张涵予"} ] } PUT /movie_chn/movie/2 { "id":2, "name":"湄公河行动", "doubanScore":8.0, "actorList":[ {"id":3,"name":"张涵予"} ] } PUT /movie_chn/movie/3 { "id":3, "name":"红海事件", "doubanScore":5.0, "actorList":[ {"id":4,"name":"张晨"} ] }
4、在测试类中测试ES
package com.javawxid; import io.searchbox.client.JestClient; import io.searchbox.core.Search; import io.searchbox.core.SearchResult; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; import java.util.HashMap; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest public class GmallListServiceApplicationTests { @Autowired JestClient jestClient; @Test public void testEs() throws IOException { String query="{\n" + " \"query\": {\n" + " \"match\": {\n" + " \"actorList.name\": \"张译\"\n" + " }\n" + " }\n" + "}"; Search search = new Search.Builder(query).addIndex("movie_chn").addType("movie").build(); SearchResult result = jestClient.execute(search); List<SearchResult.Hit<HashMap, Void>> hits = result.getHits(HashMap.class); for (SearchResult.Hit<HashMap, Void> hit : hits) { HashMap source = hit.source; System.err.println("source = " + source); } } }
打印结果:
source = {doubanScore=8.5, es_metadata_id=1, name=红海行动, actorList=[{id=1.0, name=张译}, {id=2.0, name=海清}, {id=3.0, name=张涵予}], id=1.0} source = {doubanScore=5.0, es_metadata_id=3, name=红海事件, actorList=[{id=4.0, name=张晨}], id=3.0} source = {doubanScore=8.0, es_metadata_id=2, name=湄公河行动, actorList=[{id=3.0, name=张涵予}], id=2.0}
以上技术方面的准备就做好了。