PacktPublishing / Hands-On-Microservices-with-Spring-Boot-and-Spring-Cloud

Hands-On Microservices with Spring Boot and Spring Cloud, published by Packt
MIT License
461 stars 406 forks source link

Chap12 start docker compose config-server connection refused #17

Closed srodrigues37 closed 3 years ago

srodrigues37 commented 3 years ago

After a day trying to understand chap 12, i had an issue on docker compose. On start eureka, gateway and auth-server had same issue http://config-server:8888 connection refused and all stop.

I finally found a "solution" add depends-on: config-server for them on docker-compose.yml.

config-server:
...
  healthcheck:
      test: [ "CMD", "curl", "-I", "http://config-server:8888" ]
      interval: 5s
      timeout: 5s
      retries: 10000

and

depends_on:
      config-server:
        condition: service_healthy

May be it's not the rigth way.

I use spring boot 2.4.3 and 2020.0.1 for spring cloud

magnus-larsson commented 3 years ago

The microservices are designed to be loosely coupled to each other, i.e. allowing other microservices to temporary go up and down for various reasons. Therefore, I have avoided to set up dependencies between different microservices, only using dependencies from microservices to the resource managers they depends on, such as its database ans message broker.

When it comes to the dependency to the config-server, each microservice has the following declaration in this bootstrap.yml file allowing it to retry connection to the config-server:

spring:
  application.name: product
  cloud.config:
    failFast: true
    retry:
      initialInterval: 3000
      multiplier: 1.3
      maxInterval: 10000
      maxAttempts: 20

So, given that the config-server starts up before the 20 attempts are done, they should connect ok.

The book is based on and verified to work with Spring Boot 2.3.x and the Spring Cloud Hoxton release. Since you mention that you use Spring Boot 2.4.3 and Spring Cloud 2020.0.1, i have a couple of questions to you:

  1. Does chapter 12 work in your environment if you don't change any code, e.g. don't upgrade Spring Boot and Spring Cloud?
  2. Have you read about the changes the Spring team have introduced in Spring Boot 2.4?

From my understanding, source code using Spring Config with Spring Boot 2.3.x and the Spring Cloud Hoxton will not work using Spring Boot 2.4.3 and Spring Cloud 2020.0.1. You either have to start using the new Spring Boot import mechanism:

spring.config.import=configserver:

Or tell Spring that you want to use the legacy bootstrap as described in the second link above.

srodrigues37 commented 3 years ago

Hi thanks for the answer,

I always try to used latest features when reading a book and adapt the code, it's a good way to understand.

First i want to congrats you for your book, i think that it's the best one about spring cloud infrastructure.

For chap 12 i add this dependency to allow bootstrap file implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'

I also have to change some dependencies for springfox swagger

I have a lot of deprecated alerts, that I have to upgrade but all is working from chapter 1 to 13. Only config.cloud.try cause an issue. The config.cloud.try seems to doesn't work that's why i create this patch.

I'll try to solve this with the links that you shared.

Thank's a lot

srodrigues37 commented 3 years ago

It's working for gateway only if we add on bootstrap.yml

spring:
  application.name: eureka-server
  cloud:
    **bootstrap.enabled: true**
    config:
      failFast: true
      retry:
        initialInterval: 3000
        multiplier: 1.3
        maxInterval: 10000
        maxAttempts: 20
      uri: http://${CONFIG_SERVER_USR}:${CONFIG_SERVER_PWD}@${app.config-server}:8888

and also aop dependency implementation 'org.springframework.boot:spring-boot-starter-aop'

I try spring.config.import=configserver:${app.config-server}:8888 but i have an error ${app.config-server} doesn't exist

magnus-larsson commented 3 years ago

Hello again!

It's hard for me to support you on this issue given the large number of changes required by the newer Spring versions.

Just guessing here, but given that you have problems with environment specific variables like ${app.config-server} I wonder if you saw this note from the Spring team in the first link above: https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4

In Spring Boot 2.3, you’d use the spring.profiles key to do this. With Spring Boot 2.4, we’ve decided to change the property to spring.config.activate.on-profile.

If you did not change that Spring property, you will most probably not get the correct values for environment specific variable like @${app.config-server}. It will for example translate to localhost instead of config-server if the Docker profile can't be found.

Hope it helps.

srodrigues37 commented 3 years ago

I know i just push all my progress to help all I finish to make it working with this application.yml

app.config-server: localhost

spring:
  application.name: auth-server
  config:
    import: configserver:http://config-server:8888 # example: import: configserver:http://192.168.0.4:8080
  cloud:
    bootstrap.enabled: true
    config:
      username: ${CONFIG_SERVER_USR} # This is required only if your config server use authentication
      password: ${CONFIG_SERVER_PWD} # This is required only if your config server use authentication
      failFast: true
      retry:
        initialInterval: 3000
        multiplier: 1.3
        maxInterval: 10000
        maxAttempts: 20

---

spring:
  config:
    activate:
      on-profile: docker

app.config-server: config-server

But i can't us ${app.config-server} on spring.config.import parameter

srodrigues37 commented 3 years ago
app.config-server: localhost

spring:
  application.name: eureka-server
  config:
    import: configserver:http://${app.config-server}:8888 # example: import: configserver:http://192.168.0.4:8080
Failed to bind properties under 'spring.config.import' to java.util.List<org.springframework.boot.context.config.ConfigDataLocation>:

Property: spring.config.import

Value: configserver:http://${app.config-server}:8888

Origin: class path resource [application.yml] from app.jar - 6:13

Reason: Inactive property source 'Config resource 'class path resource [application.yml]' via location 'optional:classpath:/' (document #1)' imported from location 'class path resource [application.yml]' cannot contain property 'app.config-server' [origin: class path resource [application.yml] from app.jar - 26:20]

Action:

Update your application's configuration
ClimberBear commented 3 years ago

I had serveral problems with this chapter and config-server. Take a look to my question in stackoverflow In short, the 3.0.0 version

magnus-larsson commented 3 years ago

Thanks for the clarification regarding the need of Spring Cloud Config 3.0.1. It comes bundled with Spring Cloud 2020.0.1.

Also, please let me know if you have any problems when using the versions used in this git repo!

alansastre commented 2 years ago

The above options didn't work for me, but restart: on-failure in docker-compose.yml yes.

Hope it helps.