@AuthorizedFeignClient中的动态主机名 [英] Dynamic hostname in @AuthorizedFeignClient

查看:702
本文介绍了@AuthorizedFeignClient中的动态主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

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: 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:名称为'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'

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?

失败的代码如下:

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

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

推荐答案

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

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.

在.yml文件中:

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

并使@AuthorizedFeignClient像这样:

And make the @AuthorizedFeignClient like this:

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

这篇关于@AuthorizedFeignClient中的动态主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆