【问题标题】:InfluxDB docker image doesn't work with my Spring Boot application imageInfluxDB docker 映像不适用于我的 Spring Boot 应用程序映像
【发布时间】:2020-12-16 06:51:45
【问题描述】:

我正在尝试使用 docker-compose --build 命令构建应用程序。但是,我收到以下错误:

nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 
'influxDB' defined in class path resource [com/order/app/config/InfluxDatabaseConfig.class]: Bean instantiation via factory method failed; 
nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [org.influxdb.InfluxDB]: 
Factory method 'influxDB' threw exception; 
nested exception is org.influxdb.InfluxDBIOException: 
java.net.ConnectException: Failed to connect to localhost/127.0.0.1:8086

这是我的 docker-compose.yml:

    version: '3'
services:
  influx:
    image: influxdb
    container_name: influxdb
    environment:
      INFLUXDB_DB: test
      INFLUXDB_ADMIN_USER: admin
      INFLUXDB_ADMIN_PASSWORD: admin
      INFLUXDB_HTTP_AUTH_ENABLED: "true"
    ports:
      - 8081:8081/tcp
  backend:
    container_name: order-app
    image: order-app
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - influx

这是我的 InfluxDatabaseConfig 类:

@Configuration
@EnableConfigurationProperties(InfluxDBProperties.class)
public class InfluxDatabaseConfig {
    @Bean
    public InfluxDB influxDB() {
        InfluxDB connection = InfluxDBFactory.connect("http://localhost:8086", "admin", "admin");
        connection.createDatabase("test");
        connection.setDatabase("test");
        return connection;
    }
}

application.properties 文件:

server.port=8080
spring.influxdb.database=test
spring.influxdb.url=http://localhost:8086
spring.influxdb.username=admin
spring.influxdb.password=admin
spring.influxdb.retention-policy=autogen
spring.influxdb.gzip=true

有人知道我的代码有什么问题吗?非常感谢您的帮助。

【问题讨论】:

    标签: spring spring-boot docker


    【解决方案1】:

    您的流入容器和后端在两个不同的容器中运行,这意味着它们是两台不同的机器,每台机器都有自己的 ip。

    你不能使用localhost:8086从你的后端容器调用influx db

    要访问 influx db,您必须调用 influx 容器 ip 或名称或服务名称

    还将 influx 中暴露的端口更新为8086:8086

    在您的情况下,将 http://localhost:8086 值更改为 influx:8086 并试一试

    【讨论】:

      猜你喜欢
      • 2019-08-28
      • 2021-01-17
      • 2020-05-17
      • 2021-07-11
      • 2020-01-08
      • 2022-01-10
      • 2020-06-12
      • 1970-01-01
      • 2020-07-19
      相关资源
      最近更新 更多