如何从Docker化的Spring Boot连接到Docker化的Couchbase服务器

huangapple go评论78阅读模式
英文:

How to connect dockerized couchbase server from dockerized spring boot

问题

以下是您提供的内容的翻译:

我创建了一个简单的 Spring Boot 和 Couchbase 应用,现在想将它们容器化。

我为 Spring Boot 创建了 Dockerfile:

FROM maven:3.6.0-jdk-8-slim AS build
RUN mkdir -p /workspace
WORKDIR /workspace
COPY pom.xml /workspace
COPY src /workspace/src
RUN mvn -f pom.xml clean package

FROM openjdk:8
COPY --from=build /workspace/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]

以及我的 Couchbase 的 Dockerfile:

FROM couchbase

RUN chmod +x /entrypoint.sh
COPY configure-server.sh /opt/couchbase
RUN chmod +x /opt/couchbase/configure-server.sh

CMD ["/opt/couchbase/configure-server.sh"]

以及 Docker-compose:

version: '3.0'
services:
  couchbase:
    build: ./couchbase
    container_name: couchbase
    volumes:
      - ~/couchbase/node1:/opt/couchbase/var
    ports:
      - 8091:8091
      - 8092:8092
      - 8093:8093
      - 11210:11210

  todo_server:
    build: ./todo-backend
    container_name: todo_server
    depends_on:
      - couchbase
    ports:
      - 8080:8080

当我尝试运行 docker-compose up couchbase 并在本地运行 Spring Boot 时,它们可以一起工作。但是当我尝试使用 docker-compose up 时,Spring 应用无法连接到 Couchbase 服务器。

我猜这是由于 localhost 导致的。

在 Spring 中,我的 Couchbase 配置如下:

@Configuration
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
    @Override
    public String getConnectionString() {
        return "couchbase://127.0.0.1";
    }

    @Override
    public String getUserName() {
        return "Administrator";
    }

    @Override
    public String getPassword() {
        return "password";
    }

    @Override
    public String getBucketName() {
        return "default";
    }

    @Override
    protected boolean autoIndexCreation() {
        return true;
    }
}

我认为我需要更改这一部分:

public String getConnectionString() {
    return "couchbase://127.0.0.1";
}

我尝试过只用 couchbasecouchbase://couchbase,但都不起作用。

控制台上的错误信息如下:

com.couchbase.client.core.deps.io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: /127.0.0.1:11210
Caused by: java.net.ConnectException: finishConnect(..) failed: Connection refused
        at com.couchbase.client.core.deps.io.netty.channel.unix.Errors.throwConnectException(Errors.java:124) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.channel.unix.Socket.finishConnect(Socket.java:243) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.doFinishConnect(AbstractEpollChannel.java:672) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.finishConnect(AbstractEpollChannel.java:649) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.epollOutReady(AbstractEpollChannel.java:529) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:465) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:378) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[core-io-2.0.8.jar:na]
        at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_212]

您能帮助我吗?

英文:

I create a simple spring boot and couchbase app and I want to dockerized it.

I create Dockerfile for Spring Boot:

FROM maven:3.6.0-jdk-8-slim AS build
RUN mkdir -p /workspace
WORKDIR /workspace
COPY pom.xml /workspace
COPY src /workspace/src
RUN mvn -f pom.xml clean package

FROM openjdk:8
COPY --from=build /workspace/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]

and my couchbase's dockerfile:

FROM couchbase

RUN chmod +x /entrypoint.sh
COPY configure-server.sh /opt/couchbase
RUN chmod +x /opt/couchbase/configure-server.sh

CMD ["/opt/couchbase/configure-server.sh"]

And Docker-compose:

version: '3.0'
services:
  couchbase:
    build: ./couchbase
    container_name: couchbase
    volumes:
      - ~/couchbase/node1:/opt/couchbase/var
    ports:
      - 8091:8091
      - 8092:8092
      - 8093:8093
      - 11210:11210

  todo_server:
    build: ./todo-backend
    container_name: todo_server
    depends_on:
      - couchbase
    ports:
      - 8080:8080

If I try to docker-compose up couchbase and run spring boot locally they work together. But When I try to use docker-compose up, Spring App can not connect to couchbase server.

I guess this is caused by localhost

My Couchbase Config on Spring

@Configuration
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
    @Override
    public String getConnectionString() {
        return "couchbase://127.0.0.1";
    }

    @Override
    public String getUserName() {
        return "Administrator";
    }

    @Override
    public String getPassword() {
        return "password";
    }

    @Override
    public String getBucketName() {
        return "default";
    }

    @Override
    protected boolean autoIndexCreation() {
        return true;
    }
}

I think I have to change this

 public String getConnectionString() {
        return "couchbase://127.0.0.1";
    }

I tried only couchbase and couchbase://couchbase but it doesn't work.

Errors on console:

com.couchbase.client.core.deps.io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: /127.0.0.1:11210
Caused by: java.net.ConnectException: finishConnect(..) failed: Connection refused
        at com.couchbase.client.core.deps.io.netty.channel.unix.Errors.throwConnectException(Errors.java:124) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.channel.unix.Socket.finishConnect(Socket.java:243) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.doFinishConnect(AbstractEpollChannel.java:672) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.finishConnect(AbstractEpollChannel.java:649) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe.epollOutReady(AbstractEpollChannel.java:529) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:465) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:378) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[core-io-2.0.8.jar:na]
        at com.couchbase.client.core.deps.io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[core-io-2.0.8.jar:na]
        at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_212]


Can you help me?

答案1

得分: 2

我通过以下方式解决了这个问题。

我的新Docker Compose文件:

version: '3.7'
services:
  db:
    build: ./couchbase
    container_name: todo-couchbase
    volumes:
      - ~/couchbase/node1:/opt/couchbase/var
    ports:
      - 8091:8091
      - 8092:8092
      - 8093:8093
      - 11210:11210
    networks:
      - my_network
  todo_server:
    build: ./todo-backend
    container_name: todo-backend
    environment:
      COUCHBASE_HOST: db
    depends_on:
      - db
    ports:
      - 8080:8080
    networks:
      - my_network

networks:
  my_network:
    driver: bridge

我修改了连接字符串:

public String getConnectionString() {
    return "db";
}
英文:

I solved this problem by doing the following.

My new Docker-compose file:

version: '3.7'
services:
  db:
    build: ./couchbase
    container_name: todo-couchbase
    volumes:
      - ~/couchbase/node1:/opt/couchbase/var
    ports:
      - 8091:8091
      - 8092:8092
      - 8093:8093
      - 11210:11210
    networks:
      - my_network
  todo_server:
    build: ./todo-backend
    container_name: todo-backend
    environment:
      COUCHBASE_HOST: db
    depends_on:
      - db
    ports:
      - 8080:8080
    networks:
      - my_network

networks:
  my_network:
    driver: bridge

and I changed connection string

 public String getConnectionString() {
        return "db";
    }

huangapple
  • 本文由 发表于 2020年9月11日 19:43:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63846409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定