【问题标题】:Dynamic hostname in @AuthorizedFeignClient@AuthorizedFeignClient 中的动态主机名
【发布时间】:2018-11-08 13:35:48
【问题描述】:

在微服务系统中,我有一个带有注解@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:无效的 bean 定义,名称为 'com.sistema.service.util.EmailClient' 定义为 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] os.boot.SpringApplication:应用程序运行失败

org.springframework.beans.factory.BeanDefinitionStoreException:无效的 bean 定义,名称为 'com.sistema.service.util.EmailClient' 定义为 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);
}

【问题讨论】:

  • 您是否尝试使用服务名称?
  • 不能是服务名,必须是网关地址,所以在开发环境中使用localhost,在Docker中使用容器名,因为是http请求,所以我想看看在注解中设置主机的更动态的方式。
  • 像@malyy我也认为你应该在url中使用服务名,没有主机名也没有端口,这就是为什么我们有一个feign客户端使用的服务注册表。你能解释一下为什么它必须是网关地址吗?
  • 我的意思是 - 来自 docker-compose 文件的服务名称。
  • 如果我把docker服务的名字放到docker中它只在docker中有效,在开发环境中无效(开发者的电脑上没有docker),所以我想看看它是否有动态应用程序匹配环境的形状。

标签: docker jhipster jhipster-registry


【解决方案1】:

要设置在 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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多