从我所做的研究看来,由于HTTP仅支持Amazon Elasticsearch,因此两者无法协同工作。
希望有人能澄清一下,实际上它不是Spring Data Elasticsearch所能实现的。
答案 0 :(得分:5)
从不同的讨论:
- Spring data ES and searchly
- port for the transport protocol
和AWS documentation on ES service limitations;到底部,最后一行说:
该服务支持端口80上的HTTP,但不支持TCP传输。
目前还不能使用只使用Java API的Spring Data ES和只能通过REST使用的Amazon服务。
答案 1 :(得分:2)
有一个很好的项目提供了与AWS托管ES服务一起使用的Spring Data ES的Jest实现。
答案 2 :(得分:1)
看起来像3.2.0版中的Spring数据弹性搜索可与http rest客户端一起使用,因此可以通过Rest API和端口443连接到aws弹性实例。它们以某种方式将spring-data-jest方法集成到spring数据中。 我使用RestHighLevelClient:
@Bean
public RestHighLevelClient client() {
return new RestHighLevelClient(RestClient.builder(HttpHost.create(awsUrl)));
}
awsUrl格式为: https://some_aws_generated_address.us-east-n.es.amazonaws.com:443
注意:如果您将Spring Boot与默认的bom.xml一起使用,则需要将spring boot升级到2.2.1.RELEASE或更高版本
答案 3 :(得分:0)
可以将Spring Data Elasticsearch与Amazon Elasticsearch一起使用
摘自Spring数据弹性搜索doc
TransportClient自Elasticsearch 7起不推荐使用,并且将 在Elasticsearch 8中删除了。这有利于Java High Level REST 客户。 Spring Data Elasticsearch将支持TransportClient作为 只要它在Elasticsearch中可用。
Java高级REST客户端现在是的默认客户端 Elasticsearch,它可以直接替换 TransportClient接受并返回相同的结果 请求/响应对象,因此取决于Elasticsearch 核心项目
Spring Data ElasticSearch 简化了 ElasticSearch 的最新标准,因此从spring-data-elasticsearch:3.2.X
起,它提供了一种实现自定义RestHighLevelClient
的灵活方法。(link)
即使可以使用带有或不带有身份验证的基于HTTP的弹性搜索API调用,它也无法解决与AWS弹性搜索API调用相关的问题。
由于对 AWS服务或APIGW支持的服务的任何 HTTP请求,必须遵循 "Signature Version 4 Signing Process(SigV4)"
,这最终会将身份验证信息添加到AWS HTTP发送的请求。为了安全起见,大多数对AWS的请求必须使用访问密钥签名,该访问密钥由 accesskey ID
和 secret access key
组成。因此,在调用AWS ElasticSearch服务时,我们必须遵循标准。
让我们深入了解代码并深入研究实现
请按照以下步骤操作:
第1步:添加必需的依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-elasticsearch</artifactId>
<version>1.11.346</version>
</dependency>
步骤2:添加AWS CredentialsProvider
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AWSCredentialsConfiguration {
@Value("${aws.es.accessKey}")
private String esAccessKey = null;
@Value("${aws.es.secretKey}")
private String esSecretKey = null;
@Bean
public AWSStaticCredentialsProvider awsDynamoCredentialsProviderDevelopment() {
return new AWSStaticCredentialsProvider(new BasicAWSCredentials(
esAccessKey, esSecretKey));
}
}
或者,如果您的应用程序在AWS实例上运行,并且您不想使用属性驱动/硬编码的AccessKey和SecretKey,则必须将 IAM角色分配给您的 Amazon ECS任务 for more。
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AWSCredentialsConfiguration {
@Bean
public AWSCredentialsProvider amazonAWSCredentialsProvider() {
return new EC2ContainerCredentialsProviderWrapper();
}
}
第3步:添加ElasticSearchRestClientConfiguration
**RestHighLevelClient**
提供**AbstractElasticsearchConfiguration#elasticsearchClient()**
的自定义实现。这样,我们将合并的 "elasticsearchOperations"
, "elasticsearchTemplate"
bean的customRestHighLevelClient注入到弹簧容器中。HttpRequestInterceptor
是需要注意的另一件事。特别感谢AWSRequestSigningApacheInterceptor.java,这是 AWSLabs
提供的示例实现,可帮助我们使用 AWS4Signer
将拦截器添加到RestClient机制。@EnableElasticsearchRepositories
注释帮助启用了Elasticsearch数据存储库。import com.amazonaws.auth.AWS4Signer;
import com.amazonaws.auth.AWSCredentialsProvider;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequestInterceptor;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.demo.aws.elasticsearch.data.repository")
public class ElasticSearchRestClientConfiguration extends AbstractElasticsearchConfiguration {
@Value("${aws.es.endpoint}")
private String endpoint = null;
@Value("${aws.es.region}")
private String region = null;
@Autowired
private AWSCredentialsProvider credentialsProvider = null;
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
AWS4Signer signer = new AWS4Signer();
String serviceName = "es";
signer.setServiceName(serviceName);
signer.setRegionName(region);
HttpRequestInterceptor interceptor = new AWSRequestSigningApacheInterceptor(serviceName, signer, credentialsProvider);
return new RestHighLevelClient(RestClient.builder(HttpHost.create(endpoint)).setHttpClientConfigCallback(e -> e.addInterceptorLast(interceptor)));
}
}
布拉沃祖鲁!而已。我们已经完成了配置部分。现在,通过此解决方案,您可以利用spring-data的弹性优势以及Amazon Elastic Search Service。完整的解决方案已记录在Medium Post
中答案 4 :(得分:0)
我在与 aws es 堆栈相同的配置中收到以下错误,如本文所示 https://medium.com/@prasanth_rajendran/how-to-integrate-spring-boot-elasticsearch-data-with-aws-445e6fc72998
Error creating bean with name 'supplierContacts' defined in file ... Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'supplierContactListDaoImpl' defined in file [SupplierContactListDaoImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'supplierContactListRepository': Cannot resolve reference to bean 'elasticsearchTemplate' while setting bean property 'elasticsearchOperations'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticsearchOperations' defined in class path resource [ElasticSearchConfig.class]: Unsatisfied dependency expressed through method 'elasticsearchOperations' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticsearchEntityMapper' defined in class path resource [ElasticSearchConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter]: Factory method 'elasticsearchEntityMapper' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/mapping/model/EntityInstantiators