@AuthorizedFeignClient中的动态主机名

时间:2018-11-08 13:35:49

标签: docker jhipster jhipster-registry

在微服务系统中,我有一个带有注释@AuthorizedFeignClient(name="send-email", url="http://localhost:8080/utils/api/email")的接口,在开发环境中它可以正常工作,但是,在Docker环境中,我需要 url 参数具有Docker容器名称代替localhost以便在Docker环境中工作。

我尝试在application-prod.yml中添加配置:

containers-host:
    gateway: jhipster-gateway

在注释中,我将其像这样放置:

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

但是,当尝试生成.war时,它会失败:

  

org.springframework.beans.factory.BeanDefinitionStoreException:名称为“ com.sistema.service.util.EmailClient”的无效bean定义为null:无法解析值“ http:/”中的占位符“ containers-host.gateway” /${containers-host.gateway}:8080/utils/api/email“;嵌套异常为java.lang.IllegalArgumentException:无法解析值“ http:// $ {containers-host.gateway}:8080 / utils / api / email”中的占位符“ containers-host.gateway”   2018-11-08 11:25:22.101错误64 --- [main] o.s.boot.SpringApplication:应用程序运行失败

     

org.springframework.beans.factory.BeanDefinitionStoreException:名称为“ com.sistema.service.util.EmailClient”的无效bean定义为null:无法解析值“ http:/”中的占位符“ containers-host.gateway” /${containers-host.gateway}:8080/utils/api/email“;嵌套异常是java.lang.IllegalArgumentException:无法解析值“ http:// $ {containers-host.gateway}:8080 / utils / api / email”中的占位符'containers-host.gateway'

如何根据其运行环境的配置来设置服务的主机名?

失败的代码如下:

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

    @PostMapping("/send-email")
    void sendEmail(String mail);
}

1 个答案:

答案 0 :(得分:0)

要设置在application-dev.yml或application-prod.yml中输入的值,必须创建带有@ConfigurationProperties批注的配置类。

在.yml文件中:

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

以这种方式创建配置文件:

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

并使@AuthorizedFeignClient像这样:

@AuthorizedFeignClient(name="send-email", url="${microservices.gateway}/utils/api/email")
相关问题