Dynamic hostname in @AuthorizedFeignClient

时间秒杀一切 提交于 2021-01-29 19:33:04

问题


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

回答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")


来源:https://stackoverflow.com/questions/53208861/dynamic-hostname-in-authorizedfeignclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!