【问题标题】:Spring boot with a reactive mongodb keeps trying to connect to localhost带有反应式 mongodb 的 Spring Boot 不断尝试连接到 localhost
【发布时间】:2021-09-08 06:18:31
【问题描述】:

我有一个反应式 Spring Boot API/MongoDB,我想对其进行 docker 化,但 Spring-boot 应用程序不断尝试连接到 localhost:21017 上的 mongoDB。这显然给了我一个拒绝回复的连接。

我尝试的所有选项,例如排除自动配置类,覆盖 reactiveMongoTemplate,似乎没有任何效果。我很想听听一些建议

我的 docker-compose:

version: "3.7"
services:
  api_service:
    build: .
    restart: always
    hostname: spring
    ports:
      - 8080:8080
    container_name: "review-service"
    depends_on:
      - mongo
  mongo:
    image: "mongo:latest"
    hostname: mongo
    restart: always
    container_name: mongo_review
    ports:
      - 27017:27017

应用程序属性

spring.data.mongodb.host=mongo
spring.data.mongodb.database=test
spring.data.mongodb.port=27017

应用程序类

@SpringBootApplication(exclude={MongoReactiveAutoConfiguration.class, MongoReactiveDataAutoConfiguration.class})
@EnableReactiveMongoRepositories
public class ConferenceAppReviewReactiveApplication extends AbstractReactiveMongoConfiguration implements CommandLineRunner  {
    @Autowired
    private ReviewService reviewService;

    public static void main(String[] args) {
        SpringApplication.run(ConferenceAppReviewReactiveApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Review review = new Review();
        review.setRev_referenceId("60d093a31ee645007b2df2cb");
        reviewService.save(review);
    }
    @Bean
    public MongoClient mongoClient() {
        return MongoClients.create();
    }
    @Override
    protected String getDatabaseName() {
        return "test";
    }

    @Override
    public MongoClient reactiveMongoClient() {
        return MongoClients.create("mongodb://mongo:27017/test");
    }

【问题讨论】:

    标签: mongodb spring-boot docker spring-webflux


    【解决方案1】:

    找到解决方案:

    • 删除所有配置(扩展 AbstractReactiveMongoConfiguration)
    • 删除所有排除项

    显然@EnableReactiveMongoRepositories 就足够了,剩下的交给Spring来做

    【讨论】:

      猜你喜欢
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 2017-10-26
      相关资源
      最近更新 更多