问题描述
我想使用一组由Eureka注册的服务所提供的属性来引导Web应用程序。
我在本地设置了Eureka,configserver(REST服务)和客户端应用程序。
在端口8761
中公开的Eureka Server已向其注册了服务。
配置服务器代码
@GetMapping("/data")
public Map<String,String> getAllProperties(){
Map<String,String> dataMap = new HashMap<>();
dataMap.put("A","1");
dataMap.put("B","2");
dataMap.put("C","3");
dataMap.put("D","4");
dataMap.put("E","5");
dataMap.put("F","6");
dataMap.put("G","7");
return dataMap;
}
客户端/应用程序端
由于我需要对响应进行自定义处理,因此我创建了一个CustomConfigLocator
类,该类实现了org.springframework.cloud.bootstrap.config.PropertySourceLocator
并覆盖了locate()
方法以查询和设置自定义属性。
为了查询数据(属性),我需要一个discoveryClient
的实例来发现Config Server
并查询/data
端点。
CustomConfigLocator
在使用spring.factories
文件引导应用程序时被初始化。
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.config.client.ConfigClientApplication.CustomConfigLocator
主类使用 @EnablediscoveryClient 进行注释,bootstrap.properties具有以下属性。
spring.cloud.config.discovery.enabled = true
spring.cloud.config.discovery.service-id = CUSTOMconfigserver
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
现在是真正的问题。如前所述,需要一个discoveryClient
实例才能从Eureka发现Config Server实例。但是,当我尝试注入(@Autowire)org.springframework.cloud.client.discovery.discoveryClient
的实例时,应用程序无法开始抱怨该类型的实例在容器中不可用。
public class CustomConfigLocator implements PropertySourceLocator{
@Autowired
private discoveryClient discoveryClient;
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名称为bean的错误 'com.config.client.ConfigClientApplication.CustomConfigLocator': 通过字段“ discoveryClient”表示不满意的依赖关系; 嵌套异常为 org.springframework.beans.factory.NoSuchBeanDeFinitionException:否 类型的合格豆 'org.springframework.cloud.client.discovery.discoveryClient' 可用:至少有1个符合自动装配条件的bean 候选人。依赖注释:
有人可以建议从CustomConfigLocator
类访问 discoveryClient 的方法吗?
############## UPDATE-1 ###############
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage',module: 'junit-vintage-engine'
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)