【发布时间】:2018-07-24 16:01:09
【问题描述】:
每个人。我在项目部署方面遇到了一些问题,我已经花了几天的时间来解决它。 我在堆栈上开发移动后端:
- Spring Boot 1.5 + 开发工具
- PostgreSQL
- 码头工人
- 分级
docker-compose.yml
version: '3'
services:
web:
image: mobilebackend
ports:
- 8088:8080
depends_on:
- db
links:
- db
db:
container_name: transneft_db
image: postgres
restart: always
volumes:
- transneft_db:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=pass
- POSTGRES_USER=user
- POSTGRES_DB=db
- PGDATA=/var/lib/postgresql/data/pgdata
ports:
- 54320:5432
adminer:
image: adminer
restart: always
ports:
- 8082:8080
volumes:
transneft_db: {}
application.properties
spring.datasource.url=jdbc:postgresql://localhost:54320/db
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.platform=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database=POSTGRESQL
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
jwt.secret =aotransneftsibir
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
build.gradle
buildscript {
ext {
springBootVersion = '1.5.10.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath('se.transmode.gradle:gradle-docker:1.2')
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'docker'
apply plugin: 'application'
repositories {
mavenCentral()
}
compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
mainClassName = "com.backend.MobilebackendApplication"
}
jar {
baseName = "backend-api"
group = "com.backend"
version = "0.0.1-SNAPSHOT"
manifest { attributes "Main-Class": "com.backend.mobilebackend.MobilebackendApplication" }
}
docker {
baseImage "frolvlad/alpine-oraclejdk8:slim"
maintainer 'Alex Scrobot "scrobot91@gmail.com"'
}
dependencies {
// spring boot
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
//postgresql
runtime('org.postgresql:postgresql')
//gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.7'
// JWT
compile 'io.jsonwebtoken:jjwt:0.9.0'
//test env
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}
在本地主机上运行良好,项目正在使用 DevTools 构建并且运行良好。带有 postgresql 的 Docker 容器启动了,我可以通过 localhost:port 访问它的连接,一切都很好。
问题开始了,然后我试着打电话:
./gradlew build distDocker --refresh-dependencies
在这种情况下,spring.datasource.url 属性必须包含 localhost 值,构建将失败。我输入 localhost 值,获得成功消息,该图像已构建。所以,然后我尝试使用 Spring Boot .jar 运行容器,我得到“连接被拒绝错误”。我对这种情况有一些想法:带有 .jar 的容器正在运行,它尝试通过 localhost:db_port 获取数据库访问权限,但在此容器内不能有 db 连接到其 localhost。所以,然后我把 application.property
spring.datasource.url=jdbc:postgresql://db:54320/db
但是,我无法构建更新的图像。然后gradle尝试启动组装好的.jar,它无法访问db:54320,因为启动不在容器中运行,并且正确连接localhost:54320。你有没有感觉到我的感觉的恶性循环呢?))
我不明白,我做错了什么...请帮我解决这个问题。谢谢。
【问题讨论】:
-
人,感谢 spring-postgres-gradle 项目的例子,我在互联网上找不到工作。
标签: java postgresql docker spring-boot docker-compose