添加elasticsearch快速启动模块
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
nbproject/private/
|
||||
build/
|
||||
nbbuild/
|
||||
dist/
|
||||
nbdist/
|
||||
.nb-gradle/
|
||||
@@ -0,0 +1,9 @@
|
||||
# 欢迎查看micro-fast-boot-starter-elasticsearch项目说明
|
||||
[](LICENSE)
|
||||
## 项目介绍
|
||||
`micro-fast-boot-starter-elasticsearch`项目用于快速开启spring集成elasticsearch的项目,将本项目使用maven发布到本地仓库,在其他项
|
||||
目中引入该依赖进行相关配置即可使用java操作elasticsearch
|
||||
## application.yml文件中的相关配置
|
||||
- `elasticsearch.hosts`es节点的ip:port数组(必要参数)
|
||||
- `elasticsearch.settings`构建es客户端的Settings实例的key:value数组 例如设置集群名称`cluster.name:lsy`(必要参数)
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
<?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>
|
||||
|
||||
<groupId>com.micro.fast</groupId>
|
||||
<artifactId>micro-fast-boot-starter-elasticsearch</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>boot-starter-elasticsearch</name>
|
||||
<description>boot starter elasticsearch</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.7.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<elasticsearch.version>5.6.0</elasticsearch.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!--elasticsearch客户端依赖-->
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>transport</artifactId>
|
||||
<version>${elasticsearch.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.micro.fast.elasticsearch;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BootStarterElasticsearchApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BootStarterElasticsearchApplication.class, args);
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.micro.fast.elasticsearch.config;
|
||||
|
||||
import com.micro.fast.elasticsearch.config.param.ElasticsearchParamConfig;
|
||||
import org.elasticsearch.client.transport.TransportClient;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.transport.InetSocketTransportAddress;
|
||||
import org.elasticsearch.transport.client.PreBuiltTransportClient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* 定义elasticsearch客户端的bean
|
||||
* create by lishouyu 2017/09/23
|
||||
* email 18332763730@163.com
|
||||
*
|
||||
* last edit lishouyu 2017/09/24
|
||||
* email 18332763730@163.com
|
||||
*/
|
||||
@Configuration
|
||||
public class ElasticsearchConfig {
|
||||
|
||||
/**
|
||||
* 记录es客户端生成日志
|
||||
*/
|
||||
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchParamConfig.class);
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchParamConfig elasticsearchParamConfig;
|
||||
|
||||
/**
|
||||
* 定义es客户端的bean
|
||||
* @return client
|
||||
* @throws UnknownHostException
|
||||
*/
|
||||
@Bean("esClient")
|
||||
public TransportClient transportClient() throws UnknownHostException {
|
||||
//1.根据application.yml中的参数来构建es客户端的配置对象
|
||||
Settings.Builder builder = Settings.builder();
|
||||
ArrayList<String> settingsParam = elasticsearchParamConfig.getSettings();
|
||||
if (settingsParam != null && !settingsParam.isEmpty()) {
|
||||
for (String setting:settingsParam) {
|
||||
String[] keyValue = setting.split(":");
|
||||
String key = keyValue[0];
|
||||
String value = keyValue[1];
|
||||
builder.put(key, value);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("micro-fast elasticsearch 添加elasticsearch的Settings配置: {}:{}", key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
//2.创建配置对象将配置对象放入es客户端对象之中
|
||||
Settings settings = builder.build();
|
||||
TransportClient client = new PreBuiltTransportClient(settings);
|
||||
//3.根据配置文件创建es节点,然后放入到es客户端对象之中
|
||||
ArrayList<String> hosts = elasticsearchParamConfig.getHosts();
|
||||
if (hosts != null && !hosts.isEmpty()) {
|
||||
for (String host :hosts) {
|
||||
String[] ipPort = host.split(":");
|
||||
String ip = ipPort[0];
|
||||
String port = ipPort[1];
|
||||
InetSocketTransportAddress node = new InetSocketTransportAddress(
|
||||
InetAddress.getByName(ip), Integer.valueOf(port));
|
||||
//向客户端中添加节点
|
||||
client.addTransportAddress(node);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("micro-fast elasticsearch 添加elasticsearch节点: {}:{}", ip, port);
|
||||
}
|
||||
}
|
||||
}
|
||||
//返回elasticsearch客户端
|
||||
return client;
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
package com.micro.fast.elasticsearch.config;
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.micro.fast.elasticsearch.config.param;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* elasticsearch客户端的配置类
|
||||
* create by lishouyu 2017/09/24
|
||||
* email 18332763730@163.com
|
||||
*
|
||||
* last edit lishouyu 2017/09/24
|
||||
* email 18332763730@163.com
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "elasticsearch")
|
||||
@AutoConfigureBefore(value = com.micro.fast.elasticsearch.config.ElasticsearchConfig.class)
|
||||
@Validated
|
||||
public class ElasticsearchParamConfig {
|
||||
/**
|
||||
* elasticsearch集群的ip:port数组,至少要有一个
|
||||
*/
|
||||
@NotNull
|
||||
private ArrayList<String> hosts;
|
||||
/**
|
||||
* elasticsearch客户端Settings类的配置参数 key:value数组的形式,至少要有一个
|
||||
*/
|
||||
@NotNull
|
||||
private ArrayList<String> settings;
|
||||
|
||||
public ArrayList<String> getHosts() {
|
||||
return hosts;
|
||||
}
|
||||
|
||||
public void setHosts(ArrayList<String> hosts) {
|
||||
this.hosts = hosts;
|
||||
}
|
||||
|
||||
public ArrayList<String> getSettings() {
|
||||
return settings;
|
||||
}
|
||||
|
||||
public void setSettings(ArrayList<String> settings) {
|
||||
this.settings = settings;
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
package com.micro.fast.elasticsearch.config.param;
|
||||
@@ -0,0 +1,5 @@
|
||||
elasticsearch:
|
||||
hosts:
|
||||
- localhost:9300
|
||||
settings:
|
||||
- cluster.name:lsy
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.micro.fast.elasticsearch;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class BootStarterElasticsearchApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -82,6 +82,7 @@
|
||||
<!--</dependency>-->
|
||||
|
||||
|
||||
|
||||
<!--对断路由情况进行聚合-->
|
||||
<!--<dependency>-->
|
||||
<!--<groupId>org.springframework.cloud</groupId>-->
|
||||
|
||||
@@ -7,6 +7,8 @@ import org.springframework.cloud.client.SpringCloudApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
||||
import org.springframework.cloud.netflix.feign.FeignClient;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
@SpringCloudApplication
|
||||
@EnableFeignClients
|
||||
|
||||
@@ -56,12 +56,12 @@ public class DataSourceConfig {
|
||||
//2.设置servlet初始化的参数,如果传入了参数默认的配置就不会生效
|
||||
Map<String,String> initParams = new HashMap<>();
|
||||
ArrayList<String> servletInitParams = druidParamConfig.getServletInitParams();
|
||||
if (servletInitParams!=null&&!servletInitParams.isEmpty()){
|
||||
if (servletInitParams != null && !servletInitParams.isEmpty()){
|
||||
for (String param: servletInitParams) {
|
||||
String[] keyValue = param.split(":");
|
||||
initParams.put(keyValue[0],keyValue[1]);
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
//设置登录密码
|
||||
initParams.put("loginUsername","micro");
|
||||
initParams.put("loginPassword","fast");
|
||||
|
||||
Reference in New Issue
Block a user