简体   繁体   English

Docker Swarm JDBC 连接

[英]Docker Swarm JDBC connection

Running a Postgresql DB on a Docker Swarm containing multiple nodes where the Database can be deployed.在包含可以部署数据库的多个节点的 Docker Swarm 上运行 Postgresql 数据库。 Using Docker version 1.12+.使用 Docker 1.12+ 版本。

Using a Data container, the Postgresql failover is working nicely.使用数据容器,Postgresql 故障转移运行良好。 Now I would like to have a Java client connect to the DB and also survive failover.现在我想让一个 Java 客户端连接到数据库并且还能在故障转移中存活下来。 How should the JDBC connections be managed here?这里应该如何管理 JDBC 连接? Does the connection string change?连接字符串是否更改? Or should it be managed through something like an nginx container running elsewhere?还是应该通过在其他地方运行的 nginx 容器之类的东西来管理它? Is there an example of this available for study anywhere?有没有这样的例子可以在任何地方学习? Conceptually, I think I get moving this off to another (nginx-like) container, but can't quite grok the details!从概念上讲,我想我可以把它移到另一个(类似 nginx 的)容器中,但不能完全理解细节!

In swarm mode, you get service discovery by DNS name for services in the same overlay network, you don't need to add a proxy layer yourself.在 swarm 模式下,您可以通过 DNS 名称为同一覆盖网络中的服务进行服务发现,您无需自己添加代理层。 The swam networking docs go into detail, but in essence:游泳网络文档详细介绍,但本质上:

docker network create -d overlay app-net
docker service create --name app-db --network app-net [etc.]
docker service create --name app-web --network app-net [etc.]

Your database server is available by DNS within the network as app-db , to any service in the same app-net network.您的数据库服务器可通过网络内的 DNS 作为app-db访问同一app-net网络中的任何服务。 So app-db is the server name you use in your JDBC connection string.所以app-db是您在 JDBC 连接字符串中使用的服务器名称。 You can have multiple replicas of the Postgres container, or a single container which moves around at failover - the service will always be available at that address.您可以拥有 Postgres 容器的多个副本,或者在故障转移时移动的单个容器 - 该服务将始终在该地址可用。

But: I would be cautious about failover with your data container.但是:我对您的数据容器的故障转移持谨慎态度。 You have a single container with your database state there;您有一个包含数据库状态的容器; even if your state is in a volume, it won't move around the cluster.即使您的状态在一个卷中,它也不会在集群中移动。 So if the node with the data fails, your data container will start somwhere else, but the data won't go with it.因此,如果包含数据的节点出现故障,您的数据容器将在其他地方启动,但数据不会随之而来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM