在 Openshift 容器平台(基于云的 Kubernetes 容器平台)中通过 spring 引导调用服务时名称或服务未知
Name or Service not Known when calling service via spring boot in Openshift Container Platform (a cloud-based Kubernetes container platform)
我有一个由后端(spring 引导)和搜索引擎 (elasticsearch) 组成的应用程序。在我将它部署到 OCP 之后,最初我使用命令“curl”从后端 pod 测试了两者之间的连接到 elasticsearch 服务 (https://service-name.namespace.svc.cluster.local:9200)
,它工作正常。这是图片:
但是,当我尝试从部署的后端应用程序中访问 elasticsearch 时,出现如下错误消息:
下面是我在 Spring 引导连接 Elasticsearch 时所做的配置:
package com.siolbca.config;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.siolbca.repository")
@ComponentScan(basePackages = "com.siolbca.services")
public class Config {
@Bean
public RestHighLevelClient client() {
ClientConfiguration clientConfiguration
= ClientConfiguration.builder()
.connectedTo("https://elasticsearch-siol-es-http.siolbca-dev.svc.cluster.local:9200")
.usingSsl()
.withBasicAuth("elastic","G0D1g6TurJ79pcxr1065pU0U")
.build();
return RestClients.create(clientConfiguration).rest();
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchRestTemplate(client());
}
}
我做过的一些事情是:
- 直接在后端配置中使用elasticsearch服务IP地址
https://elasticsearch-service-ipaddress:9200
- 从 elasticsearch 服务暴露一个路由,并把它放在后端配置中
https://elasticsearch-route:443
- 将服务url改为
有谁知道为什么我的后端应用程序无法与 elasticsearch 服务通信,即使两个 pods 能够连接?任何答案都会非常有帮助。谢谢。
编辑
这是我的 pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.siolbca</groupId>
<artifactId>siolbca</artifactId>
<version>1.0</version>
<name>siolbca</name>
<description>Backend project for SIOLBCA</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我认为 elasticsearch 构建器 .connectedTo
方法需要格式 host:port
,即没有协议。
所以尝试:
.connectedTo("elasticsearch-siol-es-http.siolbca-dev.svc.cluster.local:9200")
我有一个由后端(spring 引导)和搜索引擎 (elasticsearch) 组成的应用程序。在我将它部署到 OCP 之后,最初我使用命令“curl”从后端 pod 测试了两者之间的连接到 elasticsearch 服务 (https://service-name.namespace.svc.cluster.local:9200)
,它工作正常。这是图片:
但是,当我尝试从部署的后端应用程序中访问 elasticsearch 时,出现如下错误消息:
下面是我在 Spring 引导连接 Elasticsearch 时所做的配置:
package com.siolbca.config;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.siolbca.repository")
@ComponentScan(basePackages = "com.siolbca.services")
public class Config {
@Bean
public RestHighLevelClient client() {
ClientConfiguration clientConfiguration
= ClientConfiguration.builder()
.connectedTo("https://elasticsearch-siol-es-http.siolbca-dev.svc.cluster.local:9200")
.usingSsl()
.withBasicAuth("elastic","G0D1g6TurJ79pcxr1065pU0U")
.build();
return RestClients.create(clientConfiguration).rest();
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchRestTemplate(client());
}
}
我做过的一些事情是:
- 直接在后端配置中使用elasticsearch服务IP地址
https://elasticsearch-service-ipaddress:9200
- 从 elasticsearch 服务暴露一个路由,并把它放在后端配置中
https://elasticsearch-route:443
- 将服务url改为
有谁知道为什么我的后端应用程序无法与 elasticsearch 服务通信,即使两个 pods 能够连接?任何答案都会非常有帮助。谢谢。
编辑
这是我的 pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.siolbca</groupId>
<artifactId>siolbca</artifactId>
<version>1.0</version>
<name>siolbca</name>
<description>Backend project for SIOLBCA</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我认为 elasticsearch 构建器 .connectedTo
方法需要格式 host:port
,即没有协议。
所以尝试:
.connectedTo("elasticsearch-siol-es-http.siolbca-dev.svc.cluster.local:9200")