当前位置: 首页 > news >正文

做网站要学编程麽网上互联网推广

做网站要学编程麽,网上互联网推广,网站建设多长时间,市桥有经验的网站建设文章目录 1 背景2 前言3 Java如何操作ES3.1 引入依赖3.2 依赖介绍3.3 隐藏依赖3.4 初始化客户端(获取ES连接)3.5 发送请求给ES 1 背景 上篇学习了0基础学习Elasticsearch-Quick start,随后本篇研究如何使用Java操作ES 2 前言 建议通篇阅读再回…

在这里插入图片描述

文章目录

  • 1 背景
  • 2 前言
  • 3 Java如何操作ES
    • 3.1 引入依赖
    • 3.2 依赖介绍
    • 3.3 隐藏依赖
    • 3.4 初始化客户端(获取ES连接)
    • 3.5 发送请求给ES

1 背景

上篇学习了0基础学习Elasticsearch-Quick start,随后本篇研究如何使用Java操作ES

2 前言

  1. 建议通篇阅读再回头来跟着敲代码
  2. 建议先阅读Java连接ES云以及如何使用CA证书连接、ES鉴权连接对Java连接ES有哪几种方法有个认知,阅读如何Reading responses,阅读如何同步、异步发送请求
  3. ES 8版本后建议使用Java Low Level REST ClientJava客户端,本篇采用该客户端

3 Java如何操作ES

3.1 引入依赖

<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>8.13.4</version>
</dependency>

3.2 依赖介绍

low-level Java REST client内部采用了Apache Http Async Client来发送HTTP请求,内部含有以下这些依赖,如果遇到依赖冲突,需要解决:

  • org.apache.httpcomponents:httpasyncclient
  • org.apache.httpcomponents:httpcore-nio
  • org.apache.httpcomponents:httpclient
  • org.apache.httpcomponents:httpcore
  • commons-codec:commons-codec
  • commons-logging:commons-logging

3.3 隐藏依赖

如果遇到上面列出的依赖冲突,可以使用下面这个方法来解决,pom文件加入下面代码:

<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.1.0</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><relocations><relocation><pattern>org.apache.http</pattern><shadedPattern>hidden.org.apache.http</shadedPattern></relocation><relocation><pattern>org.apache.logging</pattern><shadedPattern>hidden.org.apache.logging</shadedPattern></relocation><relocation><pattern>org.apache.commons.codec</pattern><shadedPattern>hidden.org.apache.commons.codec</shadedPattern></relocation><relocation><pattern>org.apache.commons.logging</pattern><shadedPattern>hidden.org.apache.commons.logging</shadedPattern></relocation></relocations></configuration></execution></executions></plugin></plugins>
</build>

3.4 初始化客户端(获取ES连接)

概括:通过账号密码来获取连接。笔者这里通过注入bean的方式初始化ES客户端并交给Spring管理

@Slf4j
@Configuration
public class EsClient {public static final String HOST = "192.168.90.128";public static final int PORT = 9200;public static final String PROTOCOL = "https";public static final String username = "elastic";public static final String password = "84fZ4PuywWr_unOcr+JH";@Beanpublic RestClient restClient() {RestClientBuilder clientBuilder = RestClient.builder(new HttpHost(HOST, PORT, PROTOCOL)).setCompressionEnabled(true);CredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username, password));try {SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build();clientBuilder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setSSLContext(sslContext).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).setDefaultCredentialsProvider(credentialsProvider));} catch (Exception e) {log.error("EsClient_elasticsearchClient, init RestClient error. error msg:{}", e.getMessage());}return clientBuilder.build();}
}

3.5 发送请求给ES

写一个测试类来尝试操作ES:

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GmallEsApplication.class)
@ContextConfiguration
public class EsClientTest{@Resourceprivate RestClient restClient;@Testpublic void performRequest() throws IOException {Request request = new Request("GET","/");Response response = restClient.performRequest(request);log.info("response:{}", JSON.toJSONString(response));if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {System.err.println("Method failed: " + response.getStatusLine());} else {HttpEntity entity = response.getEntity();String responseBody = EntityUtils.toString(entity);log.info("responseBody:{}", responseBody);}}}
http://www.khdw.cn/news/37305.html

相关文章:

  • 广西南宁b2b网站建设百度推广联系人
  • 织梦添加网站音乐百度知道问答平台
  • 在线手机网站预览网络推广的网站有哪些
  • 遵义市红花岗区疫情最新消息seo排名软件怎么做
  • 专题网站建设qq营销软件
  • 做网站推广费用关键词优化公司靠谱推荐
  • 做网站属于什么费用竞价托管推广
  • DS716 II 做网站网站买卖交易平台
  • 云南做网站报价长沙关键词自然排名
  • 济南专业手机端网站建设广州新闻最新消息今天
  • 做标志的网站外贸接单平台
  • 制作公司的网站广东公司搜索seo哪家强
  • 云南网站建设哪家好指数基金
  • 徐州最好网站建设郑州百度seo
  • 搜索引擎优化的目标体系包括哪些seo公司优化方案
  • 软件开发学校在哪里怎么寻找网站关键词并优化
  • 网站渗透上海最近三天的新闻
  • 网站做301打不开自己如何制作网站
  • 建设中网站谷歌seo推广服务
  • oeasy教html_css-html网站建设_前端css教程网站的推广优化
  • 给中小企业提供网站建设服务网络推广公司如何做
  • 建设银行的官方网站电脑版个人网站网页首页
  • 买个域名后怎么做网站百度收录怎么弄
  • 徐家汇网站建设软文营销写作技巧
  • 网站开发常用技术电商平台网站
  • 网站对公司的作用是什么意思少儿编程
  • 正规的网站制作电话多少百度一下百度一下你就知道
  • 浙江省住房和建设厅网站盐城seo排名
  • 大量网站开发视频 文档VIP资源产品推广思路
  • 河北网站制作淘宝热搜关键词排行榜