es springboot " />

扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

springboot闆嗘垚es闆嗙兢鎬庝箞閰嶇疆

扬州沐宇科技
2024-01-27 21:56:05
es springboot

瑕佸湪Spring Boot椤圭洰涓泦鎴怑lasticsearch闆嗙兢锛屼綘闇€瑕佽繘琛屼互涓嬮厤缃細

  1. 娣诲姞Elasticsearch鐨勪緷璧栵細鍦?code>pom.xml鏂囦欢涓坊鍔燛lasticsearch鐨勪緷璧栵細
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
  1. 閰嶇疆Elasticsearch杩炴帴锛氬湪application.properties鎴?code>application.yml鏂囦欢涓厤缃瓻lasticsearch杩炴帴淇℃伅锛屽寘鎷泦缇よ妭鐐广€佺鍙g瓑锛?/li>
spring.elasticsearch.rest.uris=http://localhost:9200,http://localhost:9201,http://localhost:9202

鎴栬€呬娇鐢╕AML鏍煎紡鐨勯厤缃細

spring:
  elasticsearch:
    rest:
      uris: http://localhost:9200,http://localhost:9201,http://localhost:9202
  1. 閰嶇疆Elasticsearch鐨勮繛鎺ユ睜锛氬湪application.properties鎴?code>application.yml鏂囦欢涓厤缃瓻lasticsearch鐨勮繛鎺ユ睜鐩稿叧灞炴€э紝渚嬪鏈€澶ц繛鎺ユ暟銆佹渶澶ц矾鐢辨暟绛夛細
spring.elasticsearch.rest.max-connections=10
spring.elasticsearch.rest.max-per-route-connections=5

鎴栬€呬娇鐢╕AML鏍煎紡鐨勯厤缃細

spring:
  elasticsearch:
    rest:
      max-connections: 10
      max-per-route-connections: 5
  1. 鍒涘缓Elasticsearch鐨勫鎴风锛氬湪Spring Boot鐨勯厤缃被涓垱寤篍lasticsearch鐨凴estHighLevelClient bean锛岀敤浜庝笌Elasticsearch杩涜浜や簰锛?/li>
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticsearchConfig {

    @Bean
    public RestHighLevelClient elasticsearchClient() {
        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http"),
                        new HttpHost("localhost", 9201, "http"),
                        new HttpHost("localhost", 9202, "http")
                )
        );
    }
}

灏嗕笂杩颁唬鐮佹斁缃湪涓€涓狜Configuration娉ㄨВ鐨勭被涓紝Spring Boot浼氳嚜鍔ㄥ皢璇ョ被涓殑@Bean娉ㄨВ鐨勬柟娉曟敞鍐屼负bean銆?/p>

浠ヤ笂灏辨槸鍦⊿pring Boot椤圭洰涓泦鎴怑lasticsearch闆嗙兢鐨勯厤缃楠ゃ€備綘鍙互鏍规嵁瀹為檯闇€姹傝繘琛岀浉搴旂殑璋冩暣鍜屼紭鍖栥€?/p>

扫码添加客服微信