java - Spring Cloud Client 未从 Spring Cloud 服务器获取配置
问题描述
我是 Spring Cloud 的新手,我正在尝试使用存储在 github 上的属性文件连接服务器和客户端。
我的服务器 application.yml 文件配置如下:
---
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://github.com/JonEasy/pluralsight-spring-cloudconfig-wa-tolls.git
#username: uname
#password: pass
search-paths:
- 'station*'
repos:
perf:
pattern:
- '*/perf'
uri: https://github.com/JonEasy/pluralsight-spring-cloudconfig-wa-tolls-perf.git
search-paths:
- 'station*'
我的客户端应用程序具有以下设置
spring.application.name=s1rates
spring.profiles.active=default
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.enabled= true
休息控制器是:
@Controller
public class RateController {
@Value("${rate}")
String rate;
@Value("${lanecount}")
String lanecount;
@Value("${tollstart}")
String tollstart;
@RequestMapping("/rate")
public String getRate(Model m) {
m.addAttribute("rateamount", rate);
m.addAttribute("lanes", lanecount);
m.addAttribute("tollstart", tollstart);
//name of the view
return "rateview";
}
}
所有 ${variables} van 都可以在 git 存储库中的属性文件中找到。
服务器运行良好,但客户端给我以下错误
创建名为“rateController”的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 java.lang.IllegalArgumentException:无法解析值“${rate}”中的占位符“rate”
另请注意,我的 spring 应用程序名称“s1rates”与我的主存储库中的属性文件相匹配,位于 station1/s1rates.properties 下。
有什么提示吗?
解决方案
我遇到了同样的问题,发现这篇文章很有帮助。
但是,我将在这里巩固我的发现。
确保您的 spring 云客户端具有正确的 pom.xml 文件。
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>2020.0.0</version>
</parent>
并添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
休息多个帖子中给出的所有属性,通常都很好。
例如在客户端 bootstrap.properties 文件中
spring.application.name=s1rates
spring.profiles.active=default
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.enabled= true
推荐阅读
- javascript - 我的等待代码不会等待我的代码继续
- javascript - 有没有办法在没有文档的情况下找到哪些 API 参数有效?
- javascript - 创建钩子时出错(Promise/async):“TypeError:无法读取未定义的属性‘位置’”
- gremlin - gremlin 查询在控制台中有效,但在 javascript 中无效
- asp.net-core - Asp.Net Core 3.1 Web App(Razor Pages)SignalR - OnChange 事件在 SqlDependency 上多次命中,对于单个客户端
- python - 硒中的按钮不可点击,没有错误消息
- github-actions - 即使在主键上发生命中,也覆盖 github 操作缓存
- purescript - 由于使用了类型(行类型),类型类实例头部无效
- javascript - 如何递归地加入两个嵌套的可观察对象
- python - 我可以将 LSTM 层与 conv1d 层与 Keras 功能 API 结合起来吗