springboot整合elasticsearch

#application.properties配置

##单集群配置,如果需要做多集群配置,参照application.properties.multicluster文件
elasticsearch.serverNames = default

##default集群配配置
# x-pack或者searchguard安全认证和口令配置
elasticUser=admin
elasticPassword=admin

#集群地址配置
#elasticsearch.rest.hostNames=10.180.211.27:9280,10.180.211.27:9281,10.180.211.27:9282
#Proxy Node
#elasticsearch.rest.hostNames=192.168.137.1:9206,192.168.137.1:9207
#elasticsearch.rest.hostNames=https://10.13.11.5:9200
elasticsearch.rest.hostNames=127.0.0.1:9200
#如果开启了https协议,则需要在elasticsearch地址中添加https://协议头
#elasticsearch.rest.hostNames=https://10.180.211.27:9280,https://10.180.211.27:9281,https://10.180.211.27:9282
elasticsearch.dateFormat=yyyy.MM.dd
elasticsearch.timeZone=Asia/Shanghai
#在控制台输出脚本调试开关showTemplate,false关闭,true打开,同时log4j至少是info级别
elasticsearch.showTemplate=false
#集群节点自动发现开关
elasticsearch.discoverHost=true
elasticsearch.useHttps=false

#设置slice scroll查询对应的线程数和等待队列数
elasticsearch.sliceScrollThreadCount=100
elasticsearch.sliceScrollThreadQueue=100
elasticsearch.sliceScrollBlockedWaitTimeout=0

#设置scroll查询对应的线程数和等待队列数
elasticsearch.scrollThreadCount=50
elasticsearch.scrollThreadQueue=50
elasticsearch.scrollBlockedWaitTimeout=0
elasticsearch.includeTypeName = false
#elasticsearch.slowDslThreshold = 1000
#elasticsearch.slowDslCallback=org.bboss.elasticsearchtest.crud.TestSlowDslCallback
##default连接池配置
http.timeoutConnection = 10000
http.timeoutSocket = 50000
http.connectionRequestTimeout=10000
http.retryTime = 3
http.retryInterval = 3000
http.maxLineLength = -1
http.maxHeaderCount = 200
http.maxTotal = 400
http.defaultMaxPerRoute = 200
http.soReuseAddress = false
http.soKeepAlive = false
http.timeToLive = 3600000
http.keepAlive = 3600000
# Using the keystore- and truststore file
## http.keystore = D:/workspace/bbossesdemo/eshelloword-booter/src/main/resources/sgadmin-keystore.jks
## http.keyPassword = 7240a0366eb6a764103e
## http.truststore = D:/workspace/bbossesdemo/eshelloword-booter/src/main/resources/truststore.jks
## http.trustPassword = 6aa4bd79096852203a5b

# Using PEM certificates
## http.pemCert = D:/workspace/bbossesdemo/eshelloword-booter/src/main/resources/sgadmin.crtfull.pem
## http.pemtrustedCA = D:/workspace/bbossesdemo/eshelloword-booter/src/main/resources/chain-ca.pem
## http.pemKey = D:/workspace/bbossesdemo/eshelloword-booter/src/main/resources/sgadmin.key.pem
## http.pemkeyPassword = 7240a0366eb6a764103e

# ssl 主机名称校验,是否采用default配置,
# 如果指定为default,就采用DefaultHostnameVerifier,否则采用 SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
http.hostnameVerifier =
# 驱逐http连接池中过期的http连接
http.evictExpiredConnections=true
#每隔多少毫秒校验空闲connection,自动释放无效链接
# -1 或者0不检查
http.validateAfterInactivity=3000
# 每次获取connection时校验连接,true,校验,false不校验,有性能开销

# 默认值false
http.staleConnectionCheckEnabled=false
#* 自定义重试控制接口,必须实现接口方法
#* public interface CustomHttpRequestRetryHandler  {
   
#* 	public boolean retryRequest(IOException exception, int executionCount, HttpContext context,ClientConfiguration configuration);
#* }
#* 方法返回true,进行重试,false不重试
# http.customHttpRequestRetryHandler=org.frameworkset.spi.remote.http.DefaultHttpRequestRetryHandler
http.customHttpRequestRetryHandler=org.frameworkset.spi.remote.http.ConnectionResetHttpRequestRetryHandler


# dsl配置文件热加载扫描时间间隔,毫秒为单位,默认5秒扫描一次,<= 0时关闭扫描机制
dslfile.refreshInterval = 5000
# 指定存放dsl配置文件的地址
#dslfile.dslMappingDir=D:/workspace/bbossesdemo/eshelloword-booter/src/main/resources/

# 演示数据库数据导入elasticsearch源配置
db.name = test
db.user = root
db.password = 123456
db.driver = com.mysql.jdbc.Driver
db.url = jdbc:mysql://localhost:3306/test?useCursorFetch=true
db.usePool = true
db.validateSQL = select 1
db.jdbcFetchSize = 10000
db.showsql = true

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * 获取application.properties中配置值
 * @author hy
 *
 */
@Configuration
public class ElasticsearchConfig {
   
    @Value("${db.name}")
    private String dbName;
    @Value("${db.user}")
    private String dbUser;
    @Value("${db.password}")
    private String dbPassword;
    @Value("${db.driver}")
    private String dbDriver;
    @Value("${db.url}")
    private String dbUrl;
    @Value("${db.usePool}")
    private Boolean dbUsePool;
    @Value("${db.validateSQL}")
    private String dbValidateSQL;
    @Value("${db.jdbcFetchSize}")
    private Long dbJdbcFetchSize;
    
	public String getDbName() {
   
		return dbName;
	}
	public void setDbName(String dbName) {
   
		this.dbName = dbName;
	}
	public String getDbUser() {
   
		return dbUser;
	}
	public void setDbUser(String dbUser) {
   
		this.dbUser = dbUser;
	}
	public String getDbPassword() {
   
		return dbPassword;
	}
	public void setDbPassword(String dbPassword) {
   
		this.dbPassword = dbPassword;
	}
	public String getDbDriver() {
   
		return dbDriver;
	}
	public void setDbDriver(String dbDriver) {
   
		this.dbDriver = dbDriver;
	}
	public String getDbUrl() {
   
		return dbUrl;
	}
	public void setDbUrl(String dbUrl) {
   
		this.dbUrl = dbUrl;
	}
	public Boolean getDbUsePool
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是SpringBoot整合elasticsearch的步骤: 1. 引入elasticsearch和spring-boot-starter-data-elasticsearch的依赖: ``` <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.12.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <version>2.4.5</version> </dependency> ``` 2. 配置elasticsearch连接信息: ``` spring.data.elasticsearch.cluster-nodes=localhost:9200 ``` 3. 创建实体类: ``` @Document(indexName = "my_index") public class MyEntity { @Id private String id; private String name; // getter and setter } ``` 4. 创建es的Repository: ``` public interface MyRepository extends ElasticsearchRepository<MyEntity, String> { } ``` 5. 在service中使用Repository: ``` @Service public class MyService { @Autowired private MyRepository myRepository; public void save(MyEntity entity) { myRepository.save(entity); } public List<MyEntity> search(String name) { return myRepository.findByName(name); } } ``` 6. 在controller中调用service: ``` @RestController public class MyController { @Autowired private MyService myService; @PostMapping("/save") public void save(@RequestBody MyEntity entity) { myService.save(entity); } @GetMapping("/search") public List<MyEntity> search(@RequestParam String name) { return myService.search(name); } } ``` 这样就可以通过SpringBoot整合elasticsearch实现数据的增删改查了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值