0

In a microservice system, I have an interface with the annotation @AuthorizedFeignClient(name="send-email", url="http://localhost:8080/utils/api/email"), in the development environment it works correctly, however, in the Docker environment, I need the url parameter to have the Docker container name in place of localhost to work in the Docker environment.

I tried adding in the application-prod.yml the configuration:

containers-host:
    gateway: jhipster-gateway

And in the annotation I put it like this:

     @AuthorizedFeignClient(name="send-email", url="http://${containers-host.gateway}:8080/utils/api/email")

However, when trying to generate the .war it fails:

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'com.sistema.service.util.EmailClient' defined in null: Could not resolve placeholder 'containers-host.gateway' in value "http://${containers-host.gateway}:8080/utils/api/email"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'containers-host.gateway' in value "http://${containers-host.gateway}:8080/utils/api/email" 2018-11-08 11:25:22.101 ERROR 64 --- [ main] o.s.boot.SpringApplication : Application run failed

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'com.sistema.service.util.EmailClient' defined in null: Could not resolve placeholder 'containers-host.gateway' in value "http://${containers-host.gateway}:8080/utils/api/email"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'containers-host.gateway' in value "http://${containers-host.gateway}:8080/utils/api/email"

How can I do to set the hostname of the service according to the configuration of the environment it runs?

My code that is failing looks like this:

@AuthorizedFeignClient(name="send-email", url="http://${containers-host.gateway}:8080/utils/api/email")
public interface EmailClient {

    @PostMapping("/send-email")
    void sendEmail(String mail);
}
user2831852
  • 535
  • 2
  • 7
  • 22
  • did you try to use a service name ? – malyy Nov 08 '18 at 14:26
  • It can not be the service name, it must be the address of the gateway, so in the dev environment is used localhost and in Docker the container name is used, since it is an http request, so I want to see a more dynamic way of setting the host in the annotation. – user2831852 Nov 08 '18 at 16:12
  • Like @malyy I also think you should use service name in url, no hostname nor port, this is why we have a service registry that is used by feign client. Could you explain why it must be the gateway address? – Gaël Marziou Nov 08 '18 at 18:19
  • I meant - service name from docker-compose file. – malyy Nov 09 '18 at 08:14
  • If I put the name of the docker service it works only in docker and not in the development environment (there is no docker on the developers' computers), so I want to see if it has a dynamic shape that matches the environment by the application. – user2831852 Nov 09 '18 at 11:58

1 Answers1

1

To set the value entered in application-dev.yml or application-prod.yml it is necessary to create a configuration class with the @ConfigurationProperties annotation.

In the .yml file:

microservices:
    gateway: http://environment-host:8080

Create the configuration file this way:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;


@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "microservices", ignoreUnknownFields = false)
public class MicroservicesConectionProperties {

    private String gateway = "";

    public String getGateway() {
        return gateway;
    }

    public void setGateway(String gateway) {
        this.gateway = gateway;
    } 
}

And make the @AuthorizedFeignClient like this:

@AuthorizedFeignClient(name="send-email", url="${microservices.gateway}/utils/api/email")
user2831852
  • 535
  • 2
  • 7
  • 22