Search code examples
dockerjhipsterjhipster-registry

Dynamic hostname in @AuthorizedFeignClient


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);
}

Solution

  • 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")