feignclient多个配置_spring-boot - 如何使ConfigurationProperties bean中的两个验证注释和@FeignClient接口一起工作? - 堆栈内存溢出...

在Spring Boot应用中,使用@FeignClient与@ConfigurationProperties结合时遇到验证注解不生效的问题。当app.remote.url为空时,应用启动失败并提示缺少负载均衡配置。尝试将MyRemoteProperties注入配置未解决问题。寻求解决方案。
摘要由CSDN通过智能技术生成

假设我有这个application.yml(将取决于环境,例如通过Spring概要文件):

app.remote:

url: http://whatever.url.it.is:8080/

并匹配Java样式的配置属性类:

@Configuration

@ConfigurationProperties("app.remote")

public class MyRemoteProperties {

@NotBlank

private String url;

// matching getter/setter...

}

我想要某种客户端作为我的远程URL:

@Service

@FeignClient(value = "remote", url = "${app.remote.url}")

public interface MyRemote {

@GetMapping("/what/ever/rest/api")

String stuff();

}

不幸的是,我无法获得MyRemoteProperties的验证工作,例如,当app.remote.url属性为空(空)时,应用程序无法启动(Spring无法连接MyRemote Bean),并且出现此错误:

原因:java.lang.IllegalStateException:没有为loadBalancing定义的Feign客户端。 您是否忘了包含spring-cloud-starter-netflix-ribbon?

(并且我不希望负载平衡;我认为这是因为URL有时是空的,所以它需要一些负载平衡器配置,因此在错误消息中位于功能区)。

或者也许我不知道如何将其插入MyRemote接口的配置,例如,我也尝试过:

@FeignClient(value = "remote", configuration = MyRemoteProperties.class)

但结果相同。

如何使此验证功能生效?

pom.xml

org.springframework.boot

spring-boot-starter-parent

2.1.8.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-openfeign

org.springframework.cloud

spring-cloud-dependencies

Greenwich.SR3

pom

import

在某个时候调用该接口:

@Service

public RandomServiceOrController {

@Autowired

private MyRemote myRemote;

public void processMyStuff() {

// ...

String myStuff = myRemote.stuff();

// ...

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值