Spring Boot uses incorrect config server url – http://localhost instead of http://config

132 Views Asked by At

I have two modules in my project: config-server(spring-cloud-config-server) and service(spring-cloud-starter-config).

service application.properties:

server.port=0

spring.application.name=service

spring.config.import=optional:configserver:http://config:8888
spring.cloud.config.username=root
spring.cloud.config.password=secret

config-server application.properties:

server.port=8888
spring.application.name=config
spring.cloud.config.server.native.search-locations=classpath:/{application}/
spring.profiles.active=native
spring.security.user.name=root
spring.security.user.password=secret

docker-compose.yml:

version: "3"

services:
  config:
    build:
      context: config-server
      dockerfile: ./Dockerfile
    ports:
      - "8888:8888"
    networks:
      - test-network
    container_name: config
  service:
    build:
      context: service
      dockerfile: ./Dockerfile
    restart: always
    networks:
      - test-network
    depends_on:
      - config
    container_name: service

networks:
  test-network:
    driver: bridge

If I run docker-compose, service will fail because it can't to access http://localhost:8888, but there is no localhost urls in my project at all. As you see there is a spring.config.import=optional:configserver:http://config:8888 in properties-file. When I run service locally, it fails because http://config:8888 is unknown (as expected due to properties)

Exception log on local run:

2023-12-01T20:54:32.405+04:00  INFO 80254 --- [           main] o.s.c.c.c.ConfigServerConfigDataLoader   : Fetching config from server at : http://config:8888
2023-12-01T20:54:32.405+04:00  INFO 80254 --- [           main] o.s.c.c.c.ConfigServerConfigDataLoader   : Exception on Url - http://config:8888:org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://config:8888/service2/default": config. Will be trying the next url if available
2023-12-01T20:54:32.405+04:00  WARN 80254 --- [           main] o.s.c.c.c.ConfigServerConfigDataLoader   : Could not locate PropertySource ([ConfigServerConfigDataResource@2f98635e uris = array<String>['http://config:8888'], optional = true, profiles = 'default']): I/O error on GET request for "http://config:8888/service2/default": config

Exception log on docker-compose run:

2023-12-01T16:31:31.641Z  INFO 1 --- [           main] o.s.c.c.c.ConfigServerConfigDataLoader   : Fetching config from server at : http://localhost:8888
2023-12-01T16:31:31.642257888Z 2023-12-01T16:31:31.641Z  INFO 1 --- [           main] o.s.c.c.c.ConfigServerConfigDataLoader   : Exception on Url - http://localhost:8888:org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/service/default": Connection refused. Will be trying the next url if available
2023-12-01T16:31:31.642333971Z 2023-12-01T16:31:31.642Z  WARN 1 --- [           main] o.s.c.c.c.ConfigServerConfigDataLoader   : Could not locate PropertySource ([ConfigServerConfigDataResource@20140db9 uris = array<String>['http://root:secret@localhost:8888'], optional = true, profiles = 'default']): I/O error on GET request for "http://localhost:8888/service/default": Connection refused
1

There are 1 best solutions below

0
On

According to Loading comment, Deleting images is required. docker-compose doesn't rebuild images on each run