I try to use service name to connect my nomad job Postgresql and the app. Using agent node where the job is running works fine but with my job.service.consul do not work. As decribe in application.properties using ip or host works but service name does'nt work Can you help me ?
Application.properties
#Enable ddl
spring.jpa.hibernate.ddl-auto=none
##Works
#spring.datasource.url=jdbc:postgresql://10.3.52.121:5432/postgres
#spring.datasource.url=jdbc:postgresql://vmc####26.dev.##.##.##.##.##:5432/postgres
##DON'T WORK WITH SERVICE NAME
#spring.datasource.url=jdbc:postgresql://pgsql-##.service.consul:5432/postgres
spring.datasource.url=jdbc:postgresql://${DB_SERVICE_NAME}:5432/postgres
spring.datasource.username=${POSTGRES_USER}
spring.datasource.password=${POSTGRES_PASSWORD}
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Postgresql job
job "pgsql-qa" {
datacenters = ["###"]
type = "service"
vault {
policies = ["###"]
change_mode = "noop"
}
group "pgsql-qa" {
count = 1
task "pgsql-qa" {
driver = "docker"
config {
image = "postgres"
volumes = [
"name=####pgsqldb,io_priority=high,size=5,repl=1:/var/lib/postgresql/data"
]
volume_driver = "pxd"
network_mode = "bridge"
port_map {
db = 5432
}
}
template {
data = <<EOH
POSTGRES_USER="{{ with secret "app/###/###/db/admin" }}{{ .Data.data.user }}{{end}}"
POSTGRES_PASSWORD="{{ with secret "app/###/###/db/admin" }}{{ .Data.data.password }}{{end}}"
EOH
destination = "secrets/db"
env = true
}
logs {
max_files = 5
max_file_size = 15
}
resources {
cpu = 1000
memory = 1024
network {
mbits = 10
port "db" {
static = 5432
}
}
}
service {
name = "pgsql-qa"
tags = ["urlprefix-pgsqlqadb proto=tcp"]
port = "db"
check {
name = "alive"
type = "tcp"
interval = "10s"
timeout = "2s"
port = "db"
}
}
}
restart {
attempts = 10
interval = "5m"
delay = "25s"
mode = "delay"
}
}
update {
max_parallel = 1
min_healthy_time = "5s"
healthy_deadline = "3m"
auto_revert = false
canary = 0
}
}
I found the solution. Just add this line in template {} section to get the IP and the random port created by Nomad. db_service_name change this line with your service name.
MY_DB = "{{ range service "${db_service_name}" }}{{ .Address }}:{{ .Port }}{{ end }}"
Thx